-
Notifications
You must be signed in to change notification settings - Fork 0
/
userRouter.js
40 lines (37 loc) · 1.13 KB
/
userRouter.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
const createRouter = require('koa-bestest-router');
const router = createRouter({
GET: {
'/api/users/me': async (ctx, next) => {
if (!ctx.session.authed) {
ctx.body = {
authed: false
}
} else {
ctx.body = {
authed: true,
account: 'xiaofu',
name: 'VieZhong'
}
}
}
},
POST: {
'/api/login': (ctx, next) => {
const user = ctx.request.body;
if(user.account == 'xiaofu' && user.password == '584662') {
ctx.session.authed = true;
ctx.status = 200;
ctx.body = {'account': user.account, 'name': 'VieZhong'};
ctx.cookies.set('login', 1, {'httpOnly': false});
} else {
ctx.status = 400;
}
},
'/api/logout': (ctx, next) => {
ctx.session.authed = false;
ctx.status = 200;
ctx.cookies.set('login', 0, {'httpOnly': false});
}
}
});
module.exports = router;