add permission check
This commit is contained in:
@@ -18,8 +18,8 @@ export async function authorization(
|
||||
|
||||
/* для теста */
|
||||
if (email === 'test' && password === 'test') {
|
||||
await createSession('1111', 'test');
|
||||
redirect('/pages/');
|
||||
/* await createSession('1111', 'test', 0); */
|
||||
/* redirect('/pages/'); */
|
||||
}
|
||||
/* для теста */
|
||||
|
||||
@@ -70,7 +70,7 @@ export async function authorization(
|
||||
if (response.ok) {
|
||||
let parsed = await response.json();
|
||||
if (parsed.message_code === 0) {
|
||||
await createSession(parsed.message_body.token, email);
|
||||
await createSession(parsed.message_body.token, email, parsed.message_body.admin.id);
|
||||
} else {
|
||||
throw (`${parsed.message_code}`);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ export async function fetchStuffList(page?: number, size?: number, sortBy?: stri
|
||||
msg_id: 100,
|
||||
token: token,
|
||||
message_body: {
|
||||
/* token: token, */
|
||||
action: 'get_all',
|
||||
page: page,
|
||||
size: size,
|
||||
@@ -169,6 +168,44 @@ export async function updateStuffPermissions(id: number, permissions: Permission
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const parsed = await response.json();
|
||||
|
||||
if (parsed.message_code === 0) {
|
||||
return parsed.message_body;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
throw new Error(`${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchEmployeInfo() {
|
||||
const token = await getSessionData('token');
|
||||
const employeId = await getSessionData('employeId');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 100,
|
||||
token: token,
|
||||
message_body: {
|
||||
action: 'get_by_id',
|
||||
admin_id: employeId
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const parsed = await response.json();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user