2012-03-27 21 views
17

私はExpressとPassport OpenIDのGoogle戦略を使用しています。認証を開始したページに戻るためには、各認証要求に対してreturnURLを設定したいと思います。Node.jsのカスタムreturnUrl PassportのGoogle戦略

状況は、私はNode.jsバックエンド(および社会的なものとエディタとポータルと拡張子... https://github.com/bubersson/humla)でHTML5スライドアプリケーションを持っているので、スライドメニューでユーザーにログインできるようにしたい...)しかし、私は彼が同じスライドに簡単に戻るようにしたい。

だから私はこのようなものが必要でしょうか?

app.get('/auth/google', function(req,res) { 
    var cust = "http://localhost:1338/"+req.params.xxx; 
    passport.authenticate('google', returnURL:cust, function ... 
} 

私はPassportのガイドを読んだことがありますが、その方法はまだ分かりません。これは安全ではないことは分かっていますが、どうすればそれをやることができますか?

ログインを開始したページに戻るようにアプリケーションを設定するにはどうすればよいですか?または、AJAXを使用してOpenID認証を行う方法はありますか(そして、依然としてパスポートを使用できるようになりますか)。

答えて

24

のためにコールバックでres.redirect('back');を試してみてください、私はGoogleStrategyが非常に類似していることを確信しています。こののバリエーションを試してみてください。

app.get('/auth/twitter/callback', 
    passport.authenticate('twitter', { 
     successRedirect: authenticationRedirect(req, '/account') 
    , failureRedirect: '/' 
    }) 
); 

ただ、これにそのブロックを変更します:

app.get('/auth/twitter/callback', function(req, res, next){ 
    passport.authenticate('twitter', function(err, user, info){ 
    // This is the default destination upon successful login. 
    var redirectUrl = '/account'; 

    if (err) { return next(err); } 
    if (!user) { return res.redirect('/'); } 

    // If we have previously stored a redirectUrl, use that, 
    // otherwise, use the default. 
    if (req.session.redirectUrl) { 
     redirectUrl = req.session.redirectUrl; 
     req.session.redirectUrl = null; 
    } 
    req.logIn(user, function(err){ 
     if (err) { return next(err); } 
    }); 
    res.redirect(redirectUrl); 
    })(req, res, next); 
}); 
あなたがそう(パスポートガイドから)のような認証サービスからのコールバックのためのルートを定義していると仮定すると、

さて、このようにセッションでは、元のURLを保存するために認証されたルートのミドルウェアを定義します。

ensureAuthenticated = function (req, res, next) { 
    if (req.isAuthenticated()) { return next(); } 

    // If the user is not authenticated, then we will start the authentication 
    // process. Before we do, let's store this originally requested URL in the 
    // session so we know where to return the user later. 

    req.session.redirectUrl = req.url; 

    // Resume normal authentication... 

    logger.info('User is not authenticated.'); 
    req.flash("warn", "You must be logged-in to do that."); 
    res.redirect('/'); 
} 

作品!

+0

これは完全に動作しますが、リダイレクト時にユーザーが認証されて戻ってくると、ハッシュパラメータが不足しているように見えます。ログイン後にリダイレクトするときにURLにそれらを保持する方法がありますか? http://stackoverflow.com/questions/14124932/passport-js-express-js-forward-user-to-original-destination-after-authenticati – prototype

+0

@ user645715おそらく、 'req.session.redirectUrl = req.originalUrl ; '代わりにまたは' req.url' –

+0

は魅力のように動作します、ありがとう:) – Zub

5

が、私は私のアプリのTwitterの認証のためにこれを考え出したpassport.authenticate

+0

ハありがとう!明らかに、バックルートは最初と2番目のOpenID要求の間に設定されています。私はそれを知らなかった!ありがとう – bubersson

+2

これで問題が見つかりました。私が既にGoogleを使用してログインしている場合にのみ機能します。パスポートがGoogleページにリダイレクトされてから自分のサイトに戻った場合、「ページ」ルートは失われます。 Googleから自分の場所(ログインが開始されたページ)に強制的に戻る別の方法はありますか? – bubersson

+0

@bubersson私が正しく理解していれば、最初の答えのようなより積極的なアプローチをとる必要があります。 – matanster

3

著者によれば、これはOpenID戦略では不可能です。私たちは、直接変数にアクセスすることで、動的にこれらを更新するために管理:

app.get('/auth/google', function(req, res, next) { 
    passport._strategies['google']._relyingParty.returnUrl = 'http://localhost:3000/test'; 
    passport._strategies['google']._relyingParty.realm = 'http://localhost:3000'; 
    passport.authenticate('google')(req, res, next); 
}); 
13

あなたのログインボタンを持ってどこ、 クエリパラメータ(あなたが使用してどのようなテンプレートシステムを調整)としてリクエストの現在のURLを追加します。

<a href='/auth/google?redirect=<%= req.url %>'>Log In</a> 

その後、 req.sessionにこの値を格納し、あなたのGET /auth/googleハンドラにミドルウェアを追加します。

app.get('/auth/google', function(req, res, next) { 
    req.session.redirect = req.query.redirect; 
    next(); 
}, passport.authenticate('google')); 

最後に、あなたのコールバックハンドラでは、セッションに保存されているURLにリダイレクト:

app.get('/auth/google/callback', passport.authenticate('google', 
    failureRedirect: '/' 
), function (req, res) { 
    res.redirect(req.session.redirect || '/'); 
    delete req.session.redirect; 
}); 
+0

セッションなしでこれを行うことは可能ですか? – nahtnam

関連する問題