mirror of
https://github.com/shuaiplus/nodewarden.git
synced 2026-06-20 21:00:41 +00:00
feat: refactor vault component helpers to use dedicated functions for options retrieval
This commit is contained in:
@@ -134,7 +134,28 @@ export function loadProfileSnapshot(email?: string | null): Profile | null {
|
||||
|
||||
export function saveProfileSnapshot(profile: Profile | null): void {
|
||||
if (!profile) return;
|
||||
localStorage.setItem(PROFILE_SNAPSHOT_KEY, JSON.stringify(stripProfileSecrets(profile)));
|
||||
const nextSnapshot = stripProfileSecrets(profile);
|
||||
try {
|
||||
const rawExisting = localStorage.getItem(PROFILE_SNAPSHOT_KEY);
|
||||
if (rawExisting) {
|
||||
const existing = stripProfileSecrets(JSON.parse(rawExisting) as Profile);
|
||||
if (
|
||||
existing
|
||||
&& existing.email === nextSnapshot?.email
|
||||
&& existing.role === 'admin'
|
||||
&& nextSnapshot?.role !== 'admin'
|
||||
) {
|
||||
localStorage.setItem(PROFILE_SNAPSHOT_KEY, JSON.stringify({
|
||||
...nextSnapshot,
|
||||
role: 'admin',
|
||||
}));
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Fall back to writing the normalized snapshot below.
|
||||
}
|
||||
localStorage.setItem(PROFILE_SNAPSHOT_KEY, JSON.stringify(nextSnapshot));
|
||||
}
|
||||
|
||||
export function clearProfileSnapshot(): void {
|
||||
|
||||
@@ -144,7 +144,7 @@ function decodeAccessTokenClaims(accessToken: string): AccessTokenClaims {
|
||||
}
|
||||
}
|
||||
|
||||
function buildTransientProfile(token: TokenSuccess, email: string): Profile {
|
||||
function buildTransientProfile(token: TokenSuccess, email: string, fallbackProfile: Profile | null = null): Profile {
|
||||
const claims = decodeAccessTokenClaims(token.access_token);
|
||||
const normalizedEmail = String(claims.email || email || '').trim().toLowerCase();
|
||||
const accountKeys = token.accountKeys ?? token.AccountKeys ?? null;
|
||||
@@ -154,9 +154,11 @@ function buildTransientProfile(token: TokenSuccess, email: string): Profile {
|
||||
name: String(claims.name || normalizedEmail || ''),
|
||||
key: String(token.Key || ''),
|
||||
privateKey: token.PrivateKey ?? null,
|
||||
role: 'user',
|
||||
role: fallbackProfile?.role === 'admin' ? 'admin' : 'user',
|
||||
premium: !!claims.premium,
|
||||
accountKeys,
|
||||
masterPasswordHint: fallbackProfile?.masterPasswordHint ?? null,
|
||||
publicKey: fallbackProfile?.publicKey ?? null,
|
||||
object: 'profile',
|
||||
};
|
||||
}
|
||||
@@ -256,6 +258,7 @@ export async function completeLogin(
|
||||
masterKey: Uint8Array
|
||||
): Promise<CompletedLogin> {
|
||||
const normalizedEmail = email.trim().toLowerCase();
|
||||
const fallbackProfile = loadProfileSnapshot(normalizedEmail);
|
||||
const baseSession: SessionState = {
|
||||
accessToken: token.access_token,
|
||||
refreshToken: token.refresh_token,
|
||||
@@ -266,7 +269,7 @@ export async function completeLogin(
|
||||
() => baseSession,
|
||||
() => {}
|
||||
);
|
||||
const profile = buildTransientProfile(token, normalizedEmail);
|
||||
const profile = buildTransientProfile(token, normalizedEmail, fallbackProfile);
|
||||
if (!profile.key) {
|
||||
throw new Error('Missing profile key');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user