Initial commit: SIBU 2.0 MISSION
This commit is contained in:
47
frontend/src/stores/auth.ts
Normal file
47
frontend/src/stores/auth.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref<string | null>(localStorage.getItem('auth_token'))
|
||||
const role = ref<string | null>(localStorage.getItem('user_role'))
|
||||
const userName = ref<string | null>(localStorage.getItem('user_name'))
|
||||
|
||||
const isAuthenticated = computed(() => !!token.value)
|
||||
const isAdmin = computed(() => role.value?.toUpperCase() === 'ADMIN')
|
||||
const isDriver = computed(() => role.value?.toUpperCase() === 'DRIVER')
|
||||
const isPromoter = computed(() => role.value?.toUpperCase() === 'PROMOTER')
|
||||
const isPassenger = computed(() => !role.value || role.value?.toUpperCase() === 'PASSENGER')
|
||||
|
||||
function login(newToken: string, newRole: string, newName: string) {
|
||||
token.value = newToken
|
||||
role.value = newRole
|
||||
userName.value = newName
|
||||
localStorage.setItem('auth_token', newToken)
|
||||
localStorage.setItem('user_role', newRole)
|
||||
localStorage.setItem('user_name', newName)
|
||||
}
|
||||
|
||||
function logout() {
|
||||
token.value = null
|
||||
role.value = null
|
||||
userName.value = null
|
||||
localStorage.removeItem('auth_token')
|
||||
localStorage.removeItem('user_role')
|
||||
localStorage.removeItem('user_name')
|
||||
window.location.href = '/'
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
role,
|
||||
userName,
|
||||
isAuthenticated,
|
||||
isAdmin,
|
||||
isDriver,
|
||||
isPromoter,
|
||||
isPassenger,
|
||||
login,
|
||||
logout
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user