How to get the token in Nuxt?
In the JS SDK docs I can see
const token = await kinde.getToken();
However I don't see this example in the Nuxt documentation, how can I get the token in Nuxt, to call my backend API? (node)5 Replies
Hey @Miguel works only on
server
side and ssr
https://github.com/nuxt-modules/kinde/blob/main/src/runtime/server/utils/client.ts
you should write your own server/api/token.get.ts
endpoint in your nuxt with the client.GitHub
kinde/src/runtime/server/utils/client.ts at main · nuxt-modules/kinde
Kinde integration for Nuxt. Contribute to nuxt-modules/kinde development by creating an account on GitHub.
I'm not sure if I follow, do you have an example of how I can set this up?
Hey @Miguel,
In the Nuxt module documentation, the process of obtaining a token for API calls isn't explicitly outlined like in the JS SDK documentation. However, you can achieve this by using the Kinde authentication middleware and then accessing the token from the user's session or context.
Here’s a general approach to access the token in a Nuxt.js application:
1. Ensure that your Nuxt.js application is properly set up with the Kinde module.
2. Use the Kinde middleware to handle authentication. This middleware will automatically handle the tokens.
3. Once a user is authenticated, the token should be accessible from the user's session or through a specific Kinde context provided by the middleware.
For example, if you are using the
@nuxtjs/kinde
module, you might have access to the token through the session object in your Nuxt.js context. You can then use this token to make authenticated requests to your backend API.
Please let me know if you need further assistance.Is there any reason why the token wouldn’t be available on the front end? This isn’t a high secret token as you should always verify on the backend anyway.
Just wondering what the thought process was here.
Hey @tammycore,
Kinde supports different types of applications, including front-end/client-side apps and back-end/server-side apps . The Nuxt module is categorized as a back-end SDK , which might explain why the token handling is not explicitly described for front-end use.
If you explain more about your setup (including language and frameworks used) and how you want your authentication setup (e.g. have authentication logic and pass token to backend), I can help you further.
I would also suggest having a read of the following doc: https://docs.kinde.com/build/applications/authenticating-spa/?r=search#_top
Kinde docs
Authenticating single-page apps (SPAs) with Kinde
Our developer tools provide everything you need to get started with Kinde.