Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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 何时以及为什么在实现GitHub OAuth 2.0时需要Passport.js?_Node.js_Express_Github Api_Github Oauth_Passport Github2 - Fatal编程技术网

Node.js 何时以及为什么在实现GitHub OAuth 2.0时需要Passport.js?

Node.js 何时以及为什么在实现GitHub OAuth 2.0时需要Passport.js?,node.js,express,github-api,github-oauth,passport-github2,Node.js,Express,Github Api,Github Oauth,Passport Github2,我已经使用Express和Axios在Node.js应用程序中集成了GitHub OAuth登录而不使用Passport.js,它工作得非常好。那么什么时候以及为什么需要Passport.js呢 // Import the express library const express = require('express') // Import the axios library, to make HTTP requests const axios = require('axios') //

我已经使用Express和Axios在Node.js应用程序中集成了GitHub OAuth登录而不使用Passport.js,它工作得非常好。那么什么时候以及为什么需要Passport.js呢

// Import the express library
const express = require('express')

// Import the axios library, to make HTTP requests
const axios = require('axios')

// This is the client ID and client secret that you obtained
// while registering the application
const clientID = '' //add your own ID
const clientSecret = '' //add your own secret

// Create a new express application and use
// the express static middleware, to serve all files
// inside the public directory

const app = express()
app.use(express.static(__dirname + '/public'))

app.get('/oauth/redirect', (req, res) => {
  // The req.query object has the query params that
  // were sent to this route. We want the `code` param
  const requestToken = req.query.code
  axios({
    // make a POST request
    method: 'post',
    // to the Github authentication API, with the client ID, client secret
    // and request token
    url: `https://github.com/login/oauth/access_token?client_id=${clientID}&client_secret=${clientSecret}&code=${requestToken}`,
    // Set the content type header, so that we get the response in JSON
    headers: {
      accept: 'application/json'
    }
  }).then((response) => {
    // Once we get the response, extract the access token from
    // the response body
    const accessToken = response.data.access_token
    // redirect the user to the welcome page, along with the access token
    res.redirect(`/welcome.html?access_token=${accessToken}`)
  })
})

// Start the server on port 8080
app.listen(8080, function () {
    console.log('App listening at port: 8080');
});

您已经添加了一个部分,现在添加了另一个需要有效令牌的路由,然后您发现您需要将其提取出来,以便在100多条路由中重复使用,然后一些bright spark需要google/facebook/twitter/etc/etc auth或passport实施的500多条其他策略之一。。然后,您意识到可能已经很好地使用了一个经过尝试和测试的库,比如passport,而不是使用有效的令牌来滚动您自己。您在代码中指的是requestToken吗?还是访问令牌?另外,我是OAuth的新手,所以你能用points解释一下吗?access_token,所以重定向到welcome.html(通过url参数传递,这不应该做,因为它也可能登录到http反向代理日志中)。。但是您需要再次使用它,例如
/profile
/whatnot
,因此如果您将其传递回服务器,您必须在每个受保护的路由中以某种方式验证它(第二部分),然后还必须处理它是否失效。。正如人们可以简单地转到
/welcome.html?访问\u token=123
,它就可以工作了。。基本上,我想说的是,除了简单的登录,还有更多的东西需要验证,passport在一个简单易用的API中处理这些东西,虽然你应该自己开发,弄清楚这一切,但显然不是一个生产应用程序,然后看看如何使用lib,看看它提供了什么,与您在滚动时实现了多少相比好吧,那么您基本上是说,如果我们想在不同的路由中使用GitHub API的数据,那么会话将不会被存储,我们必须为每个路由验证它?您是否有示例代码,以便我更好地理解?因为我花了一天多的时间在这上面,我还没有遇到过他们创建不同路线的代码。