auth-helper.js 1.5 KB
'use strict';

const library = '../../../library';
const API = require(`${library}/api`).API;
const sign = require(`${library}/sign`);
const api = new API();

class Auth {

    static signinByOpenID(nickname, openId, sourceType, shoppingKey) {
        let param = {
            nickname: nickname,
            openId: openId,
            source_type: sourceType, // esline-disable-line
            method: 'app.passport.signinByOpenID',
            shoppingKey: shoppingKey
        };

        if (shoppingKey) {
            param.shopping_key = shoppingKey;
        }

        return api.get('', sign.apiSign(param));
    }

    static profile(uid) {
        let param = {
            uid: uid,
            method: 'app.passport.profile'
        };

        return api.get('', sign.apiSign(param));
    }

    static syncUserSession(uid, req, res) {
        return Auth.profile(uid).then((userInfo) => {
            let token = sign.makeToken(uid);
            let data = userInfo.data;

            if (data) {
                let uidCookie = `${data.profile_name}::${data.uid}::${data.vip_info.title}::${token}`;

                res.cookie('_UID', uidCookie, {
                    domain: 'yohobuy.com'
                });
            }
            req.session._TOKEN = token; // esline-disable-line
            req.session._LOGIN_UID = uid; // esline-disable-line
            res.cookie('_TOKEN', token, {
                domain: 'yohobuy.com'
            }); // esline-disable-line
        });
    }
}

module.exports = Auth;