Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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
Node.js Passport战略在幕后如何运作_Node.js_Passport.js - Fatal编程技术网

Node.js Passport战略在幕后如何运作

Node.js Passport战略在幕后如何运作,node.js,passport.js,Node.js,Passport.js,我试图理解passport策略是如何运作的 考虑一下我用来进行身份验证的这些api路由 router.get("/google", passport.authenticate('google', { scope: ['profile', 'email'] })); router.get("/google/callback", passport.authenticate('google'), (req, res) => { res.redirect("http://lo

我试图理解passport策略是如何运作的

考虑一下我用来进行身份验证的这些api路由

  router.get("/google",  passport.authenticate('google', { scope: ['profile', 'email'] }));
  router.get("/google/callback", passport.authenticate('google'), (req, res) => {
      res.redirect("http://localhost:3000/")
  })
这就是护照策略

const passport = require('passport')
const GoogleStratergy = require('passport-google-oauth20')
const keys = require("./key.js")
const User = require("../models/user-model.js")

passport.serializeUser((user, done) => {
    done(null, user.id) 
})

passport.deserializeUser((id, done) => {
    User.findById(id).then((user) => {
        done(null, user) //pass it in req of our routes
    })
})

passport.use(
    new GoogleStratergy({
    //Options for the stratergy 
        callbackURL: "/auth/google/callback", 
        clientID: keys.google.clientID,
        clientSecret: keys.google.clientSecret
    }, (accessToken, refreshToken, profile, done) => {


        User.findOne({userId: profile.id }).then((currentUser) => {
            if (currentUser) {
                done(null, currentUser)
            } else {
                    //Changing Image String
                    let  oldURL=  profile.photos[0]["value"]
                    let newURL =  oldURL.substr(0,  oldURL.length-2);
                    newURL = newURL + "250"
                //Creating Mongoose Database
                    new User({
                        username: profile.displayName,
                        userId: profile.id,
                        image: newURL,
                        email: profile.emails[0]["value"]
                    }).save().then((newUser) => {
                        console.log("new user created", newUser)
                        done(null, newUser)
                   })
            }

        })

    })
)
现在,我想我明白这里发生了什么,但有一件事我无法理解,那就是

怎么样

passport.use(
    new GoogleStratergy({
    //Options for the stratergy 
在这里被叫?我的意思是我没有看到任何导出语句,那么它是如何与out Node应用程序链接的呢?或者passport如何在幕后知道我们谷歌战略的位置**


还有,我只是想确认一下,在我们通过护照后。用什么?当您
require
passport时,它将进行序列化?

,您将得到一个单例实例,即它是在您
第一次require
passport时构造的,并且随后在
require
d的任何地方都被重用

因此,您不需要在模块之间共享实例,即不需要导出。您在实例上执行的任何配置都可以在您需要的任何地方看到

NodeJS中还有其他类似的对象,一个突出的例子是
express
app实例

这是护照,你可以在这里核实一下