2012-04-11 38 views
8

Tôi đang sử dụng mô-đun hộ chiếu (xác thực github) trong ứng dụng của mình và tôi muốn chuyển hướng tùy theo hành động ... tôi kiểm tra xem đó có phải là đăng nhập bình thường hay không. đăng nhập lần đầu tiên.hộ chiếu: chuyển hướng khác nhau để đăng nhập và đăng ký tài khoản

passport.use(new GitHubStrategy({ 
    clientID: conf.github.app_id, 
    clientSecret: conf.github.app_secret, 
    callbackURL: conf.github.callback_url 
    }, 
    function(accessToken, refreshToken, profile, done) { 
    // asynchronous verification, for effect... 
    process.nextTick(function() { 

     // To keep the example simple, the user's GitHub profile is returned to 
     // represent the logged-in user. In a typical application, you would want 
     // to associate the GitHub account with a user record in your database, 
     // and return that user instead. 

     Models_User.findOrCreateUser(profile, function(msg){ 
     console.log("auth type:" + msg); 
     }); 

     return done(null, profile); 

    }); 
    } 
)); 

chức năng findOrCreateUser của tôi kiểm tra xem đó là một người dùng mới và làm tất cả những hành động db ... để thử nghiệm tôi cho chức năng trả về một biến msg mà chỉ là một chuỗi nói rằng "đăng nhập" hoặc "new_registration ".

vì vậy câu hỏi của tôi là cách "vận chuyển" biến mà tôi nhận được từ findOrCreateUser để tôi có thể chuyển hướng cho phù hợp ("/ welcome" hoặc "/ back_again") sau khi xác thực hộ chiếu.

mã hộ chiếu khác trong ứng dụng của tôi:

// GET /auth/github 
// Use passport.authenticate() as route middleware to authenticate the 
// request. The first step in GitHub authentication will involve redirecting 
// the user to github.com. After authorization, GitHubwill redirect the user 
// back to this application at /auth/github/callback 
app.get('/auth/github', 
    passport.authenticate('github'), 
    //passport.authenticate('github', { scope: ['user', 'public_repo', 'gist'] }), 
    function(req, res){ 
    // The request will be redirected to GitHub for authentication, so this 
    // function will not be called. 
    }); 

// GET /auth/github/callback 
// Use passport.authenticate() as route middleware to authenticate the 
// request. If authentication fails, the user will be redirected back to the 
// login page. Otherwise, the primary route function function will be called, 
// which, in this example, will redirect the user to the home page. 
app.get('/auth/github/callback', 
    passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }), 
    function(req, res) { 
    res.redirect('/'); 
    }); 

Trả lời

9

Trong xác minh callback của bạn, tôi sẽ thay đổi mọi thứ lên để các findOrCreateUser chức năng cung cấp các hồ sơ thực tế để gọi lại, và sau đó vượt qua rằng thông qua để done(), như vậy:

Models_User.findOrCreateUser(profile, function(user){ 
    console.log("auth type:" + msg); 
    return done(null, user); 
}); 

// take this out, use the actual model above 
//return done(null, profile); 

Bây giờ, khi xử lý các URL gọi lại sau khi xác thực, bạn có thể kiểm tra hồ sơ thành viên này và xem nếu nó là mới (tôi giả sử nó có một tài sản isNew đây):

app.get('/auth/github/callback', 
    passport.authenticate('github', { failureRedirect: '/login' }), 
    function(req, res) { 
    // successful auth, user is set at req.user. redirect as necessary. 
    if (req.user.isNew) { return res.redirect('/back_again'); } 
    res.redirect('/welcome'); 
    }); 
+0

cảm ơn bạn, câu trả lời hay! – toxinlabs

+2

@ Jared Hanson, Điều đó cũng làm việc cho tôi. Có trang web hoặc tài liệu nào để tôi có thể tìm hiểu điều này không? –

Các vấn đề liên quan