#wierd bug while using useRuntimeConfig()

1 messages · Page 1 of 1 (latest)

west wingBOT
#

Hi @pseudo torrent. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the knowledge sources, I can address the issue you're experiencing with useRuntimeConfig(). The behavior you're seeing is not actually a bug, but rather a misuse of the useRuntimeConfig() function. Let me explain:

  1. The await keyword before useRuntimeConfig(event) is unnecessary and should be removed. useRuntimeConfig() is not an asynchronous function and doesn't return a Promise.

  2. You're correctly passing the event parameter to useRuntimeConfig(event), which is recommended for server routes to ensure you get the runtime config overwritten by environment variables at runtime as mentioned in the Nuxt documentation (https://nuxt.com/docs/guide/going-further/runtime-config#server-routes).

Here's the corrected version of your code:

import Jwt from "jsonwebtoken";

export default defineEventHandler(async (event) => {
  const body = await readBody(event);
  if (!body.token) {
    return new Response('No token', { status: 403 });
  }
  const config = useRuntimeConfig(event);
  try {
    Jwt.verify(body.token, config.secret);
    return true;
  }
  catch (e) {
    return false;
  }
})

Additionally, make sure that you've properly defined the secret in your nuxt.config.ts file under the runtimeConfig option. It should look something like this:

export default defineNuxtConfig({
  runtimeConfig: {
    secret: '', // This will be overridden by NUXT_SECRET environment variable
  },
})

Remember to set the corresponding environment variable (NUXT_SECRET in this case) when running your application.

If you're still experiencing issues after making these changes, it might be helpful to check if the secret is properly set in your runtime config by logging it (be cautious with logging sensitive information in production environments).

#

Nuxt documentation on Runtime Config (https://nuxt.com/docs/guide/going-further/runtime-config) provides more detailed information on how to properly use and set up runtime configuration in Nuxt 3.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@pseudo torrent Kapa.ai is still learning and improving, please let me know how I did by reacting below.