Skip to content

Commit

Permalink
handle decrypt function error
Browse files Browse the repository at this point in the history
  • Loading branch information
delbaoliveira committed Mar 19, 2024
1 parent a943521 commit 310dbed
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions app/auth/02-stateless-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ import type { SessionPayload } from '@/app/auth/definitions';
const secretKey = 'yourSecretKey';
const key = new TextEncoder().encode(secretKey);

async function encrypt(payload: SessionPayload) {
export async function encrypt(payload: SessionPayload) {
return new SignJWT(payload)
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('1hr')
.sign(key);
}

async function decrypt(userId: string) {
const { payload } = await jwtVerify(userId, key, {
algorithms: ['HS256'],
});
return payload;
export async function decrypt(session: string | undefined = '') {
try {
const { payload } = await jwtVerify(session, key, {
algorithms: ['HS256'],
});
return payload;
} catch (error) {
console.log('Failed to verify session');
return null;
}
}

export async function createSession(userId: string) {
Expand Down

0 comments on commit 310dbed

Please sign in to comment.