mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat(roles, visibility): refactor user roles to use flags and introduce visibility enum
This commit is contained in:
@@ -5,17 +5,19 @@ import { getServerToken } from '$lib/utils/auth';
|
||||
import type { ReportListResponse } from '$lib/types/report';
|
||||
import { extractError } from '$lib/utils/misc';
|
||||
|
||||
function decodeJwtRoles(token?: string | null): number[] {
|
||||
function decodeJwtRoles(token?: string | null): number {
|
||||
try {
|
||||
if (!token) return [];
|
||||
if (!token) return 0;
|
||||
const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
|
||||
const roles = payload?.role;
|
||||
if (Array.isArray(roles)) {
|
||||
return roles.map((r: string) => parseInt(r, 10)).filter((r: number) => !isNaN(r));
|
||||
if (typeof roles === 'number') return roles;
|
||||
if (typeof roles === 'string') {
|
||||
const parsed = parseInt(roles, 10);
|
||||
return isNaN(parsed) ? 0 : parsed;
|
||||
}
|
||||
return [];
|
||||
return 0;
|
||||
} catch {
|
||||
return [];
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +31,7 @@ export const load: PageServerLoad = async ({ fetch, cookies, url }) => {
|
||||
if (!token) throw error(401, 'Login required');
|
||||
|
||||
const roles = decodeJwtRoles(token);
|
||||
if (!roles.includes(1) && !roles.includes(255)) {
|
||||
if ((roles & 2) === 0 && (roles & 4) === 0) {
|
||||
throw error(403, 'You do not have permission to view reports.');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user