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
|
import { reactive } from 'vue'
import { login, logout } from '@/api/pse-squared.js'
const username = sessionStorage.getItem("username") || localStorage.getItem("username");
const password = sessionStorage.getItem("password") || localStorage.getItem("password");
export const store = reactive({
isLoggedIn: username && password,
username,
password,
async login({username, password}, persistant) {
try {
await login({username, password});
this.username = username;
this.password = password;
this.isLoggedIn = true;
setStorage(this, persistant);
return true;
} catch(err) {
console.error(err);
return false;
}
},
async logout() {
logout();
this.username = "";
this.password = "";
this.isLoggedIn = false;
clearStorage();
return true;
}
});
if (username && password) {
store.login({username, password});
}
function setStorage(data, persistant) {
if (persistant) {
localStorage.setItem("username", data.username);
localStorage.setItem("password", data.password);
}
sessionStorage.setItem("username", data.username);
sessionStorage.setItem("password", data.password);
}
function clearStorage() {
sessionStorage.clear();
localStorage.clear();
}
|