mirror of
https://github.com/chillpadclub/bedolaga-cabinet.git
synced 2026-07-30 02:23:47 +00:00
refactor(auth): extract applySession / clearSession from initialize()
initialize() had six inline set({ accessToken, refreshToken, user,
isAuthenticated, isLoading }) blocks — three success variants and three
failure variants. They were byte-identical aside from which access
token they passed through, so any future field addition would have
required updating six places in lockstep, with no compile-time
guarantee.
Extract two closure-scoped helpers:
applySession(token, refresh, user) — the success shape
clearSession() — the failure shape, with the
tokenStorage.clearTokens()
call bundled in
State writes are byte-for-byte identical to before; this is a
maintenance-only refactor. The accessToken param keeps the same
'string | null' typing the inline set() previously accepted, so behavior
on edge cases (null tokens reaching success branches via type-flow gaps)
is unchanged.
This commit is contained in:
@@ -159,6 +159,38 @@ export const useAuthStore = create<AuthState>()(
|
|||||||
return initState.promise;
|
return initState.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Three identical state shapes appeared inline 6 times — extract them
|
||||||
|
// here (closure-scoped to keep auth state internal). Behaviour is
|
||||||
|
// byte-for-byte identical to the previous inline blocks; the only
|
||||||
|
// change is that mistypes can't drift between branches.
|
||||||
|
// accessToken is typed `string | null` to match the previous inline
|
||||||
|
// set() shape and AuthState's field; in practice it's only called
|
||||||
|
// along the post-isTokenValid / post-refresh branches where the
|
||||||
|
// value is guaranteed non-null at runtime.
|
||||||
|
const applySession = (
|
||||||
|
accessToken: string | null,
|
||||||
|
refreshTokenValue: string,
|
||||||
|
user: User,
|
||||||
|
): void => {
|
||||||
|
set({
|
||||||
|
accessToken,
|
||||||
|
refreshToken: refreshTokenValue,
|
||||||
|
user,
|
||||||
|
isAuthenticated: true,
|
||||||
|
isLoading: false,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const clearSession = (): void => {
|
||||||
|
tokenStorage.clearTokens();
|
||||||
|
set({
|
||||||
|
accessToken: null,
|
||||||
|
refreshToken: null,
|
||||||
|
user: null,
|
||||||
|
isAuthenticated: false,
|
||||||
|
isLoading: false,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
initState.isInitializing = true;
|
initState.isInitializing = true;
|
||||||
initState.promise = (async () => {
|
initState.promise = (async () => {
|
||||||
try {
|
try {
|
||||||
@@ -184,22 +216,9 @@ export const useAuthStore = create<AuthState>()(
|
|||||||
if (newToken) {
|
if (newToken) {
|
||||||
const user = await authApi.getMe();
|
const user = await authApi.getMe();
|
||||||
await get().checkAdminStatus();
|
await get().checkAdminStatus();
|
||||||
set({
|
applySession(newToken, refreshToken, user);
|
||||||
accessToken: newToken,
|
|
||||||
refreshToken,
|
|
||||||
user,
|
|
||||||
isAuthenticated: true,
|
|
||||||
isLoading: false,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
tokenStorage.clearTokens();
|
clearSession();
|
||||||
set({
|
|
||||||
accessToken: null,
|
|
||||||
refreshToken: null,
|
|
||||||
user: null,
|
|
||||||
isAuthenticated: false,
|
|
||||||
isLoading: false,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -207,45 +226,19 @@ export const useAuthStore = create<AuthState>()(
|
|||||||
try {
|
try {
|
||||||
const user = await authApi.getMe();
|
const user = await authApi.getMe();
|
||||||
await get().checkAdminStatus();
|
await get().checkAdminStatus();
|
||||||
set({
|
applySession(accessToken, refreshToken, user);
|
||||||
accessToken,
|
|
||||||
refreshToken,
|
|
||||||
user,
|
|
||||||
isAuthenticated: true,
|
|
||||||
isLoading: false,
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
const newToken = await tokenRefreshManager.refreshAccessToken();
|
const newToken = await tokenRefreshManager.refreshAccessToken();
|
||||||
if (newToken) {
|
if (newToken) {
|
||||||
try {
|
try {
|
||||||
const user = await authApi.getMe();
|
const user = await authApi.getMe();
|
||||||
await get().checkAdminStatus();
|
await get().checkAdminStatus();
|
||||||
set({
|
applySession(newToken, refreshToken, user);
|
||||||
accessToken: newToken,
|
|
||||||
refreshToken,
|
|
||||||
user,
|
|
||||||
isAuthenticated: true,
|
|
||||||
isLoading: false,
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
tokenStorage.clearTokens();
|
clearSession();
|
||||||
set({
|
|
||||||
accessToken: null,
|
|
||||||
refreshToken: null,
|
|
||||||
user: null,
|
|
||||||
isAuthenticated: false,
|
|
||||||
isLoading: false,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tokenStorage.clearTokens();
|
clearSession();
|
||||||
set({
|
|
||||||
accessToken: null,
|
|
||||||
refreshToken: null,
|
|
||||||
user: null,
|
|
||||||
isAuthenticated: false,
|
|
||||||
isLoading: false,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user