EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
auth.js
Go to the documentation of this file.
1/**
2 * Parse JWT token from localStorage
3 * @returns {Object|null} Decoded JWT payload or null if invalid
4 */
5export function parseJwt(token) {
6 if (!token) return null;
7 try {
8 const payload = token.split('.')[1];
9 return JSON.parse(atob(payload.replace(/-/g, '+').replace(/_/g, '/')));
10 } catch {
11 return null;
12 }
13}
14
15/**
16 * Get current user's role from JWT
17 * @returns {string} User role (admin, staff, msp, root) or empty string
18 */
19export function getUserRole() {
20 const token = localStorage.getItem('token');
21 const payload = parseJwt(token);
22 return payload?.role || '';
23}
24
25/**
26 * Check if current user is admin (admin, msp, or root)
27 * @returns {boolean} True if user has admin privileges
28 */
29export function isAdmin() {
30 const role = getUserRole();
31 return role === 'admin' || role === 'msp' || role === 'root';
32}
33
34/**
35 * Check if current user is staff
36 * @returns {boolean} True if user is staff
37 */
38export function isStaff() {
39 const role = getUserRole();
40 return role === 'staff';
41}