簡體   English   中英

Passport Google策略 - passport.use回調何時運行? callbackURL?

[英]Passport Google strategy - when does the passport.use callback run? The callbackURL?

我正在使用Passport並致力於Google OAuth 2策略 我無法理解流程。

以下是官方文檔中的代碼:

var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

// Use the GoogleStrategy within Passport.
//   Strategies in Passport require a `verify` function, which accept
//   credentials (in this case, an accessToken, refreshToken, and Google
//   profile), and invoke a callback with a user object.
passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://www.example.com/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, done) {
       User.findOrCreate({ googleId: profile.id }, function (err, user) {
         return done(err, user);
       });
  }
));

然后這個路線:

// GET /auth/google
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  The first step in Google authentication will involve
//   redirecting the user to google.com.  After authorization, Google
//   will redirect the user back to this application at /auth/google/callback
app.get('/auth/google',
  passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login'] }));

// GET /auth/google/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/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });

這是我對流程的理解。 用戶點擊一個按鈕,在瀏覽器上使用谷歌登錄。 請求發送到/auth/google 哪個叫passport.authenticate 從那里開始,谷歌處理一些東西,一旦用戶允許我們訪問他們的谷歌帳戶,那么這就是我不理解的部分。

下一步是什么? Google似乎會向我們提供的callbackURL發送回復。 但在那種情況下,什么時候對passport.use的回調運行? 該回調是接受accessTokenrefreshTokenprofile ,所以看起來它是接收來自Google的響應,而不是callbackURL

在您的應用程序中,當用戶首次允許您通過Google 對其帳戶進行身份驗證時,需要考慮兩種可能的“流量”:

  • 用戶已在您的系統中擁有現有帳戶,並希望將該帳戶與其Google身份相關聯。
  • 用戶正在您的系統中創建一個新帳戶,並為您提供了使用其Google身份創建新帳戶的選項

如果用戶選擇使用Google進行身份驗證,則最終結果是您的數據庫中包含Google的身份驗證服務響應提供的其他信息的記錄。 數據與護照文檔中的此頁面類似。

帶有令牌的對象的第一個參數的passport.use(...)調用是護照用於訪問Google系統的標准(標准OAUTH2憑證)。 第二個參數是與系統數據層交互的回調函數,例如: User.findOrCreate(data)

在將來訪問您的應用程序時,用戶可以利用其帳戶與其Google身份“關聯”並具有簡單登錄體驗的事實。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM