feat(web): add login, auth store and router guard
This commit is contained in:
31
admin-web/src/stores/auth.ts
Normal file
31
admin-web/src/stores/auth.ts
Normal 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 }
|
||||
})
|
||||
Reference in New Issue
Block a user