-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
46 lines (45 loc) · 1.65 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import axios from 'axios';
import { defineStore } from 'pinia';
export const useAuthStore = defineStore('auth', {
state: () => ({ authUser: null, authToken: null, authCompany: null }),
getters: {
user: (state) => state.authUser,
company: (state) => state.authCompany,
token: (state) => state.authToken
},
actions: {
async login(form) {
try {
const response = await axios.post('/login', form);
this.authToken = response.data.token;
this.authUser = response.data.user;
this.authCompany = response.data.company;
this.router.push('/');
} catch (errors) {
const status = errors.response.status;
const list_errors = errors.response.data.errors;
return { status, list_errors };
}
},
async register(form) {
try {
const response = await axios.post('/register', form);
this.authToken = response.data.token;
this.authUser = response.data.user;
this.router.push('/');
} catch (errors) {
const status = errors.response.status;
const list_errors = errors.response.data.errors;
return { status, list_errors };
}
},
async logout() {
const headers = { Authorization: `Bearer ${this.authToken}` };
await axios.post('/logout', null, { headers });
this.authToken = null;
this.authUser = null;
this.router.push('/login');
},
},
persist: true
})