-
Notifications
You must be signed in to change notification settings - Fork 17
/
user.vuex.ts
129 lines (106 loc) · 3.48 KB
/
user.vuex.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-disable max-classes-per-file */
import {
createModule, mutation, action,
} from 'vuex-class-component';
import { AxiosInstance } from 'axios';
const VuexModule = createModule({
strict: false,
});
// export default class UserStore extends VuexModule {
export default function createUserStore($http: AxiosInstance) {
class UserStore extends VuexModule {
public username: string|null = null;
public uid: number|null = null;
public loggedIn = false;
public triedAuthenticating = false;
@mutation showLoginFailedModal({ errorMessage }: { errorMessage: String }) { }
@mutation showResetCompleteModal() { }
@action() async login({ username, password }: { username: string, password: string }) {
const loginParams = {
name: username, pass: password, type: 'login', workbranch: 'localhost:8080',
};
const { data } = (await $http.post('/login/', new URLSearchParams(loginParams))).data;
if (data.success) {
this.loggedIn = true;
}
return data;
}
/** Authenticates the logged-in player. */
@action() async authenticate(): Promise<any> {
this.triedAuthenticating = true;
const response = await $http.get('/eterna_authenticate.php');
const { data } = response;
if (data === 'NOT LOGGED IN') {
this.loggedIn = false;
this.username = 'Anonymous';
this.uid = 0;
return;
}
const matches = data.match(/^(.+)\s(\d+)$/);
if (matches && matches.length === 3) {
const [match, username, uid] = matches;
this.username = username;
this.uid = Number(uid);
this.loggedIn = true;
} else {
throw new Error(`Authentication response malformed: ${data}`);
// TODO: is throw the right action?
}
}
}
return UserStore;
}
/*
class UserStore extends VuexModule {
constructor(public $http: AxiosInstance) {
super();
}
static bound(axios: AxiosInstance) {
class BoundUserStore extends UserStore {
constructor() {
super(axios);
}
}
BoundUserStore.prototype = UserStore.prototype;
return BoundUserStore;
}
public username: string|null = null;
public uid: number|null = null;
public loggedIn = false;
public triedAuthenticating = false;
@mutation showLoginFailedModal({ errorMessage }: { errorMessage: String }) { }
@mutation showResetCompleteModal() { }
@action() async login({ username, password }: { username: string, password: string }) {
const loginParams = {
name: username, pass: password, type: 'login', workbranch: 'localhost:8080',
};
const { data } = (await this.$http.post('/login/', new URLSearchParams(loginParams))).data;
if (data.success) {
this.loggedIn = true;
}
return data;
}
/** Authenticates the logged-in player. */
/* @action() async authenticate(): Promise<any> {
this.triedAuthenticating = true;
const response = await this.$http.get('/eterna_authenticate.php');
const { data } = response;
if (data === 'NOT LOGGED IN') {
this.loggedIn = false;
this.username = 'Anonymous';
this.uid = 0;
return;
}
const matches = data.match(/^(.+)\s(\d+)$/);
if (matches && matches.length === 3) {
const [match, username, uid] = matches;
this.username = username;
this.uid = Number(uid);
this.loggedIn = true;
} else {
throw new Error(`Authentication response malformed: ${data}`);
// TODO: is throw the right action?
}
}
}
*/