Welcome to the Kinde community.

K
A
A
A
M

Getting access token outside a react component

what is the best way to get the access token outside a react compoment?

K
C
D
3 comments

To get the access token outside a React component, you can use the `getToken` method from the Kinde SDK. This method can be called in any JavaScript function, not just within React components. Here is an example of how you can do this: 1. *Import the necessary functions from the Kinde SDK:* ``` import { getToken } from '@kinde-oss/kinde-auth-react'; ``` 2. *Create a function to get the access token:* ``` async function fetchAccessToken() { try { const accessToken = await getToken(); console.log('Access Token:', accessToken); return accessToken; } catch (error) { console.error('Error fetching access token:', error); } } ``` 3. *Call this function wherever you need the access token:* ``` fetchAccessToken().then(token => { // Use the token for your API calls or other purposes }); ``` This approach allows you to fetch the access token outside of React components, making it versatile for various use cases such as API calls in utility functions or middleware.

This is not working, I'm getting this error:
Module '"@kinde-oss/kinde-auth-react"' has no exported member 'getToken'.

Hi, what backend service are you using?

Add a reply
Sign up and join the conversation on Slack
Join