Refactor app architecture and clean local artifacts
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import { AUTH_TOKEN_KEY, api } from '../lib/api'
|
||||
import type { AuthSession, LoginPayload } from '../types/awards'
|
||||
import { ApiRequestError } from '../lib/http'
|
||||
import type { AuthSession, DemoLoginPayload, LoginPayload } from '../types/awards'
|
||||
|
||||
const adminAccessRoles = new Set(['content_admin', 'admin', 'owner'])
|
||||
const adminWorkspaceRoles = new Set(['admin', 'owner'])
|
||||
|
||||
function readStoredToken() {
|
||||
if (typeof window === 'undefined') return null
|
||||
@@ -26,7 +30,12 @@ export const useAuthStore = defineStore('auth', {
|
||||
}),
|
||||
getters: {
|
||||
isLoggedIn: (state) => Boolean(state.session),
|
||||
isAdmin: (state) => state.session?.role === 'admin',
|
||||
isAdmin: (state) => adminAccessRoles.has(state.session?.role ?? ''),
|
||||
canAccessAdmin: (state) => adminAccessRoles.has(state.session?.role ?? ''),
|
||||
canManageContent: (state) => adminAccessRoles.has(state.session?.role ?? ''),
|
||||
canManageAdminWorkspace: (state) => adminWorkspaceRoles.has(state.session?.role ?? ''),
|
||||
canManageOperationalSettings: (state) => state.session?.role === 'owner',
|
||||
isOwner: (state) => state.session?.role === 'owner',
|
||||
},
|
||||
actions: {
|
||||
async hydrate() {
|
||||
@@ -37,9 +46,11 @@ export const useAuthStore = defineStore('auth', {
|
||||
|
||||
try {
|
||||
this.session = await api.getSession()
|
||||
} catch {
|
||||
} catch (error) {
|
||||
this.session = null
|
||||
writeStoredToken(null)
|
||||
if (error instanceof ApiRequestError && (error.status === 401 || error.status === 403)) {
|
||||
writeStoredToken(null)
|
||||
}
|
||||
} finally {
|
||||
this.hydrated = true
|
||||
}
|
||||
@@ -54,6 +65,16 @@ export const useAuthStore = defineStore('auth', {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
async demoLogin(payload: DemoLoginPayload) {
|
||||
this.loading = true
|
||||
try {
|
||||
const session = await api.demoLogin(payload)
|
||||
this.session = session
|
||||
writeStoredToken(session.sessionToken)
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
async logout() {
|
||||
this.loading = true
|
||||
try {
|
||||
@@ -64,5 +85,18 @@ export const useAuthStore = defineStore('auth', {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
async deleteMyData() {
|
||||
this.loading = true
|
||||
try {
|
||||
const result = await api.deleteMyData()
|
||||
this.session = null
|
||||
writeStoredToken(null)
|
||||
return result
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
export type AuthStore = ReturnType<typeof useAuthStore>
|
||||
|
||||
Reference in New Issue
Block a user