update token life extension
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
'use server'
|
||||
import { getSessionData } from '@/app/actions/session';
|
||||
import { getSessionData, deleteSession } from '@/app/actions/session';
|
||||
import { testUserData, API_BASE_URL } from '@/app/actions/definitions';
|
||||
import {tokenLifeExtension} from '@/app/actions/auth';
|
||||
import { tokenLifeExtension } from '@/app/actions/auth';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export async function getUserData() {
|
||||
const userEmail = await getSessionData('email');
|
||||
@@ -55,6 +56,8 @@ export async function getUserFilesInfo() {
|
||||
const parsed = await response.json();
|
||||
if (parsed.message_code === 0) {
|
||||
return parsed.message_body;
|
||||
} else if (parsed.message_code === 2) {
|
||||
await deleteSession();
|
||||
} else {
|
||||
throw parsed.message_code;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ export async function logout() {
|
||||
throw error;
|
||||
} finally {
|
||||
await deleteSession();
|
||||
redirect('/login');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,11 +315,12 @@ export async function tokenLifeExtension() {
|
||||
|
||||
if (response.ok) {
|
||||
const parsed = await response.json();
|
||||
await updateSession();
|
||||
if (parsed.message_code === 0) {
|
||||
await updateSession(parsed.message_body.token);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
await deleteSession();
|
||||
redirect('/login');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
+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