Add (vite build)
This commit is contained in:
44
resources/js/stores/useAuth.js
Normal file
44
resources/js/stores/useAuth.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { defineStore } from "pinia";
|
||||
import axios from "axios";
|
||||
|
||||
export const useAuthStore = defineStore("auth", {
|
||||
state: () => ({
|
||||
loggedIn: localStorage.getItem("token") ? true : false,
|
||||
user: null,
|
||||
}),
|
||||
|
||||
getters: {},
|
||||
|
||||
actions: {
|
||||
async login(credentials) {
|
||||
await axios.get("sanctum/csrf-cookie");
|
||||
|
||||
const response = (await axios.post("api/login", credentials)).data;
|
||||
|
||||
if (response) {
|
||||
const token = `Bearer ${response.token}`;
|
||||
|
||||
localStorage.setItem("token", token);
|
||||
axios.defaults.headers.common["Authorization"] = token;
|
||||
|
||||
await this.ftechUser();
|
||||
}
|
||||
},
|
||||
|
||||
async logout() {
|
||||
const response = (await axios.post("api/logout")).data;
|
||||
|
||||
if (response) {
|
||||
localStorage.removeItem("token");
|
||||
|
||||
this.$reset();
|
||||
}
|
||||
},
|
||||
|
||||
async ftechUser() {
|
||||
this.user = (await axios.get("api/me")).data;
|
||||
|
||||
this.loggedIn = true;
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user