login.js 7.91 KB
/**
 * 登录
 * @author: Bi Kai<kai.bi@yoho.cn>
 * @date: 2016/05/09
 */
'use strict';

const passport = require('passport');
const md5 = require('md5');
const uuid = require('uuid');
const cookie = global.yoho.cookie;
const helpers = global.yoho.helpers;
const log = global.yoho.logger;
const config = global.yoho.config;
const AuthHelper = require('../models/auth-helper');

const loginPage = `${config.siteUrl}/passport/login/index`;

function doPassportCallback(openId, nickname, sourceType, req, res) {
    let shoppingKey = cookie.getShoppingKey(req);
    let refer = req.cookies.refer;

    if (refer) {
        refer = decodeURI(req.cookies.refer);
    } else {
        refer = `${config.siteUrl}/home`;
    }

    if (/sign|login/.test(refer)) {
        refer = `${config.siteUrl}/home`;
    }
    if (openId && nickname) {
        return AuthHelper.signinByOpenID(nickname, openId, sourceType, shoppingKey).then((result) => {
            if (result.data['is_bind'] && result.data['is_bind'] === 'N') { //eslint-disable-line
                return helpers.urlFormat('/passport/bind/index', {
                    openId: openId,
                    sourceType: sourceType,
                    refer: refer
                });
            } else if (result.code === 200 && result.data.uid) {
                return AuthHelper.syncUserSession(result.data.uid, req, res).then(() => {
                    return refer;
                });
            }
        }).then((redirectTo) => {
            return res.redirect(redirectTo);
        });
    }
}

const common = {
    beforeLogin: (req, res, next) => {
        let refer = req.query.refer;

        if (!refer) {
            refer = req.get('Referer');
        }
        refer && res.cookie('refer', encodeURI(refer), {
            domain: 'yohobuy.com'
        });
        next();
    }
};

const local = {
    loginPage: (req, res) => {
        // 设置登录有效时间30分钟, 防机器刷,cache不稳定,改为cookie
        res.cookie('LE' + md5('_LOGIN_EXPIRE'), (new Date()).getTime() / 1000 + 1800);

        // 清除cookie
        res.clearCookie('_UID');
        res.clearCookie('_TOKEN');

        res.render('login', {
            loginIndex: true, // 模板中使用JS的标识
            backUrl: 'javascript:history.go(-1)', // eslint-disable-line // 返回的URL链接
            showHeaderImg: true, // 控制显示头部图片
            isPassportPage: true, // 模板中模块标识
            registerUrl: '/reg.html', // 注册的URL链接
            aliLoginUrl: '/passport/login/alipay', // 支付宝快捷登录的URL链接
            weiboLoginUrl: '/passport/login/sina', // 微博登录的URL链接
            qqLoginUrl: '/passport/login/qq', // 腾讯QQ登录的URL链接
            wechatLoginUrl: '/passport/login/wechat', // 微信登录的URL链接
            internationalUrl: '/login.html', // 国际号登录的URL链接
            phoneRetriveUrl: '/passport/back/mobile', // 通过手机号找回密码的URL链接
            emailRetriveUrl: '/passport/back/email', // 通过邮箱找回密码的URL链接
            module: 'passport',
            page: 'login',
            title: '登录'
        });
    },
    login: (req, res, next) => {
        passport.authenticate('local', (err, user) => {
            if (err) {
                res.json({
                    code: 400,
                    message: err,
                    data: ''
                });
            } else {
                let refer = req.cookies.refer;

                if (refer) {
                    refer = decodeURI(req.cookies.refer);
                } else {
                    refer = `${config.siteUrl}/home`;
                }

                if (/sign|login/.test(refer)) {
                    refer = `${config.siteUrl}/home`;
                }
                user.session = refer;
                user.href = refer;
                AuthHelper.syncUserSession(user.uid, req, res).then(() => {
                    res.json({
                        code: 200,
                        data: user
                    });
                });
            }
        })(req, res, next);
    },
    logout: (req, res) => {
        req.session = null;

        res.clearCookie('_UID', {
            domain: 'yohobuy.com'
        });
        res.clearCookie('_TOKEN', {
            domain: 'yohobuy.com'
        });
        res.clearCookie('_SPK');
        let refer = req.get('Referer') || config.siteUrl;

        res.redirect(refer);
    }
};

const wechat = {
    login: (req, res, next) => {
        req.session = req.session || {};
        req.session.authState = uuid.v4();
        return passport.authenticate('weixin', {
            state: req.session.authState
        })(req, res, next);
    },
    callback: (req, res, next) => {
        if (req.session && req.session.authState && req.session.authState === req.query.state) {
            passport.authenticate('weixin', (err, user) => {
                if (err) {
                    log.error(`wechat authenticate error : ${JSON.stringify(err)}`);
                    return res.redirect(loginPage);
                }
                let nickname = user._json.nickname || user.displayName;
                let openId = user._json.unionid || user.id;

                doPassportCallback(openId, nickname, 'wechat', req, res).catch(next);
            })(req, res, next);
        } else {
            return next(new Error('Auth State Mismatch'));
        }
    }
};

const sina = {
    login: (req, res, next) => {
        req.session = req.session || {};
        req.session.authState = uuid.v4();
        return passport.authenticate('sina', {
            state: req.session.authState
        })(req, res, next);
    },
    callback: (req, res, next) => {
        if (req.session && req.session.authState && req.session.authState === req.query.state) {
            passport.authenticate('sina', (err, user) => {
                if (err) {
                    log.error(`sina authenticate error : ${JSON.stringify(err)}`);
                    return res.redirect(loginPage);
                }
                let nickname = user.screen_name;
                let openId = user.id;

                doPassportCallback(openId, nickname, 'sina', req, res).catch(next);
            })(req, res, next);
        } else {
            return next(new Error('Auth State Mismatch'));
        }
    }
};

const qq = {
    login: (req, res, next) => {
        req.session = req.session || {};
        req.session.authState = uuid.v4();
        return passport.authenticate('qq', {
            state: req.session.authState
        })(req, res, next);
    },
    callback: (req, res, next) => {
        if (req.session && req.session.authState && req.session.authState === req.query.state) {
            passport.authenticate('qq', (err, user) => {
                if (err) {
                    log.error(`qq authenticate error : ${JSON.stringify(err)}`);
                    return res.redirect(loginPage);
                }
                let nickname = user.nickname;
                let openId = user.openid;

                doPassportCallback(openId, nickname, 'qq', req, res).catch(next);
            })(req, res, next);
        } else {
            return next(new Error('Auth State Mismatch'));
        }
    }
};

const alipay = {
    login: (req, res, next) => {
        return passport.authenticate('alipay')(req, res, next);
    },
    callback: (req, res, next) => {
        passport.authenticate('alipay', (err, user) => {
            if (err) {
                log.error(`alipay authenticate error : ${JSON.stringify(err)}`);
                return res.redirect(loginPage);
            }
            let nickname = user.realName;
            let openId = user.userId;

            doPassportCallback(openId, nickname, 'alipay', req, res).catch(next);
        })(req, res, next);
    }
};

exports.common = common;
exports.local = local;
exports.wechat = wechat;
exports.sina = sina;
exports.qq = qq;
exports.alipay = alipay;