加固后台管理安全
This commit is contained in:
@@ -6,17 +6,12 @@ import {
|
||||
type AdminRole,
|
||||
type AdminUser,
|
||||
} from '@/features/admin/api/adminAuth'
|
||||
import {
|
||||
clearAuthStorage,
|
||||
getAccessToken,
|
||||
getRefreshToken,
|
||||
setAuthTokens,
|
||||
} from '@/shared/utils/authStorage'
|
||||
import { clearAuthStorage } from '@/shared/utils/authStorage'
|
||||
|
||||
export const useAdminSessionStore = defineStore('adminSession', {
|
||||
state: () => ({
|
||||
token: getAccessToken('admin'),
|
||||
refreshToken: getRefreshToken('admin'),
|
||||
token: '',
|
||||
refreshToken: '',
|
||||
adminId: Number(localStorage.getItem('admin_id') || 0),
|
||||
username: localStorage.getItem('admin_username') || '',
|
||||
nickname: '',
|
||||
@@ -24,10 +19,12 @@ export const useAdminSessionStore = defineStore('adminSession', {
|
||||
| 'online'
|
||||
| 'offline'
|
||||
| 'busy',
|
||||
passwordMustChange: localStorage.getItem('admin_password_must_change') === 'true',
|
||||
roles: [] as AdminRole[],
|
||||
permissions: [] as string[],
|
||||
}),
|
||||
getters: {
|
||||
hasSessionHint: state => state.adminId > 0 || state.username !== '',
|
||||
hasPermission: state => {
|
||||
return (code: string) => state.permissions.includes(code) || state.permissions.includes('*')
|
||||
},
|
||||
@@ -41,7 +38,7 @@ export const useAdminSessionStore = defineStore('adminSession', {
|
||||
actions: {
|
||||
async login(username: string, password: string, captchaId: string, captchaCode: string) {
|
||||
const result = await loginAdmin(username, password, captchaId, captchaCode)
|
||||
this.applySession(result.admin, result.tokens.access_token, result.tokens.refresh_token)
|
||||
this.applySession(result.admin)
|
||||
return result
|
||||
},
|
||||
async loadMe() {
|
||||
@@ -56,28 +53,27 @@ export const useAdminSessionStore = defineStore('adminSession', {
|
||||
this.username = ''
|
||||
this.nickname = ''
|
||||
this.supportStatus = 'offline'
|
||||
this.passwordMustChange = false
|
||||
this.roles = []
|
||||
this.permissions = []
|
||||
clearAuthStorage('admin')
|
||||
localStorage.removeItem('admin_support_status')
|
||||
localStorage.removeItem('admin_password_must_change')
|
||||
},
|
||||
syncFromStorage() {
|
||||
this.token = getAccessToken('admin')
|
||||
this.refreshToken = getRefreshToken('admin')
|
||||
this.token = ''
|
||||
this.refreshToken = ''
|
||||
this.adminId = Number(localStorage.getItem('admin_id') || 0)
|
||||
this.username = localStorage.getItem('admin_username') || ''
|
||||
this.supportStatus = (localStorage.getItem('admin_support_status') || 'offline') as
|
||||
| 'online'
|
||||
| 'offline'
|
||||
| 'busy'
|
||||
this.passwordMustChange = localStorage.getItem('admin_password_must_change') === 'true'
|
||||
},
|
||||
applySession(admin: AdminUser, accessToken: string, refreshToken: string) {
|
||||
this.token = accessToken
|
||||
this.refreshToken = refreshToken
|
||||
setAuthTokens('admin', {
|
||||
access_token: accessToken,
|
||||
refresh_token: refreshToken,
|
||||
})
|
||||
applySession(admin: AdminUser) {
|
||||
this.token = ''
|
||||
this.refreshToken = ''
|
||||
this.applyAdmin(admin)
|
||||
},
|
||||
applyAdmin(admin: AdminUser) {
|
||||
@@ -85,11 +81,13 @@ export const useAdminSessionStore = defineStore('adminSession', {
|
||||
this.username = admin.username
|
||||
this.nickname = admin.nickname
|
||||
this.supportStatus = admin.support_status || 'offline'
|
||||
this.passwordMustChange = admin.password_must_change
|
||||
this.roles = admin.roles || []
|
||||
this.permissions = admin.permissions || []
|
||||
localStorage.setItem('admin_id', String(admin.id))
|
||||
localStorage.setItem('admin_username', admin.username)
|
||||
localStorage.setItem('admin_support_status', admin.support_status || 'offline')
|
||||
localStorage.setItem('admin_password_must_change', String(admin.password_must_change))
|
||||
},
|
||||
setSupportStatus(status: 'online' | 'offline' | 'busy') {
|
||||
this.supportStatus = status
|
||||
|
||||
Reference in New Issue
Block a user