This commit is contained in:
2026-01-24 19:31:14 -05:00
parent e6d5b40acd
commit d7c91ac6c5
6 changed files with 198 additions and 198 deletions

39
src/server/nessa-auth.ts Normal file
View File

@@ -0,0 +1,39 @@
import { SignJWT, jwtVerify } from "jose";
import { env } from "~/env/server";
const NESSA_JWT_EXPIRY = "30d";
export type NessaAuthPayload = {
sub: string;
exp?: number;
iat?: number;
};
export async function verifyNessaToken(
token: string
): Promise<NessaAuthPayload> {
const secret = new TextEncoder().encode(env.NESSA_JWT_SECRET);
const { payload } = await jwtVerify(token, secret, {
algorithms: ["HS256"]
});
if (!payload.sub) {
throw new Error("Missing subject in Nessa JWT");
}
return {
sub: payload.sub as string,
exp: payload.exp as number | undefined,
iat: payload.iat as number | undefined
};
}
export async function signNessaToken(userId: string): Promise<string> {
const secret = new TextEncoder().encode(env.NESSA_JWT_SECRET);
return new SignJWT({})
.setProtectedHeader({ alg: "HS256" })
.setSubject(userId)
.setIssuedAt()
.setExpirationTime(NESSA_JWT_EXPIRY)
.sign(secret);
}