Mean stack application

时间:2017-09-18 07:20:22

标签: node.js express mean-stack passport-facebook

我使用Mean stack开发网站。我想为我使用passport-facebook策略的应用程序添加facebook登录信息。

这是我的app.js文件内容

****
// Setting passport config
var passport = require('passport');
var passportService = 
var passportService=require('./controllers/services/passportservice')
(passport);

// Setting cors parameters
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3001');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, 
OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, 
Content-Type, Accept, Authorization, Access-Control-Allow-
Credentials");
res.header("Access-Control-Allow-Credentials", "true");
next();
});

// Setting route
var facebooksignin = 
require('./controllers/routes/authenticate/facebooksignincontroller')
(passport);
app.use('/facebook',facebooksignin);

这是facebook登录控制器

module.exports = function (passport) {

/**
Description: This method handles the request recieved for facebook signin
Method Type: Get
Return: Successfully authenticated or not
*/
router.get('/auth', function (req, res, next) {
    console.log('Inside facebook get controller');
    res.header('Access-Control-Allow-Origin', 'http://localhost:3001');
    passport.authenticate('facebook', { scope : 'email' })(req, res, next);
});

router.get('/callback',function(req,res,next){

    passport.authenticate('facebook', {
        successRedirect : '/',
        failureRedirect : '/'
    })
});

return router;    

}

这是护照服务代码

module.exports=function(passport){

/**
 * This is for facebook sign in using passport
 */
passport.use(new FacebookStrategy({

    // pull in our app id and secret from our auth.js file
    clientID        : facebookconfig.appId,
    clientSecret    : facebookconfig.appSecret,
    callbackURL     : facebookconfig.callbackUrl
},function(token, refreshToken, profile, done) {

    // asynchronous
    process.nextTick(function() {

        console.log('Fetching email: '+profile.emails[0]);
        // find the user by the email obtained from facebook and check if they exists
        userdao.findUserByEmail(profile.emails[0].value,function(err,user){
            if(err){
                // handle error
            }
            if(user){
                return done(null,user);
            }
            // user does not exists
            else{
                var user={};
                user.email=profile.emails[0].value;
                user.accountType=1;
                userdao.create(user,function(err,u){
                  if(u.status === "success"){
                    return done(null,user);
                  } 
                  else{
                      // failed to create user instance. check why
                    return done(err,null);
                  } 
                })
            }
        })
    });
}));

}

我能够在控制台中看到消息记录。但正在发生的事情是Facebook弹出窗口没有打开并被阻止。 这是我在chrome console中遇到的错误。

 https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Ffacebook%2Fcallback&scope=email&client_id=1975124799431772. Redirect from 'https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Ffacebook%2Fcallback&scope=email&client_id=1234' to 'http://localhost:3000/facebook/callback?code=AQDBsGqN-OmaA1OV8uX35u64hiwcUYdBnNkRJ_3UaSOXqU_71FC7ZP-SMRZeaMwUqz84e0SRdsGyDatWxuBR2pxHXhBdQu49YqhCK92ZMj0bv4NLf9ZvXgSDdqEaC_AKB5Ky0S1MOCfaU_UlqmGCebAvj8ktGY5IvzD_6Ke9nKh0zN08ZK91J74l8ueOJszu-Kxgs5CUZy5wi0cdbseoEAsT6AqQgU9tYSDUBn7miEt-KdlDZkgXTC3-oMelGdbzGI_Z2XYnz-JwMe2rPPDkmBFMqUKjxXcNlSKgr-77e3PtBdfDOw1_nwhnm-_LzlDzLGk#_=_' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

尝试了与cors请求相关的所有事情。不知道我哪里错了。请任何建议都会有很大的帮助

0 个答案:

没有答案