mirror of
https://github.com/hpd840321/craftlabs-authorization-sdk.git
synced 2026-06-09 10:00:30 +08:00
feat(web): I1 shell and I2 customer/project UI
Vue 3 + Element Plus layout with JWT login, RBAC routes, axios 401 handling with token restore, and Customers/Projects views wired to platform APIs. Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { defineStore } from "pinia";
|
||||
import axios from "axios";
|
||||
|
||||
const TOKEN_KEY = "craftlabs_platform_token";
|
||||
|
||||
export const useAuthStore = defineStore("auth", {
|
||||
state: () => ({
|
||||
token: localStorage.getItem(TOKEN_KEY) || "",
|
||||
displayName: "",
|
||||
roles: [],
|
||||
}),
|
||||
actions: {
|
||||
async login(username, password) {
|
||||
const { data } = await axios.post("/api/v1/auth/login", { username, password });
|
||||
this.token = data.token;
|
||||
this.displayName = data.displayName || username;
|
||||
this.roles = data.roles || [];
|
||||
localStorage.setItem(TOKEN_KEY, this.token);
|
||||
axios.defaults.headers.common.Authorization = `Bearer ${this.token}`;
|
||||
},
|
||||
logout() {
|
||||
this.token = "";
|
||||
this.displayName = "";
|
||||
this.roles = [];
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
delete axios.defaults.headers.common.Authorization;
|
||||
},
|
||||
restoreAxiosAuth() {
|
||||
if (this.token) {
|
||||
axios.defaults.headers.common.Authorization = `Bearer ${this.token}`;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user