简体   繁体   English

Passport.JS - 没有会话授权?

[英]Passport.JS - authorize with no session?

How to use authorization (binding to existing account) if no session is being used? 如果没有使用会话,如何使用授权(绑定到现有帐户)? I cannot find a way know which user wanted to authorize in the first place after authorization response comes back from facebook. 在授权响应从facebook返回后,我找不到知道哪个用户想要授权的方法。 With no session it is impossible to tell that... token is no longer present in the request (neither the user). 没有会话就不可能告诉...令牌不再出现在请求中(用户都不会)。 Normally they would take that information from session. 通常他们会从会话中获取该信息。 Tried continuation local storage but it won't work as passport makes several internal callbacks which I cannot bind to current context. 尝试继续本地存储,但它不会工作,因为护照会进行几个内部回调,我无法绑定到当前上下文。

http://passportjs.org/docs/authorize http://passportjs.org/docs/authorize

as you specified you are using Passport.JS with Facebook strategy, you can use the state field. 如您所指定的,您正在使用带有Facebook策略的Passport.JS,您可以使用州字段。 you will need to create 2 middlewares, one to be used before you call the authenticate for the login URI, and another to be used before the loginCallback URI. 您需要创建2个中间件,一个在调用登录URI的身份验证之前使用,另一个在loginCallback URI之前使用。

Keep in mind that you will need to also update your strategy to manage the users properly. 请注意,您还需要更新策略以正确管理用户。 remember, you cannot use 'req.user, 记住,你不能使用'req.user,

I personally dont like the sessions too, but its very common to find info about how to use auth with them. 我个人也不喜欢这些会话,但是很常见的是找到有关如何使用auth的信息。

Please check how this method is calling the 'authenticate'passport method disabling the session and is also in a middleware fashion, you can of course do this in-line with you router.get(...) but I found it a good practice as in my app I use local, facebook and JWT auth simultaneously and I like to have a consistent pattern. 请检查这种方法如何调用'authenticate'passport方法禁用会话,并且也是中间件方式,你当然可以与你的router.get(...)一起做这个,但我觉得这是一个很好的做法在我的应用程序中,我同时使用本地,脸书和JWT身份验证,我喜欢有一致的模式。 Also, if you plan to create and save the token. 此外,如果您计划创建并保存令牌。 you may run into troubles handling the callback if you happen to use asynchronous storage, like mongoDB. 如果您碰巧使用异步存储(例如mongoDB),则可能会遇到处理回调的麻烦。

According to the documentation you should use authorize if you will use that endopoint to connect to a local account, but as you wont have sessions at all, it makes no sense to use, never the less I think you should have 2 paths, one for registering with facebook, and another if you plan to merge a local account with facebook, in wich case the token passed to facebook can determine if its first time or connect) 根据您应该使用的文档授权,如果您将使用该endopoint连接到本地帐户,但由于您根本没有会话,使用没有任何意义,我认为您应该有2条路径,一条用于如果您计划将本地帐户与Facebook合并,则可以在Facebook上注册另一个,如果传递给Facebook的令牌可以确定是否第一次或连接)

Do not forget to disable the session in both authenticate calls. 不要忘记在两次身份验证调用中禁用会话。

function facebookTempTokenCreate(req, res, next) {
// create the token the way you think is best, 
// I personally prefer to create a JWT and save it somewhere in the DB.
// also you need to pass it to the next middleware by saving the value 
// in the req. name can be anything you want. 
// but make sure is quite unique as you do not want to break other libraries.

   req.facebookConnectToken = 'that_sweet_token';
   next(); // dont forget to continue to next middleware.
}

function facebookAuthenticate(req, res, next) {        
    passport.authenticate('facebook', { 
        scope : 'email',
        callbackURL : 'your_app_callback_uri',
        state : req.facebookConnectToken,
        session : false
    })(req,res,next);
}

function facebookAuthenticateCallback(req, res, next) {

    passport.authenticate('facebook', { 
        scope : 'email',
        session : false
    })(req,res,next);
}

function facebookTempTokenValidate(req, res, next) {    
    console.log('here we validate the token: ' + req.query.state );
    next();//dont forget to pass execution to next middleware.
}

app.get('/facebook/login', facebookTempTokenCreate, facebookAuthenticate);

app.get('/facebook/login/callback', facebookTempTokenValidate, facebookAuthenticateCallback);

You can find further info in this link. 您可以在此链接中找到更多信息。 https://github.com/jaredhanson/passport-facebook/issues/14 https://github.com/jaredhanson/passport-facebook/issues/14

Hope this can help you. 希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM