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

const passport = require('passport');
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(req, res, next, user) {
    let shoppingKey = cookie.getShoppingKey(req);
    let refer = req.cookies.refer;

    if (refer) {
        refer = decodeURI(req.cookies.refer);
    } else {
        refer = config.siteUrl;
    }

    if (/sign|login/.test(refer)) {
        refer = config.siteUrl;
    }
    if (user.openId && user.nickname) {
        let signinByOpenID;

        if (user.sourceType === 'wechat') {

            // PC 的微信登录之前使用了 open_id, 所以需要特别的接口处理
            signinByOpenID = AuthHelper.signinByWechat(
                user.nickname, user.openId, user.unionId, user.sourceType, shoppingKey);
        } else {
            signinByOpenID = AuthHelper.signinByOpenID(
                user.nickname, user.openId, user.sourceType, shoppingKey);
        }

        signinByOpenID.then((result) => {
            if (result.code !== 200) {
                return Promise.reject(result);
            }
            if (result.data['is_bind'] && result.data['is_bind'] === 'N') { //eslint-disable-line
                return helpers.urlFormat('/passport/thirdlogin/index', {
                    openId: user.unionId || user.openId,
                    sourceType: user.sourceType,
                    refer: refer
                });
            } else if (result.code === 200 && result.data.uid) {
                return AuthHelper.syncUserSession(result.data.uid, req, res).then(() => {
                    return refer;
                }).catch(next);
            }
        }).then((redirectTo) => {
            res.redirect(redirectTo);
        }).catch(() => {
            res.redirect(loginPage);
        });
    } else {
        res.redirect(loginPage);
    }
}

const wechat = {
    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();
    },
    login: (req, res, next) => {
        return passport.authenticate('wechat', {
            state: uuid.v4()
        })(req, res, next);
    },
    callback: (req, res, next) => {
        passport.authenticate('wechat', (err, user) => {
            if (err) {
                log.error(`wechat authenticate error : ${JSON.stringify(err)}`);
                return res.redirect(loginPage);
            }

            doPassportCallback(req, res, next, {
                openId: user._json.openid,
                unionId: user._json.unionid || user.id,
                nickname: user._json.nickname || user.displayName,
                sourceType: 'wechat',
                rawUser: user
            });
        })(req, res, next);
    }
};

exports.wechat = wechat;