A function that retrieves the current user's session token or a custom JWT template. Returns a promise that resolves to the token. See the reference docJavaScript Icon.
The following example demonstrates how to use the useAuth() composable to access the current auth state, like whether the user is signed in or not. It also includes a basic example for using the getToken() method to retrieve a session token for fetching data from an external resource.
app/pages/protected-page.vue
<scriptsetup>const { getToken,isLoaded,isSignedIn,sessionId,userId } =useAuth()constfetchProtectedData=async () => {// Use `getToken()` to get the current user's session tokenconsttoken=awaitgetToken.value()// Use the token to fetch data from an external APIconstresponse=awaitfetch('https://api.example.com/data', { headers: { Authorization:`Bearer ${token}`, }, })returnresponse.json()}</script><template><!-- Use `isLoaded` to check if Clerk is loaded --> <divv-if="!isLoaded">Loading...</div><!-- Use `isSignedIn` to check if the user is signed in --> <divv-else-if="!isSignedIn">Sign in to view this page</div><!-- Use `userId` to access the current user's ID and `sessionId` to access the current session ID --> <divv-else>Hello, {{ userId }}! Your current active session is {{ sessionId }}.</div></template>