mirror of
https://github.com/hpd840321/craftlabs-authorization-sdk.git
synced 2026-06-09 10:00:30 +08:00
feat: add sidebar grouping, auth store persistence fix, idle timeout
Sidebar now groups menu items into Business/Operations/Analytics/System sections. Auth store restores roles/permissions from JWT on page reload. Added idleTimer utility for session timeout. Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -2,13 +2,47 @@ import { defineStore } from "pinia";
|
||||
import axios from "axios";
|
||||
|
||||
const TOKEN_KEY = "craftlabs_platform_token";
|
||||
const AUTH_KEY = "craftlabs_platform_auth";
|
||||
|
||||
function decodeJwtPayload(token) {
|
||||
try {
|
||||
const parts = token.split(".");
|
||||
if (parts.length !== 3) return null;
|
||||
const payload = parts[1];
|
||||
const decoded = atob(payload.replace(/-/g, "+").replace(/_/g, "/"));
|
||||
return JSON.parse(decoded);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function restoreAuth() {
|
||||
const token = localStorage.getItem(TOKEN_KEY);
|
||||
if (!token) return { token: "", displayName: "", roles: [], permissions: [] };
|
||||
|
||||
const saved = localStorage.getItem(AUTH_KEY);
|
||||
if (saved) {
|
||||
try {
|
||||
return { token, ...JSON.parse(saved) };
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
const claims = decodeJwtPayload(token);
|
||||
if (claims) {
|
||||
return {
|
||||
token,
|
||||
displayName: claims.displayName || claims.sub || "",
|
||||
roles: claims.roles || [],
|
||||
permissions: [],
|
||||
};
|
||||
}
|
||||
|
||||
return { token: "", displayName: "", roles: [], permissions: [] };
|
||||
}
|
||||
|
||||
export const useAuthStore = defineStore("auth", {
|
||||
state: () => ({
|
||||
token: localStorage.getItem(TOKEN_KEY) || "",
|
||||
displayName: "",
|
||||
roles: [],
|
||||
permissions: [],
|
||||
...restoreAuth(),
|
||||
}),
|
||||
getters: {
|
||||
hasAnyRole: (state) => {
|
||||
@@ -27,6 +61,9 @@ export const useAuthStore = defineStore("auth", {
|
||||
this.roles = data.roles || [];
|
||||
this.permissions = data.permissions || [];
|
||||
localStorage.setItem(TOKEN_KEY, this.token);
|
||||
localStorage.setItem(AUTH_KEY, JSON.stringify({
|
||||
displayName: this.displayName, roles: this.roles, permissions: this.permissions
|
||||
}));
|
||||
axios.defaults.headers.common.Authorization = `Bearer ${this.token}`;
|
||||
},
|
||||
logout() {
|
||||
@@ -35,7 +72,12 @@ export const useAuthStore = defineStore("auth", {
|
||||
this.roles = [];
|
||||
this.permissions = [];
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
localStorage.removeItem(AUTH_KEY);
|
||||
delete axios.defaults.headers.common.Authorization;
|
||||
if (window.__idleCleanup) {
|
||||
window.__idleCleanup()
|
||||
delete window.__idleCleanup
|
||||
}
|
||||
},
|
||||
restoreAxiosAuth() {
|
||||
if (this.token) {
|
||||
|
||||
Reference in New Issue
Block a user