feat(web): add login, auth store and router guard

This commit is contained in:
2026-07-07 15:25:47 +08:00
parent bcef04a8cc
commit e731ee7a2e
9 changed files with 227 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
export const useAuthStore = defineStore('auth', () => {
const token = ref(localStorage.getItem('mci-token') || '')
const tenantCode = ref(localStorage.getItem('mci-tenant-code') || '')
const username = ref('')
const roleCodes = ref<string[]>([])
const isLoggedIn = computed(() => !!token.value)
function setAuth(t: string, tc: string, user: string, roles: string[]) {
token.value = t
tenantCode.value = tc
username.value = user
roleCodes.value = roles
localStorage.setItem('mci-token', t)
localStorage.setItem('mci-tenant-code', tc)
}
function clearAuth() {
token.value = ''
tenantCode.value = ''
username.value = ''
roleCodes.value = []
localStorage.removeItem('mci-token')
localStorage.removeItem('mci-tenant-code')
}
return { token, tenantCode, username, roleCodes, isLoggedIn, setAuth, clearAuth }
})