fix: роль сбрасывалась при перезагрузке
Frontend CI / build-and-check (push) Failing after 5m12s
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Successful in 13s
🚀 Create and publish a Docker image / Build & publish backend image (push) Successful in 16s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Successful in 22s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Successful in 11s
Frontend CI / build-and-check (push) Failing after 5m12s
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Successful in 13s
🚀 Create and publish a Docker image / Build & publish backend image (push) Successful in 16s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Successful in 22s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Successful in 11s
This commit is contained in:
@@ -7,11 +7,20 @@ import type { AuthResponse } from '@/api/types'
|
|||||||
import type { User } from '@/types'
|
import type { User } from '@/types'
|
||||||
|
|
||||||
const TOKEN_STORAGE_KEY = 'universe.accessToken'
|
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) {
|
function applyAuthResponse(response: AuthResponse) {
|
||||||
localStorage.setItem(TOKEN_STORAGE_KEY, response.accessToken)
|
localStorage.setItem(TOKEN_STORAGE_KEY, response.accessToken)
|
||||||
setApiAccessToken(response.accessToken)
|
setApiAccessToken(response.accessToken)
|
||||||
return mapApiUser(response.user)
|
return restoreStoredActiveRole(mapApiUser(response.user))
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAuthReturnUrl() {
|
function getAuthReturnUrl() {
|
||||||
@@ -48,13 +57,13 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
const refreshed = await authApi.refresh()
|
const refreshed = await authApi.refresh()
|
||||||
await hydrateFromResponse(refreshed)
|
await hydrateFromResponse(refreshed)
|
||||||
const me = await authApi.me()
|
const me = await authApi.me()
|
||||||
user.value = mapApiUser(me)
|
user.value = restoreStoredActiveRole(mapApiUser(me))
|
||||||
return true
|
return true
|
||||||
} catch (refreshError) {
|
} catch (refreshError) {
|
||||||
if (accessToken.value) {
|
if (accessToken.value) {
|
||||||
try {
|
try {
|
||||||
const me = await authApi.me()
|
const me = await authApi.me()
|
||||||
user.value = mapApiUser(me)
|
user.value = restoreStoredActiveRole(mapApiUser(me))
|
||||||
return true
|
return true
|
||||||
} catch {
|
} catch {
|
||||||
// Fall through to local cleanup below.
|
// Fall through to local cleanup below.
|
||||||
@@ -100,7 +109,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
localStorage.setItem(TOKEN_STORAGE_KEY, token)
|
localStorage.setItem(TOKEN_STORAGE_KEY, token)
|
||||||
setApiAccessToken(token)
|
setApiAccessToken(token)
|
||||||
const me = await authApi.me()
|
const me = await authApi.me()
|
||||||
user.value = mapApiUser(me)
|
user.value = restoreStoredActiveRole(mapApiUser(me))
|
||||||
initialized.value = true
|
initialized.value = true
|
||||||
return true
|
return true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -129,6 +138,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
user.value = null
|
user.value = null
|
||||||
accessToken.value = null
|
accessToken.value = null
|
||||||
localStorage.removeItem(TOKEN_STORAGE_KEY)
|
localStorage.removeItem(TOKEN_STORAGE_KEY)
|
||||||
|
localStorage.removeItem(ACTIVE_ROLE_STORAGE_KEY)
|
||||||
setApiAccessToken(null)
|
setApiAccessToken(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +148,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
|
|
||||||
function setActiveRole(role: User['activeRole']) {
|
function setActiveRole(role: User['activeRole']) {
|
||||||
if (!user.value || !user.value.roles.includes(role)) return false
|
if (!user.value || !user.value.roles.includes(role)) return false
|
||||||
|
localStorage.setItem(ACTIVE_ROLE_STORAGE_KEY, role)
|
||||||
user.value = { ...user.value, activeRole: role }
|
user.value = { ...user.value, activeRole: role }
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user