feat: add admin login
This commit is contained in:
31
frontend/admin/src/stores/auth.js
Normal file
31
frontend/admin/src/stores/auth.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { authService } from '@/api/authService';
|
||||
import { defineStore } from 'pinia';
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref(localStorage.getItem('token'));
|
||||
const user = ref(null);
|
||||
|
||||
const isAuthenticated = computed(() => !!token.value);
|
||||
|
||||
async function login(username, password) {
|
||||
const { data } = await authService.login(username, password);
|
||||
token.value = data.token;
|
||||
user.value = data.user;
|
||||
localStorage.setItem('token', data.token);
|
||||
}
|
||||
|
||||
function logout() {
|
||||
token.value = null;
|
||||
user.value = null;
|
||||
localStorage.removeItem('token');
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
user,
|
||||
isAuthenticated,
|
||||
login,
|
||||
logout
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user