Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 身份验证回调错误_Javascript_Authentication_Routes_Passport.js_Mean Stack - Fatal编程技术网

Javascript 身份验证回调错误

Javascript 身份验证回调错误,javascript,authentication,routes,passport.js,mean-stack,Javascript,Authentication,Routes,Passport.js,Mean Stack,我的网页https://localhost:3000/#/login和https://localhost:3000/#/new工作,https://localhost:3000/#/new有一个按钮,可以通过passportjs登录facebook。以下是相关代码: 在auth.js中: module.exports = { 'facebookAuth': { 'clientID': '1416536398420xxx', 'clientSecret':

我的网页
https://localhost:3000/#/login
https://localhost:3000/#/new
工作,
https://localhost:3000/#/new
有一个按钮,可以通过
passportjs
登录facebook。以下是相关代码:

auth.js
中:

module.exports = {
    'facebookAuth': {
        'clientID': '1416536398420xxx',
        'clientSecret': '...',
        'callbackURL': 'https://localhost:3000/#/auth/facebook/callback'
    }
}
passport.use(new FacebookStrategy({
    clientID: configAuth.facebookAuth.clientID,
    clientSecret: configAuth.facebookAuth.clientSecret,
    callbackURL: configAuth.facebookAuth.callbackURL
},
    function (accessToken, refreshToken, profile, done) {
        process.nextTick(function () {
            User.findOne({ 'facebook.id': profile.id }, function (err, usr) {
                if (err) return done(err);
                if (user) return done(null, user);
                else {
                    var newUser = new User();
                    newUser.facebook.id = profile.id;
                    newUser.facebook.token = accessToken;
                    newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName;
                    newUser.facebook.email = profile.emails[0].value;
                    newUser.save(function (err) {
                        if (err) throw err;
                        return done(null, newUser)
                    })
                }
            })
        })
    }
));
router.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email'] }));

router.get('/auth/facebook/callback',
  passport.authenticate('facebook', { successRedirect: '/login',
                                      failureRedirect: '/login' }));
passport.js
中:

module.exports = {
    'facebookAuth': {
        'clientID': '1416536398420xxx',
        'clientSecret': '...',
        'callbackURL': 'https://localhost:3000/#/auth/facebook/callback'
    }
}
passport.use(new FacebookStrategy({
    clientID: configAuth.facebookAuth.clientID,
    clientSecret: configAuth.facebookAuth.clientSecret,
    callbackURL: configAuth.facebookAuth.callbackURL
},
    function (accessToken, refreshToken, profile, done) {
        process.nextTick(function () {
            User.findOne({ 'facebook.id': profile.id }, function (err, usr) {
                if (err) return done(err);
                if (user) return done(null, user);
                else {
                    var newUser = new User();
                    newUser.facebook.id = profile.id;
                    newUser.facebook.token = accessToken;
                    newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName;
                    newUser.facebook.email = profile.emails[0].value;
                    newUser.save(function (err) {
                        if (err) throw err;
                        return done(null, newUser)
                    })
                }
            })
        })
    }
));
router.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email'] }));

router.get('/auth/facebook/callback',
  passport.authenticate('facebook', { successRedirect: '/login',
                                      failureRedirect: '/login' }));
index.js
中:

module.exports = {
    'facebookAuth': {
        'clientID': '1416536398420xxx',
        'clientSecret': '...',
        'callbackURL': 'https://localhost:3000/#/auth/facebook/callback'
    }
}
passport.use(new FacebookStrategy({
    clientID: configAuth.facebookAuth.clientID,
    clientSecret: configAuth.facebookAuth.clientSecret,
    callbackURL: configAuth.facebookAuth.callbackURL
},
    function (accessToken, refreshToken, profile, done) {
        process.nextTick(function () {
            User.findOne({ 'facebook.id': profile.id }, function (err, usr) {
                if (err) return done(err);
                if (user) return done(null, user);
                else {
                    var newUser = new User();
                    newUser.facebook.id = profile.id;
                    newUser.facebook.token = accessToken;
                    newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName;
                    newUser.facebook.email = profile.emails[0].value;
                    newUser.save(function (err) {
                        if (err) throw err;
                        return done(null, newUser)
                    })
                }
            })
        })
    }
));
router.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email'] }));

router.get('/auth/facebook/callback',
  passport.authenticate('facebook', { successRedirect: '/login',
                                      failureRedirect: '/login' }));
通过点击facebook登录按钮,在输入用户名和密码后,它会重定向到facebook登录页面,地址变为
https://localhost:3000/?code=AQCh9Q9KgZjL3TzTpyMW61gxcNZjN2vEgQAvr5r1k9-WxjwfmVzsPL9Txu-OTKP08MJXYVMJIGET8ZRGHGJLABPG3SSVCGQEM1JDXWJ4YUGL5DMUUU3XM7JZFUOCGUAGULSEFCFx-s62-X6UUPS0D6WWZRAI-NK6gdvudl-JZWBK2O5PTHHHHN8PBBLYTGPSI8VVVKAI55TU6FJYA9V2R7FP 7R2C4KRDHA8PP2A3Z9D9QDPG42CZLZU8PP2ZUKZZZZUKKZZZZZYYKKKZZZZZZZYYYKKKZZZZZLNZLNZLNZLNZZLNZZZF-8iGLQQ0#/auth/facebook/callback

因此,它被挂起;这一页是空白的

有人知道我的路线出了什么问题吗

编辑1:我刚刚添加了
有效的OAuth重定向URI
,但是我得到的url地址仍然是通过
\auth/facebook/callbak
完成的


您的路由器正在URL路径的根目录下查找
/auth/facebook/callback
。我不知道为什么,但facebook将其代码放在URL的根上

按如下方式更改路由器

   router.get(/auth\/facebook\/callback$/,
      passport.authenticate('facebook', { successRedirect: '/login',
                                  failureRedirect: '/login' }));

这就是查询参数(搜索)和哈希的工作方式

哈希参数不会传递给服务器,它对客户端很有用。查询优先于散列

您可以在浏览器中自己尝试。打开任何网页(我在这里假设为localhost),并使用控制台在URL中强制执行查询参数、哈希参数

window.location.hash = 'someHash'
URL将更新为
localhost/#someHash

现在做

window.location.search = 'someQueryParam=value'
请注意,URL更改为
localhost/?someQueryParam=value#someHash

实现这一点的唯一方法是使用不带
符号的路由器。哈希路由器仅用于客户端。

这与Facebook回调URL或passport无关


我希望这能有所帮助。

facebook OAuth设置页面中的“有效OAuth重定向URI”中有什么URL?请查看我的更新…哇,我真的希望这不是你的实际OAuth凭据…@Paul你是什么意思?在你的代码示例中,ClientID和ClientSecret应该是,你知道,机密的。你不应该把它们贴在网上。在auth.js的示例中file@SoftTimur如果答案对你有帮助,请接受。谢谢你的解释。我同意我们需要让我的angularjs应用程序在没有
#
符号的情况下工作。所以它归结为和…相同的问题,我仍然没有让它工作。。。。