K
Kinde5mo ago
Spleen

How can I get the users JWT token after logging when using the Nuxt module?

Hey, i'm using the Nuxt module and I am trying to get the JWT token after login so I can pass it through to my Cloudflare Workers API in the Authorization header. Is this possible? I can't find anything in the Nuxt Module documentation that says how to access the token.
3 Replies
skywalker-kiwi#02131
There is a composable called useKindeClient. There is an async method called getToken(). Use that to get your jwt. I realise that intellisense/documentation is severely lacking...
Spleen
SpleenOP5mo ago
Thanks! That worked for me. In case anyone else has a similar issue I implemented it by adding this to my app.vue:
const token = useState("token");
await callOnce(async () => {
const { getToken } = useKindeClient();
token.value = await getToken();
});
const token = useState("token");
await callOnce(async () => {
const { getToken } = useKindeClient();
token.value = await getToken();
});
This makes sure the token is also provided to the client for subsequent API calls. You can then use it in your API requests like so:
useFetch(path, {
onRequest: async ({ options }) => {
const token = useState("token");
options.headers = {
Authorization: `Bearer ${token.value}`,
};
},
});
useFetch(path, {
onRequest: async ({ options }) => {
const token = useState("token");
options.headers = {
Authorization: `Bearer ${token.value}`,
};
},
});
skywalker-kiwi#02131
[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables. When I added the above code to my app.vue to add token handling globally, this occurred. It appears as though my useKindeClient() composable calls are returning a null client, despite being logged in - bizarre.
Want results from more Discord servers?
Add your server