Recruitment_site/offer_frontend/src/stores/auth.js

32 lines
824 B
JavaScript

import { defineStore } from 'pinia'
import { login as loginApi, getMe } from '@/api/auth'
export const useAuthStore = defineStore('auth', {
state: () => ({
user: null,
loading: false,
}),
getters: {
isLoggedIn: s => !!s.user,
isSuperAdmin: s => s.user?.role === 'superadmin',
isAdmin: s => s.user?.role === 'admin',
isSeeker: s => s.user?.role === 'seeker',
},
actions: {
async login(username, password) {
const { data } = await loginApi({ username, password })
localStorage.setItem('access_token', data.access)
localStorage.setItem('refresh_token', data.refresh)
await this.fetchMe()
},
async fetchMe() {
const { data } = await getMe()
this.user = data
},
logout() {
localStorage.clear()
this.user = null
},
},
})