fix(lint): resolve eslint warnings, type errors and clean unused code
This commit is contained in:
+13
-7
@@ -1,11 +1,16 @@
|
||||
import { SignJWT, jwtVerify } from 'jose';
|
||||
import { SignJWT, jwtVerify, type JWTPayload } from 'jose';
|
||||
import { cookies } from 'next/headers';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const secretKey = process.env.JWT_SECRET || 'super-secret-key-replace-me-in-production';
|
||||
const key = new TextEncoder().encode(secretKey);
|
||||
|
||||
export async function encrypt(payload: any) {
|
||||
export interface SessionPayload extends JWTPayload {
|
||||
userId?: string;
|
||||
expires?: Date;
|
||||
}
|
||||
|
||||
export async function encrypt(payload: JWTPayload) {
|
||||
return await new SignJWT(payload)
|
||||
.setProtectedHeader({ alg: 'HS256' })
|
||||
.setIssuedAt()
|
||||
@@ -13,13 +18,13 @@ export async function encrypt(payload: any) {
|
||||
.sign(key);
|
||||
}
|
||||
|
||||
export async function decrypt(input: string): Promise<any> {
|
||||
export async function decrypt(input: string): Promise<JWTPayload | null> {
|
||||
try {
|
||||
const { payload } = await jwtVerify(input, key, {
|
||||
algorithms: ['HS256'],
|
||||
});
|
||||
return payload;
|
||||
} catch (error) {
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -53,16 +58,17 @@ export async function updateSession(request: NextRequest) {
|
||||
const session = request.cookies.get('session')?.value;
|
||||
if (!session) return;
|
||||
|
||||
const parsed = await decrypt(session);
|
||||
const parsed = (await decrypt(session)) as SessionPayload | null;
|
||||
if (!parsed) return;
|
||||
|
||||
parsed.expires = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
const expires = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
parsed.expires = expires;
|
||||
const res = NextResponse.next();
|
||||
res.cookies.set({
|
||||
name: 'session',
|
||||
value: await encrypt(parsed),
|
||||
httpOnly: true,
|
||||
expires: parsed.expires,
|
||||
expires: expires,
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user