INIT commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import {defineStore} from 'pinia'
|
||||
import {api} from "boot/axios";
|
||||
|
||||
export const useUserStore = defineStore('LoggedUser', {
|
||||
state: () => ({
|
||||
user: {},
|
||||
accessToken: null
|
||||
}),
|
||||
actions: {
|
||||
|
||||
setAccessToken(token) {
|
||||
console.log(token);
|
||||
if (token) {
|
||||
localStorage.setItem("accessToken", token);
|
||||
}
|
||||
this.accessToken = localStorage.getItem("accessToken");
|
||||
},
|
||||
getAccessToken() {
|
||||
if (!this.accessToken) {
|
||||
this.setAccessToken();
|
||||
}
|
||||
|
||||
return this.accessToken;
|
||||
},
|
||||
|
||||
async setupCredentials() {
|
||||
await api.get("/me").then(({data}) => {
|
||||
this.user = data;
|
||||
}).catch(e => {
|
||||
console.log(e);
|
||||
});
|
||||
},
|
||||
|
||||
async register(formData) {
|
||||
return api.post('/registration', formData).then(response => {
|
||||
const registeredUser = response.data;
|
||||
const isSuccess = response.status === 201
|
||||
})
|
||||
},
|
||||
|
||||
async login(formData) {
|
||||
|
||||
return api.post("/login", formData).then(response => {
|
||||
const data = response.data;
|
||||
console.log("data", data);
|
||||
this.setAccessToken(data.access_token);
|
||||
this.user = data.user;
|
||||
})
|
||||
|
||||
// console.log("login", username, password);
|
||||
// Object.assign(this.user, {
|
||||
// id: 1,
|
||||
// email: "michal@kvacek.cz",
|
||||
// name: "Michal K.",
|
||||
// accessToken: "abcdef",
|
||||
// avatarImageUrl: "https://via.placeholder.com/50x50"
|
||||
// });
|
||||
},
|
||||
logout() {
|
||||
Object.assign(this.user, {});
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user