getUser() vs getClaims()

Hey guys, Previously when we were using getUser(), we would also cache it like:
export const user = cache(async () => {
const supabase = await supabaseServerClient()
const { data } = await supabase.auth.getUser()
return data.user?.role
})
export const user = cache(async () => {
const supabase = await supabaseServerClient()
const { data } = await supabase.auth.getUser()
return data.user?.role
})
Is this still relevant when using getClaims()?
export const claims = cache(async () => {
const supabase = await supabaseServerClient()
const { data } = await supabase.auth.getClaims()

if (!data) return null

const claims = data.claims
return claims
})
export const claims = cache(async () => {
const supabase = await supabaseServerClient()
const { data } = await supabase.auth.getClaims()

if (!data) return null

const claims = data.claims
return claims
})
Thanks for your help!
9 Replies
ihm40
ihm403w ago
I think the using getClaims() makes a request to /auth/v1/.well-known/jwks.json endpoint for your project to get the public key to decrypt so caching probably still is relevant to avoid having to make that request
Rob Schilder
Rob SchilderOP3w ago
thank you a lot for the answer @ihm40 ! Do you perhaps also know why getClaims() does not return a type with email etc? created an issue here too : https://github.com/supabase/auth-js/issues/1128
GitHub
Provide TypeScript types for documented JWT claims fields (email, p...
Refactor Request Describe the refactor The getClaims() method returns JwtPayload with type RequiredClaims & { [key: string]: any }, which doesn't provide TypeScript types for common Supabas...
ihm40
ihm403w ago
hmm it should be a return value. What does your getClaims currently return? Obviously annonnymise as needed
garyaustin
garyaustin3w ago
getClaims still generate a network request but it is usually to the CDN for a number of requests versus all the way to the SB server every time. Note though the claims only change when the JWT refreshes versus getUser will get the latest user data from auth.users. This is critical for things like user blocked/deleted or any role info set in metadata that must change immediately.
Rob Schilder
Rob SchilderOP3w ago
So should we also still cache it or is that not necessary?
garyaustin
garyaustin3w ago
Up to you.
Rob Schilder
Rob SchilderOP3w ago
Would the Supabase team recommend it to us, I mean. I don't think it is relevant for performance right?
garyaustin
garyaustin3w ago
It would be a bit faster. GetClaims still makes a network call with 10’s of msec of latency so really depends on how much you use it.
Rob Schilder
Rob SchilderOP3w ago
Yeah. 10 msec is not really worth caching for. But I also wonder if the caching leads to issues

Did you find this page helpful?