Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
var config = require('../../server/config/config'),
mongoSeed = require('../../server/config/mongo-seed'),
app = require('../../app'),
jwt = require('koa-jwt'),
baseUrl = 'http://localhost:' + config.app.port + '/api',
supertest = require('co-supertest'),
request = supertest(baseUrl);
// create a valid jwt token to be sent with every request
var user = mongoSeed.users[1];
var token = jwt.sign({id: user._id, name: user.name, email: user.email}, config.app.secret);
token = 'Bearer ' + token;
// make request and token objects available
exports.request = request;
exports.token = token;
// initiate KOAN server before each test is run
// also drop and re-seed the test database before each run
console.log('Mocha starting to run server tests on port ' + config.app.port);
beforeEach(function *() {
yield app.init(true);
});
// close the server after each test is done
afterEach(function (done) {
app.server.close(done);
log('Couldn\'t load user', err)
}
if (!user) {
this.status = 400
this.body = {
error: 'User doesn\'t exist'
}
return
}
// Compare password with the one within the DB
const isMatch = user.tryPassword(body.password)
if (isMatch) {
const token = jwt.sign(body, process.env.SESSION_SECRET, {
expiresIn: 300
})
this.body = {
token
}
return
}
this.status = 400
this.body = {
error: 'Wrong password'
}
if (userInfo != null) { // 如果查无此用户会返回 null
if (userInfo.password != data.password) {
if (!bcrypt.compareSync(data.password, userInfo.password)) {
this.body = { // 返回给前端的数据
success: false,
info: '密码错误!'
}
}
} else { // 密码正确
const userToken = {
id: userInfo.id,
name: userInfo.user_name,
originExp: Date.now() + 60 * 60 * 1000, // 设置过期时间(毫秒)为 1 小时
}
const secret = 'vue-koa-demo'; // 指定密钥,这是之后用来判断 token 合法性的标志
const token = jwt.sign(userToken, secret); // 签发 token
this.body = {
success: true,
token: token
}
}
} else {
this.body = {
success: false,
info: '用户不存在!'
}
}
}
if (!user) {
ctx.status = 400
ctx.body = {
error: 'User doesn\'t exist'
}
return
}
// Compare password with the one within the DB
const isMatch = user.tryPassword(body.password)
if (isMatch) {
const token = jwt.sign(body, process.env.SESSION_SECRET, {
expiresIn: 300
})
ctx.body = { token }
return
}
ctx.status = 400
ctx.body = {
error: 'Wrong password'
}
await next()
})
}
if (!user) {
this.status = 400
this.body = {
error: 'User doesn\'t exist'
}
return
}
// Compare password with the one within the DB
const isMatch = user.tryPassword(body.password)
if (isMatch) {
const token = jwt.sign(body, process.env.SESSION_SECRET, {
expiresIn: 300
})
this.body = {
token
}
return
}
this.status = 400
this.body = {
error: 'Wrong password'
}
}
if (!user) {
this.status = 401
this.body = {
error: 'User doesn\'t exist'
}
return
}
const isMatch = user.tryPassword(decoded.password)
if (isMatch) {
this.body = {
token: jwt.sign(decoded, process.env.SESSION_SECRET, {
expiresIn: 300
})
}
return
}
this.status = 401
this.body = {
error: 'Wrong password'
}
yield next
})
module.exports.sign = function(obj, option){
option = option || {};
return jwt.sign(obj, secret, option);
};
if (!user) {
ctx.status = 401
ctx.body = {
error: 'User doesn\'t exist'
}
return
}
const isMatch = user.tryPassword(decoded.password)
if (isMatch) {
ctx.body = {
token: jwt.sign(decoded, process.env.SESSION_SECRET, {
expiresIn: 300
})
}
return
}
ctx.status = 401
ctx.body = {
error: 'Wrong password'
}
await next()
})
phone : phone,
password: password
});
if (!foundUserList || foundUserList.length === 0) return this.throw(404, 'No user found');
const user = foundUserList[0].toJSON();
const jwtUser = {
id : user.id,
phone: user.phone,
name : user.name,
role : user.role
};
user.token = jwt.sign(jwtUser, config.jwt.secret, config.jwt.expiresInMinutes);
this.body = user;
};