| import { type JWTPayload, jwtVerify } from "jose"; | |
| export type Session = { | |
| user: { | |
| id: string; | |
| email?: string; | |
| full_name?: string; | |
| }; | |
| teamId?: string; | |
| }; | |
| type SupabaseJWTPayload = JWTPayload & { | |
| user_metadata?: { | |
| email?: string; | |
| full_name?: string; | |
| [key: string]: string | undefined; | |
| }; | |
| }; | |
| export async function verifyAccessToken( | |
| accessToken?: string, | |
| ): Promise<Session | null> { | |
| if (!accessToken) return null; | |
| try { | |
| const { payload } = await jwtVerify( | |
| accessToken, | |
| new TextEncoder().encode(process.env.SUPABASE_JWT_SECRET), | |
| ); | |
| const supabasePayload = payload as SupabaseJWTPayload; | |
| return { | |
| user: { | |
| id: supabasePayload.sub!, | |
| email: supabasePayload.user_metadata?.email, | |
| full_name: supabasePayload.user_metadata?.full_name, | |
| }, | |
| }; | |
| } catch (_error) { | |
| return null; | |
| } | |
| } | |