Authored by htoooth

add captcha

... ... @@ -77,8 +77,6 @@ const sendCodePage = (req, res, next) => {
service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
.then(result => {
console.log(result);
if (!(result.code && result.code === 200)) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
... ...
... ... @@ -4,10 +4,13 @@
'use strict';
const Captchapng = require('captchapng');
const _ = require('lodash');
const helpers = global.yoho.helpers;
const requiredAPI = (req, res, next) => {
let captchaToken = (req.body.verifyCode || '').toLowerCase();
let captchaToken = +(req.body.verifyCode || '').toLowerCase();
if (captchaToken === req.session.captcha) {
return next();
... ... @@ -20,16 +23,48 @@ const requiredAPI = (req, res, next) => {
};
const requiredPage = (req, res, next) => {
let captchaToken = (req.body.verifyCode || '').toLowerCase();
let captchaToken = +(req.body.verifyCode || '').toLowerCase();
if (captchaToken === req.session.captcha) {
return next();
} else {
return res.redirect(helpers.urlFormat('/passport/back/index.html'));
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
};
const _generateCaptcha = (width, height, length) => {
let min = Math.pow(10, (length - 1 || 1));
let max = Math.pow(10, (length - 1 || 1)) * 9;
let token = '' + _.random(min, max);
let png = new Captchapng(width, height, token);//
png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
return {
image: new Buffer(png.getBase64(), 'base64'),
text: token
};
};
const generate = (req, res) => {
let width = req.query.w || 150;
let height = req.query.h || 50;
let length = +(req.query.l || 4);
let captcha = _generateCaptcha(width, height, length);
req.session.captcha = captcha.text;
res.writeHead(200, {
'Content-Type': 'image/png'
});
res.end(captcha.image);
};
module.exports = {
requiredAPI,
requiredPage
requiredPage,
generate
};
... ...
... ... @@ -10,7 +10,7 @@ const express = require('express');
const cRoot = './controllers';
const login = require(cRoot + '/login');
// const captcha = require(cRoot + '/captcha');
const captcha = require(cRoot + '/captcha');
const back = require(cRoot + '/back');
const reg = require(cRoot + '/reg');
... ... @@ -34,23 +34,20 @@ router.get('/back/index', back.index);
// 实时验证输入是否正确
router.post('/back/authcode',
// captcha.requiredAPI,
captcha.requiredAPI,
back.validateInputAPI,
back.getUserInfoAPI);
// 提交按钮邮件API
router.post('/back/email',
// captcha.requiredPage,
captcha.requiredPage,
back.validateUserPage,
back.sendCodePage,
back.saveInSession);
// 提交按钮手机API
router.post('/back/mobile',
// captcha.requiredPage,
captcha.requiredPage,
back.validateUserPage,
back.sendCodePage,
back.saveInSession);
... ... @@ -68,29 +65,26 @@ router.get('/back/sendEmail',
*/
// 验证手机短信页面
router.get('/back/verification',
captcha.requiredPage,
back.validateMobileInSession,
// captcha.requiredPage,
back.verifyCodeByMobilePage);
// 重新发送短信接口
router.post('/back/sendBackMobile',
// captcha.requiredAPI,
captcha.requiredAPI,
back.validateMobileAPI,
back.sendBackMobileAPI);
// 验证手机验证码接口
router.post('/back/backMobile',
// captcha.requiredAPI,
captcha.requiredAPI,
back.verifyCodeByMobileAPI);
/**
* 重置密码
*/
// 重置密码页面
// 重置密码页面
router.get('/back/backcode',
back.validateExistCodePage,
back.validateCodeByMobilePage,
... ... @@ -109,4 +103,6 @@ router.get('/back/resetSuccess',
back.validateSuccessStatusPage,
back.resetPwdSuccessPage);
router.get('/images', captcha.generate);
module.exports = router;
... ...