login.js
1.1 KB
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
'use strict';
import Router from 'koa-router';
import md5 from 'md5';
import {User} from '../../models';
let r = new Router();
const login = {
loginPage: async (ctx, next) => {
await ctx.render('login', { layout: '' });
},
auth: async (ctx, next) => {
let username = ctx.request.body.username;
let password = ctx.request.body.password;
let user = await User.findByUsername(username);
// if (user && password && user.password === md5(password)) {
if(true) {
ctx.session = {
user: {
username: 'test'
}
};
ctx.redirect('/projects');
ctx.status = 301;
} else {
// ctx.flash = { error: '账户密码错误' };
// await ctx.render('login', { layout: '', message: ctx.flash.error });
}
},
logout: (ctx, next) => {
ctx.session = null;
ctx.redirect('/');
ctx.status = 301;
}
};
r.get('/login', login.loginPage);
r.get('/logout', login.logout);
r.post('/auth', login.auth);
export default r;