From a0ca50a718b06e876ab5158bd0c1ed2b4fca219b Mon Sep 17 00:00:00 2001 From: Sergey Karmanov Date: Sat, 16 May 2026 11:22:51 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=D1=80=D0=BE=D0=BB=D1=8C=20=D1=81=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D1=81=D1=8B=D0=B2=D0=B0=D0=BB=D0=B0=D1=81=D1=8C=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=B3?= =?UTF-8?q?=D1=80=D1=83=D0=B7=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/stores/auth.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/frontend/src/stores/auth.ts b/frontend/src/stores/auth.ts index 303d95f..98912a0 100644 --- a/frontend/src/stores/auth.ts +++ b/frontend/src/stores/auth.ts @@ -7,11 +7,20 @@ import type { AuthResponse } from '@/api/types' import type { User } from '@/types' const TOKEN_STORAGE_KEY = 'universe.accessToken' +const ACTIVE_ROLE_STORAGE_KEY = 'universe.activeRole' + +function restoreStoredActiveRole(mappedUser: User) { + const storedRole = localStorage.getItem(ACTIVE_ROLE_STORAGE_KEY) as User['activeRole'] | null + + if (!storedRole || !mappedUser.roles.includes(storedRole)) return mappedUser + + return { ...mappedUser, activeRole: storedRole } +} function applyAuthResponse(response: AuthResponse) { localStorage.setItem(TOKEN_STORAGE_KEY, response.accessToken) setApiAccessToken(response.accessToken) - return mapApiUser(response.user) + return restoreStoredActiveRole(mapApiUser(response.user)) } function getAuthReturnUrl() { @@ -48,13 +57,13 @@ export const useAuthStore = defineStore('auth', () => { const refreshed = await authApi.refresh() await hydrateFromResponse(refreshed) const me = await authApi.me() - user.value = mapApiUser(me) + user.value = restoreStoredActiveRole(mapApiUser(me)) return true } catch (refreshError) { if (accessToken.value) { try { const me = await authApi.me() - user.value = mapApiUser(me) + user.value = restoreStoredActiveRole(mapApiUser(me)) return true } catch { // Fall through to local cleanup below. @@ -100,7 +109,7 @@ export const useAuthStore = defineStore('auth', () => { localStorage.setItem(TOKEN_STORAGE_KEY, token) setApiAccessToken(token) const me = await authApi.me() - user.value = mapApiUser(me) + user.value = restoreStoredActiveRole(mapApiUser(me)) initialized.value = true return true } catch (err) { @@ -129,6 +138,7 @@ export const useAuthStore = defineStore('auth', () => { user.value = null accessToken.value = null localStorage.removeItem(TOKEN_STORAGE_KEY) + localStorage.removeItem(ACTIVE_ROLE_STORAGE_KEY) setApiAccessToken(null) } @@ -138,6 +148,7 @@ export const useAuthStore = defineStore('auth', () => { function setActiveRole(role: User['activeRole']) { if (!user.value || !user.value.roles.includes(role)) return false + localStorage.setItem(ACTIVE_ROLE_STORAGE_KEY, role) user.value = { ...user.value, activeRole: role } return true }