登录注册

This commit is contained in:
lwg 2017-07-10 20:51:04 +08:00
parent abc3470010
commit c97b538740
9 changed files with 196 additions and 74 deletions

View File

@ -16,6 +16,7 @@
"license": "ISC",
"dependencies": {
"fs-extra": "^3.0.1",
"jsonwebtoken": "^7.4.1",
"koa": "^2.0.0",
"koa-bodyparser": "^3.2.0",
"koa-logger": "^3.0.0",

View File

@ -7,6 +7,8 @@ yapi.commons = commons;
import dbModule from './utils/db.js';
import userauth from './middleware/userauth.js'
//checkToken作为中间件存在
import checkToken from './middleware/checkToken.js';
import Koa from 'koa'
import convert from 'koa-convert'
@ -24,6 +26,7 @@ app.use(router.allowedMethods())
app.use(koaStatic(
yapi.path.join(yapi.WEBROOT, 'static')
))
app.use(checkToken) //不是注册和登录才需要检查token
app.listen(yapi.WEBCONFIG.port)
commons.log(`the server is start at port ${yapi.WEBCONFIG.port}`)

View File

@ -3,6 +3,7 @@ import yapi from '../yapi.js'
import baseController from './base.js'
import mongoose from 'mongoose'
const jwt = require('jsonwebtoken');
const sha1 = require('sha1');
class userController extends baseController{
@ -26,28 +27,26 @@ class userController extends baseController{
let username = ctx.request.body.username;
let password = ctx.request.body.password;
let result = await userInst.findByName(username);
console.log(password)
userInst.save(function(error){
console.log(111)
var error = userInst.validateSync();
assert.equal(error.errors['password'].message, 'password required');
});
var token = jwt.sign(result._id,'qunar',{expiresIn: 24 * 60 * 60 /* 1 days */});
console.log(token);
if(!username){
return ctx.body = yapi.commons.resReturn(null,400,'用户名不能为空');
}
// if(!password){
// return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空');
// }
if(!password){
return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空');
}
//输入一个不存在的用户名
var checkRepeat = await userInst.checkRepeat(username);//然后检查是否已经存在该用户
if(checkRepeat==0){
return ctx.body = yapi.commons.resReturn(null,404,'该用户不存在'); //返回的错误码对吗????
}else if(sha1(result.password)===password){ //用户名存在,判断密码是否正确,正确则可以登录
console.log('密码一致'); //是不是还需要把用户名密码一些东西写到session
//生成一个新的token,并存到数据库
// var token = jwt.sign(result._id,'qunar',{expiresIn: 24 * 60 * 60 /* 1 days */});
// console.log(token);
//result.token = token;
// setCookie('token', sha1(username+password));
// userInst.update({_id, result._id}, {token: sha1(username+password)})
// return ctx.body = {username: ''}
@ -62,36 +61,40 @@ class userController extends baseController{
async reg(ctx){ //注册
var userInst = yapi.getInst(userModel);
let params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码
// if(!params.username){
// return ctx.body = yapi.commons.resReturn(null,400,'用户名不能为空');
// }
// if(!params.password){
// return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空');
// }
// if(!params.email){
// return ctx.body = yapi.commons.resReturn(null,400,'邮箱不能为空');
// }
// var checkRepeat = await userInst.checkRepeat(params.username);//然后检查是否已经存在该用户
// if(checkRepeat>0){
// return ctx.body = yapi.commons.resReturn(null,401,'该用户名已经注册');
// }
// var checkRepeat = await userInst.checkRepeat(params.email);//然后检查是否已经存在该用户
// if(checkRepeat>0){
// return ctx.body = yapi.commons.resReturn(null,401,'该邮箱已经注册');
// }
let result = await userInst.findByName(params.username);
if(!params.username){
return ctx.body = yapi.commons.resReturn(null,400,'用户名不能为空');
}
if(!params.password){
return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空');
}
if(!params.email){
return ctx.body = yapi.commons.resReturn(null,400,'邮箱不能为空');
}
var checkRepeat = await userInst.checkRepeat(params.username);//然后检查是否已经存在该用户
if(checkRepeat>0){
return ctx.body = yapi.commons.resReturn(null,401,'该用户名已经注册');
}
var checkRepeat = await userInst.checkRepeat(params.email);//然后检查是否已经存在该用户
if(checkRepeat>0){
return ctx.body = yapi.commons.resReturn(null,401,'该邮箱已经注册');
}
//var token = jwt.sign(result._id,'qunar',{expiresIn: 24 * 60 * 60 /* 1 days */});
//console.log(111)
let data = {
username: params.username,
password: sha1(params.password),//加密
email: params.email,
//token: token, //创建token并存入数据库
role: params.role,
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
}
try{
let user = await userInst.save(data);
user = yapi.commons.fieldSelect(user,['id','username','password','email','role'])
user = yapi.commons.fieldSelect(user,['id','username','password','email'])
ctx.body = yapi.commons.resReturn(user);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 401, e.message);

View File

@ -0,0 +1,17 @@
const jwt = require('jsonwebtoken');
//检查token是否过期
module.exports = async ( ctx, next ) => {
// const authorization = ctx.get('Authorization');
// if (authorization === '') {
// ctx.throw(401, 'no token detected in http header ');
// }
// const token = authorization.split(' ')[1];
// let tokenContent;
// try {
// tokenContent = await jwt.verify(token, 'qunar'); //如果token过期或验证失败将抛出错误
// } catch (err) {
// ctx.throw(401, 'invalid token');
// }
await next();
}

View File

@ -21,6 +21,7 @@ class userModel extends baseModel{
type: String,
required: true
},
token: String,
passsalt: String,
role: String,
add_time: Number,

View File

@ -16,6 +16,10 @@ var _userauth = require('./middleware/userauth.js');
var _userauth2 = _interopRequireDefault(_userauth);
var _checkToken = require('./middleware/checkToken.js');
var _checkToken2 = _interopRequireDefault(_checkToken);
var _koa = require('koa');
var _koa2 = _interopRequireDefault(_koa);
@ -39,6 +43,8 @@ var _router2 = _interopRequireDefault(_router);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_yapi2.default.commons = _commons2.default;
//checkToken作为中间件存在
_yapi2.default.connect = _db2.default.connect();
@ -48,6 +54,7 @@ app.use((0, _koaBodyparser2.default)());
app.use(_router2.default.routes());
app.use(_router2.default.allowedMethods());
app.use((0, _koaStatic2.default)(_yapi2.default.path.join(_yapi2.default.WEBROOT, 'static')));
app.use(_checkToken2.default); //不是注册和登录才需要检查token
app.listen(_yapi2.default.WEBCONFIG.port);
_commons2.default.log('the server is start at port ' + _yapi2.default.WEBCONFIG.port);

View File

@ -50,6 +50,7 @@ var _mongoose2 = _interopRequireDefault(_mongoose);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var jwt = require('jsonwebtoken');
var sha1 = require('sha1');
var userController = function (_baseController) {
@ -80,7 +81,7 @@ var userController = function (_baseController) {
key: 'login',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var userInst, username, password, result, checkRepeat;
var userInst, username, password, result, token, checkRepeat;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
@ -95,14 +96,9 @@ var userController = function (_baseController) {
case 5:
result = _context.sent;
token = jwt.sign(result._id, 'qunar', { expiresIn: 24 * 60 * 60 /* 1 days */ });
console.log(password);
userInst.save(function (error) {
console.log(111);
var error = userInst.validateSync();
assert.equal(error.errors['password'].message, 'password required');
});
console.log(token);
if (username) {
_context.next = 10;
@ -112,36 +108,49 @@ var userController = function (_baseController) {
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '用户名不能为空'));
case 10:
_context.next = 12;
return userInst.checkRepeat(username);
if (password) {
_context.next = 12;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空'));
case 12:
_context.next = 14;
return userInst.checkRepeat(username);
case 14:
checkRepeat = _context.sent;
if (!(checkRepeat == 0)) {
_context.next = 17;
_context.next = 19;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '该用户不存在'));
case 17:
case 19:
if (!(sha1(result.password) === password)) {
_context.next = 22;
_context.next = 24;
break;
}
//用户名存在,判断密码是否正确,正确则可以登录
console.log('密码一致'); //是不是还需要把用户名密码一些东西写到session
//生成一个新的token,并存到数据库
// var token = jwt.sign(result._id,'qunar',{expiresIn: 24 * 60 * 60 /* 1 days */});
// console.log(token);
//result.token = token;
// setCookie('token', sha1(username+password));
// userInst.update({_id, result._id}, {token: sha1(username+password)})
// return ctx.body = {username: ''}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 200, 'ok'));
case 22:
case 24:
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码错误'));
case 23:
case 25:
case 'end':
return _context.stop();
}
@ -159,7 +168,7 @@ var userController = function (_baseController) {
key: 'reg',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) {
var userInst, params, data, user;
var userInst, params, result, checkRepeat, data, user;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
@ -167,57 +176,101 @@ var userController = function (_baseController) {
//注册
userInst = _yapi2.default.getInst(_user2.default);
params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码
// if(!params.username){
// return ctx.body = yapi.commons.resReturn(null,400,'用户名不能为空');
// }
// if(!params.password){
// return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空');
// }
// if(!params.email){
// return ctx.body = yapi.commons.resReturn(null,400,'邮箱不能为空');
// }
// var checkRepeat = await userInst.checkRepeat(params.username);//然后检查是否已经存在该用户
// if(checkRepeat>0){
// return ctx.body = yapi.commons.resReturn(null,401,'该用户名已经注册');
// }
// var checkRepeat = await userInst.checkRepeat(params.email);//然后检查是否已经存在该用户
// if(checkRepeat>0){
// return ctx.body = yapi.commons.resReturn(null,401,'该邮箱已经注册');
// }
_context2.next = 4;
return userInst.findByName(params.username);
case 4:
result = _context2.sent;
if (params.username) {
_context2.next = 7;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '用户名不能为空'));
case 7:
if (params.password) {
_context2.next = 9;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空'));
case 9:
if (params.email) {
_context2.next = 11;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '邮箱不能为空'));
case 11:
_context2.next = 13;
return userInst.checkRepeat(params.username);
case 13:
checkRepeat = _context2.sent;
if (!(checkRepeat > 0)) {
_context2.next = 16;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该用户名已经注册'));
case 16:
_context2.next = 18;
return userInst.checkRepeat(params.email);
case 18:
checkRepeat = _context2.sent;
if (!(checkRepeat > 0)) {
_context2.next = 21;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该邮箱已经注册'));
case 21:
//var token = jwt.sign(result._id,'qunar',{expiresIn: 24 * 60 * 60 /* 1 days */});
//console.log(111)
data = {
username: params.username,
password: sha1(params.password), //加密
email: params.email,
//token: token, //创建token并存入数据库
role: params.role,
add_time: _yapi2.default.commons.time(),
up_time: _yapi2.default.commons.time()
};
_context2.prev = 3;
_context2.next = 6;
_context2.prev = 22;
_context2.next = 25;
return userInst.save(data);
case 6:
case 25:
user = _context2.sent;
user = _yapi2.default.commons.fieldSelect(user, ['id', 'username', 'password', 'email', 'role']);
user = _yapi2.default.commons.fieldSelect(user, ['id', 'username', 'password', 'email']);
ctx.body = _yapi2.default.commons.resReturn(user);
_context2.next = 14;
_context2.next = 33;
break;
case 11:
_context2.prev = 11;
_context2.t0 = _context2['catch'](3);
case 30:
_context2.prev = 30;
_context2.t0 = _context2['catch'](22);
ctx.body = _yapi2.default.commons.resReturn(null, 401, _context2.t0.message);
case 14:
case 33:
case 'end':
return _context2.stop();
}
}
}, _callee2, this, [[3, 11]]);
}, _callee2, this, [[22, 30]]);
}));
function reg(_x2) {

View File

@ -0,0 +1,36 @@
'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var jwt = require('jsonwebtoken');
//检查token是否过期
module.exports = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) {
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return next();
case 2:
case 'end':
return _context.stop();
}
}
}, _callee, undefined);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();

View File

@ -63,6 +63,7 @@ var userModel = function (_baseModel) {
type: String,
required: true
},
token: String,
passsalt: String,
role: String,
add_time: Number,