update token life extension
This commit is contained in:
+24
-10
@@ -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');
|
||||
}
|
||||
Reference in New Issue
Block a user