update token life extension

This commit is contained in:
smanylov
2026-01-20 16:53:11 +07:00
parent 2b7ba7255a
commit 7d7213b457
10 changed files with 72 additions and 54 deletions
+24 -10
View File
@@ -3,6 +3,7 @@
/* import 'server-only'; */
import { cookies } from 'next/headers'
import { SignJWT, jwtVerify } from 'jose'
import { redirect } from 'next/navigation';
/* const secretKey = process.env.SESSION_SECRET */
const secretKey = 'AAA+sq1yKte/gMgUUu/B/OyXqr45/LMYplPVOlWc+uo='
@@ -28,7 +29,7 @@ export async function decrypt(session: string | undefined = '') {
}
export async function createSession(token: string, email: string) {
const expiresAt = new Date(Date.now() + 60 * 60 * 1000);
const expiresAt = new Date(Date.now() + 50 * 60 * 1000);
const session = await encrypt({
token,
email,
@@ -45,21 +46,32 @@ export async function createSession(token: string, email: string) {
});
}
export async function updateSession() {
const session = (await cookies()).get('session')?.value
const payload = await decrypt(session)
export async function updateSession(newToken: string) {
const cookieStore = await cookies();
const session = cookieStore.get('session')?.value;
if (!session || !payload) {
return null
if (!session) {
return null;
}
const expiresAt = new Date(Date.now() + 60 * 60 * 1000);
const payload = await decrypt(session);
const cookieStore = await cookies()
cookieStore.set('session', session, {
if (!payload) {
return null;
}
const updatedPayload = {
...payload,
token: newToken,
expiresAt: new Date(Date.now() + 50 * 60 * 1000)
};
const updatedSession = await encrypt(updatedPayload);
cookieStore.set('session', updatedSession, {
httpOnly: true,
secure: false,
expires: expiresAt,
expires: updatedPayload.expiresAt,
sameSite: 'lax',
path: '/',
});
@@ -84,6 +96,8 @@ export async function getSessionData(type: 'token' | 'email'): Promise<string |
export async function deleteSession() {
console.log('delete session')
const cookieStore = await cookies()
cookieStore.delete('session');
redirect('/login');
}