-
Notifications
You must be signed in to change notification settings - Fork 4
/
user.js
61 lines (54 loc) · 1.71 KB
/
user.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import axios from 'axios';
import { defineStore } from 'pinia';
import { useAuthStore } from './auth';
const HEADERS = {
headers: { 'Content-Type': 'application/json' },
};
const SIGNUP_URL = `${import.meta.env.VITE_VUE_API_BASE_URL}/user`;
const CHANGE_PASSWORD_URL = `${import.meta.env.VITE_VUE_API_BASE_URL}/user`;
const REQUEST_PASSWORD_RESET_URL = `${import.meta.env.VITE_VUE_API_BASE_URL}/request-reset-password`;
const PASSWORD_RESET_URL = `${import.meta.env.VITE_VUE_API_BASE_URL}/reset-password`;
const VALIDATE_PASSWORD_RESET_TOKEN_URL = `${import.meta.env.VITE_VUE_API_BASE_URL}/reset-token-validation`;
export const useUserStore = defineStore('user', {
state: () => ({
email: '',
}),
persist: true,
actions: {
async signup(email, password) {
const auth = useAuthStore();
await axios.post(SIGNUP_URL, { email, password }, HEADERS);
// Update store with email
this.$patch({ email });
// Log users in after signup and return that response
return await auth.login(email, password);
},
async changePassword(email, password) {
const auth = useAuthStore();
await axios.put(
CHANGE_PASSWORD_URL,
{ email, password },
{
headers: {
...HEADERS.headers,
Authorization: `Bearer ${auth.accessToken.value}`,
},
},
);
return await auth.login(email, password);
},
async requestPasswordReset(email) {
return await axios.post(REQUEST_PASSWORD_RESET_URL, { email }, HEADERS);
},
async resetPassword(password, token) {
return await axios.post(PASSWORD_RESET_URL, { password, token }, HEADERS);
},
async validateResetPasswordToken(token) {
return await axios.post(
VALIDATE_PASSWORD_RESET_TOKEN_URL,
{ token },
HEADERS,
);
},
},
});