stack/examples/supabase/utils/actions.ts
Zai Shi 3d2be1f84c
Supabase docs & examples (#219)
* added supabase example

* removed unused files

* added jwt endpoint

* supabase server action

* removed unused

* updated dependencies

* reverted package changes

* fixed bugs

* added supabase docs

* updated docs

* updated pnpm lock
2024-09-01 02:32:07 +02:00

26 lines
573 B
TypeScript

'use server';
import { stackServerApp } from "@/stack";
import * as jose from "jose";
/*
This is a server action that returns a Supabase JWT with the Stack Auth user ID
*/
export const getSupabaseJwt = async () => {
const user = await stackServerApp.getUser();
if (!user) {
return null;
}
const token = await new jose.SignJWT({
sub: user.id,
role: "authenticated",
})
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setExpirationTime('1h')
.sign(new TextEncoder().encode(process.env.SUPABASE_JWT_SECRET));
return token;
};