UserDomain Add
This commit is contained in:
36
frontend/src/stores/useAuthStore.ts
Normal file
36
frontend/src/stores/useAuthStore.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { api } from 'boot/axios';
|
||||
import { Router } from '../router';
|
||||
|
||||
|
||||
export const useAuthStore = defineStore({
|
||||
id: 'auth',
|
||||
state: () => ({
|
||||
token: localStorage.getItem('token'),
|
||||
returnUrl: ''
|
||||
}),
|
||||
actions: {
|
||||
async login(username:string, password:string) {
|
||||
const params = new URLSearchParams();
|
||||
params.append('username', username);
|
||||
params.append('password', password);
|
||||
try{
|
||||
const result = await api.post(`http://127.0.0.1:8000/api/token`,params);
|
||||
console.info(result);
|
||||
this.token =result.data.access_token;
|
||||
localStorage.setItem('token', result.data.access_token);
|
||||
Router.push(this.returnUrl || '/');
|
||||
return true;
|
||||
}catch(e)
|
||||
{
|
||||
console.info(e);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
logout() {
|
||||
this.token = null;
|
||||
localStorage.removeItem('token');
|
||||
Router.push('/login');
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user