使用不带Mongoose用户的passport-facebook(MEAN堆栈中没有Mongo)

时间:2016-02-20 04:23:48

标签: angularjs node.js mongodb express passport-facebook

我对MEAN堆栈很新,这似乎是非常天真或错误的方法,但我想问一下,当我们使用passport-facebook策略进行身份验证时,使用以下代码:

var FacebookStrategy = require('passport-facebook').Strategy;
var User = require('../models/user');
var fbConfig = require('../fb.js');

module.exports = function(passport) {

passport.use('facebook', new FacebookStrategy({
    clientID        : fbConfig.appID,
    clientSecret    : fbConfig.appSecret,
    callbackURL     : fbConfig.callbackUrl
},

// facebook will send back the tokens and profile
function(access_token, refresh_token, profile, done) {

    console.log('profile', profile);

    // asynchronous
    process.nextTick(function() {

        // find the user in the database based on their facebook id
        User.findOne({ 'id' : profile.id }, function(err, user) {

            // if there is an error, stop everything and return that
            // ie an error connecting to the database
            if (err)
                return done(err);

            // if the user is found, then log them in
            if (user) {
                return done(null, user); // user found, return that user
            } else {
                // if there is no user found with that facebook id, create them
                var newUser = new User();

                // set all of the facebook information in our user model
                newUser.fb.id    = profile.id; // set the users facebook id                 
                newUser.fb.access_token = access_token; // we will save the token that facebook provides to the user                    
                newUser.fb.firstName  = profile.name.givenName;
                newUser.fb.lastName = profile.name.familyName; // look at the passport user profile to see how names are returned
                //newUser.fb.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first

                // save our user to the database
                newUser.save(function(err) {
                    if (err)
                        throw err;

                    // if successful, return the new user
                    return done(null, newUser);
                });
            }

        });
    });

}));

};

我不需要将用户信息存储在任何数据存储中。我想仅在用户登录我的Web应用程序时存储令牌,基本上我不需要使用Mongo,因为将在Web应用程序中显示的所有数据都来自Facebook api例如,个人资料的帖子,特定帖子上的喜欢的数量等。我不需要有这样的后端,因为如果我将数据存储在Mongo等任何数据存储中,下次用户登录然后数据将是陈旧的(在某种程度上Facebook api是我的后端),我也希望在Facebook上完成的任何帖子的信息更新应该在我的网络应用程序上实时更新,例如如果有人喜欢实际Facebook页面上的帖子,我的网页应用程序上的喜欢的数量也应该实时更新,所以似乎没有必要首先从Facebook SDK中提取数据然后将其存储在Mongo中,为什么不给它到控制器并从那里视图可以呈现数据。如果我的方法有误,请纠正我。

因此,基本上每次用户登录访问令牌时都会创建并用于该会话,当用户注销时,访问令牌被销毁,因此完全无需存储令牌和使用的任何数据Facebook SDK。

2 个答案:

答案 0 :(得分:0)

替换函数调用

 User.findOne({ 'id' : profile.id }, function(err, user) {

使用facebook sdk身份验证调用并在验证后返回用户对象。

  return done(null, user);

请参考......

https://github.com/jaredhanson/passport-facebook

答案 1 :(得分:0)

您需要在模型文件夹中创建新的用户模板。我创建了以下内容:user.js

var  facebook =  module.exports.facebook =       {
    id           : String,
    token        : String,
    email        : String,
    name         : String
}

然后更改passport.serializeUser和passport.deserializeUser函数。

  passport.serializeUser(function(user, done) {
        done(null, user.facebook.id);
    });

    // used to deserialize the user
    //passport.deserializeUser(function(id, done) {
    passport.deserializeUser(function(id, done) {
        done(null, { id: User.facebook.id, token: User.facebook.token, name: User.facebook.name, email: User.facebook.email})

    });

然后函数:process.nextTick(function(){}用这段代码替换内容:

var newUser = User;
                    // set all of the facebook information in our user model
                    newUser.facebook.id    = profile.id; // set the users facebook id                   
                    newUser.facebook.token = token; // we will save the token that facebook provides to the user                    
                    newUser.facebook.name  = profile.name.givenName + ' ' + profile.name.familyName; // look at the passport user profile to see how names are returned
                    newUser.facebook.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first

                    return done(null, newUser);

在函数profileFields: ['id', 'displayName', 'photos', 'emails', 'name']

中添加行passport.use(new FacebookStrategy({}

通过删除本地信息div并更改其他属性<% = user.facebook.id%> to <% = user.id%>等来更改profile.ejs文件。