-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
58 lines (50 loc) · 1.67 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
import { partials, sessionInfo, checkInput, passwordCheck, displayError } from './helper.js'
import { post } from './requester.js'
export const user = {
login: function (ctx) {
ctx.loadPartials(partials)
.partial('./templates/auth/login.hbs')
},
postLogin: function (ctx) {
const { username, password } = ctx.params
if (checkInput(username, password)) {
alert('All inputs must be field!')
return
}
post('user', 'login', { username, password }, 'Basic')
.then(data => {
sessionInfo(data)
ctx.loadPartials(partials)
.partial('./templates/home/home.hbs')
ctx.redirect('#/')
})
.catch(console.error);
},
register: function (ctx) {
ctx.loadPartials(partials)
.partial('./templates/auth/register.hbs')
},
postRegister: function (ctx) {
const { username, password, rePassword } = ctx.params
if (checkInput(username, password, rePassword)) {
displayError('All inputs must be field!');
return;
}
if (passwordCheck(password, rePassword)) {
post('user', '', { username, password, rePassword }, 'Basic')
.then(data => {
sessionInfo(data)
ctx.redirect('#/')
})
.catch(console.error);
}
},
logout: function (ctx) {
post('user', '_logout', {}, 'Kinvey')
.then(() => {
sessionStorage.clear();
ctx.redirect('#/');
})
.catch(console.error);
}
}