add permission check
This commit is contained in:
@@ -4,11 +4,33 @@
|
||||
import { cookies } from 'next/headers';
|
||||
import { SignJWT, jwtVerify } from 'jose';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
/* const secretKey = process.env.SESSION_SECRET */
|
||||
const secretKey = 'AAA+sq1yKte/gMgUUu/B/OyXqr45/LMYplPVOlWc+uo='
|
||||
const encodedKey = new TextEncoder().encode(secretKey)
|
||||
const secretKey = process.env.SESSION_SECRET || 'AAA+sq1yKte/gMgUUu/B/OyXqr45/LMYplPVOlWc+uo=';
|
||||
const encodedKey = new TextEncoder().encode(secretKey);
|
||||
|
||||
export interface SessionPayload {
|
||||
token: string;
|
||||
email: string;
|
||||
employeId: number;
|
||||
permissions: {
|
||||
subscriptions: number;
|
||||
mailings: number;
|
||||
complaints: number;
|
||||
agreements: number;
|
||||
staff: number;
|
||||
content_moderation: number;
|
||||
users: number;
|
||||
kyc: number;
|
||||
money: number;
|
||||
claims: number;
|
||||
api: number;
|
||||
dash: number;
|
||||
tariffs: number;
|
||||
};
|
||||
expiresAt: Date | number;
|
||||
createdAt?: Date;
|
||||
}
|
||||
|
||||
export async function encrypt(payload: any) {
|
||||
return new SignJWT(payload)
|
||||
@@ -29,18 +51,20 @@ export async function decrypt(session: string | undefined = '') {
|
||||
}
|
||||
}
|
||||
|
||||
export async function createSession(token: string, email: string) {
|
||||
export async function createSession(token: string, email: string, employeId: number) {
|
||||
const expiresAt = new Date(Date.now() + 50 * 60 * 1000);
|
||||
const session = await encrypt({
|
||||
token,
|
||||
email,
|
||||
expiresAt
|
||||
employeId,
|
||||
expiresAt,
|
||||
createdAt: new Date()
|
||||
});
|
||||
const cookieStore = await cookies();
|
||||
|
||||
cookieStore.set('session_nc_ap', session, {
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
secure: process.env.NODE_ENV !== 'development',
|
||||
expires: expiresAt,
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
@@ -78,7 +102,7 @@ export async function updateSession(newToken: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getSessionData(type: 'token' | 'email'): Promise<string | null> {
|
||||
export async function getSessionData(type: 'token' | 'email' | 'employeId'): Promise<string | null> {
|
||||
const cookieStore = await cookies();
|
||||
const sessionCookie = cookieStore.get('session_nc_ap')?.value;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user