Is it possible to get clerk token without using server side function or client hook? #2203
Replies: 4 comments 5 replies
-
the only solution I have for now is this, if you have something better let me know import { auth } from '@clerk/nextjs'
import Clerk from '@clerk/clerk-js'
export async function getAuthToken() {
const isServer = typeof window === 'undefined'
if (isServer) {
const { getToken } = auth()
return await getToken()
}
const clerk = new Clerk(process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY!)
await clerk.load()
return await clerk.session?.getToken()
} and import { getAuthToken } from './get-auth-token'
export async function authFetch(
url: string,
options?: RequestInit,
): Promise<Response> {
const token = await getAuthToken()
if (token) {
options = {
...(options || {}),
headers: {
...(options?.headers || {}),
Authorization: `Bearer ${token}`,
},
}
}
return await fetch(url, options)
} |
Beta Was this translation helpful? Give feedback.
-
@leonardo-motta-deel did you find any better solution to this . |
Beta Was this translation helpful? Give feedback.
-
I wonder... could I get the token in the const response = NextResponse.next()
const token = await getToken({ template: 'dashboard' }) // only uses the template for server-side auth (leaves the client-side with the default expiration time)
if (!token || !userId) {
protect()
}
response.headers.set('token', token as string) and then pass it through headers to pages which have no issues with export async function getAuthToken() {
if (typeof window == 'undefined') {
const { headers } = await import('next/headers') // next/headers does not care about `typeof window !=`
const token = headers().get('token')
if (!token) {
return undefined
}
return token
}
const token = window.Clerk.session?.getToken()
if (!token) {
return undefined
}
return token
} This could potentially cause issues with expired tokens, but with a higher expiration time, it may be fine for my use case (requests won't take longer than 2-5 minutes, which I could accommodate by increasing the expiration time using JWT templates). |
Beta Was this translation helpful? Give feedback.
-
I have a case where I use a nextjs app, but I do not fetch data from next api, I have an external server protected by clerk, and I have a page that is 'use client', but the data is prefetched by a server component, and I wanted an easier way to get clerk token for this use case.
example case:
I have getUsers function that will be used to dehydrate data from server component and also fetch data from client side
now I have my server component that use this function to pre fetch data
and now I have my client component to render client page
since it is called from both client side and server side, I can't use hooks or the server functions provided by clerk, I also tried
@clerk/clerk-js
but no success, the only approach I found so far was to create a higher order function to get token from client or server side and pass as param togetUsers
function, but honestly there most be something better...Beta Was this translation helpful? Give feedback.
All reactions