Sometimes Row Level Security is not enough and we want to do all logic server side, then we need a way for the server to get hold onto current user token.
Send JWT token in Authorization header
From client, we can get session from supabase auth, then send that as Bearer in Authorization header
Sometimes Row Level Security is not enough and we want to do all logic server side, then we need a way for the server to get hold onto current user token.
Send JWT token in Authorization header
From client, we can get
session
from supabase auth, then send that asBearer
inAuthorization
headerDecode token
From the nextjs API Route, we can handle all auth logic in middleware. We will use jwt-decode to decode token
src/middleware.ts
The decoded object looks like this, where
sub
, meaning subject, is usually the user idWith the user id, we can query Supabase Postgres database to check if this user is valid or not
Validate token
To validate json token, we can use the JWT Secret from Supabase to verify, using
jsonwebtoken
packageGet user
In the server, we get initiate
createClient
with service role key, and callauth.getUser(jwt)
to get userRead more