my dashboard is done
This commit is contained in:
parent
26bf86b9fe
commit
e8732bf436
@ -36,11 +36,18 @@ export class ZinoClient {
|
|||||||
this.onAuthError = fn;
|
this.onAuthError = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
setToken(token: string | null): void {
|
setToken(token: string | null, user?: User | null): void {
|
||||||
this.token = token;
|
this.token = token;
|
||||||
if (typeof window === 'undefined') return;
|
if (typeof window === 'undefined') return;
|
||||||
if (token) localStorage.setItem(TOKEN_KEY, token);
|
if (token) {
|
||||||
else localStorage.removeItem(TOKEN_KEY);
|
localStorage.setItem(TOKEN_KEY, token);
|
||||||
|
if (user) {
|
||||||
|
localStorage.setItem(TOKEN_KEY + '_user', JSON.stringify(user));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
localStorage.removeItem(TOKEN_KEY);
|
||||||
|
localStorage.removeItem(TOKEN_KEY + '_user');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getToken(): string | null {
|
getToken(): string | null {
|
||||||
@ -94,7 +101,7 @@ export class ZinoClient {
|
|||||||
password,
|
password,
|
||||||
...(orgId ? { org_id: Number(orgId) } : {}),
|
...(orgId ? { org_id: Number(orgId) } : {}),
|
||||||
});
|
});
|
||||||
this.setToken(res.token);
|
this.setToken(res.token, res.user);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,13 +112,27 @@ export class ZinoClient {
|
|||||||
/** Decode the persisted JWT into a User (no network). */
|
/** Decode the persisted JWT into a User (no network). */
|
||||||
currentUser(): User | null {
|
currentUser(): User | null {
|
||||||
if (!this.token) return null;
|
if (!this.token) return null;
|
||||||
|
|
||||||
|
// First, try to retrieve the user object stored during login
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
try {
|
||||||
|
const storedUser = localStorage.getItem(TOKEN_KEY + '_user');
|
||||||
|
if (storedUser) {
|
||||||
|
return JSON.parse(storedUser);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// ignore JSON parse errors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const p = JSON.parse(atob(this.token.split('.')[1]));
|
const p = JSON.parse(atob(this.token.split('.')[1]));
|
||||||
|
console.log('--- JWT PAYLOAD ---', p);
|
||||||
return {
|
return {
|
||||||
id: String(p.user_id ?? p.sub ?? ''),
|
id: String(p.user_id ?? p.sub ?? ''),
|
||||||
org_id: String(p.org_id ?? ''),
|
org_id: String(p.org_id ?? ''),
|
||||||
name: p.name ?? '',
|
name: p.name ?? p.given_name ?? '',
|
||||||
email: p.email ?? '',
|
email: p.email ?? p.preferred_username ?? p.upn ?? p.username ?? '',
|
||||||
roles: p.roles ?? [],
|
roles: p.roles ?? [],
|
||||||
groups: p.groups ?? [],
|
groups: p.groups ?? [],
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user