【问题标题】:Using Google function with Node-express-passport将 Google 功能与 Node-express-passport 一起使用
【发布时间】:2019-06-03 03:27:30
【问题描述】:

我使用了护照推特策略,碰巧在我执行策略时我无法在客户端看到护照登录窗口

这就是我最初正在做的事情

class Auth extends ParentClass {
  constructor(context, options) {
    super() 
    this.app.get('/callback/:service', (req, res) => 
    this.callbackEndpoint(req,res))
    this.app.get('/', (req, res, next) => this.loginEndpoint(req, res, next))
}

  async loginEndpoint (req, res, next) {
      if (req.query.Twitter) {
        console.log(`Inside Twitter Authentication`)
        passport.authenticate('twitter', { scope : ['email'] })(req,res,next);
      }

}

在我的 ParentClass 中,我或多或少地初始化了一些东西

class ParentClass {
   this.use(corsMiddleware(options.CORS_URLS))
    this.use(bodyParser.json())
    this.use(session({
      secret: 'keyboard cat',
      resave: false,
      saveUninitialized: true,
      cookie: { secure: true }
    }))
    this.use(passport.initialize())
    this.use((passport.session()))
}

use (middleware) {
    this.app.use(middleware)
  }
}

最后,这是我的护照策略

passport.use(new TwitterStrategy({
    consumerKey: functions.config().twitterCredentials.apikey,
    consumerSecret: functions.config().twitterCredentials.apiSecretKey,
    callbackURL: redirect_uri,
    passReqToCallback: true
  }, async (req, accessToken, refreshToken, params, profile, done) => {
      console.log(`logging from Passport Twitter ${req.workspace, req.accessToken}`)
        done()
}))

从第一个代码中,sn-p 我看到了日志Inside Twitter Authentication,但此后没有任何反应。

我期待什么?显示一个窗口让我登录推特,但什么也没有显示,过了一段时间我在控制台信息中得到了这个:Execution took 569847 ms, finished with status: 'timeout'

以下是我的问题: 1.我们可以使用谷歌功能的护照和正常的oauth流程吗? (如果没有回答就评论) 2. 如果是的话,我们可以在我上面的代码中发现错误吗?

更新说明:在谷歌搜索我的问题时,我偶然发现了这个老问题,并意识到虽然它有点相似,但对前一个问题的描述非常模糊。我今天晚些时候问了一个类似的问题,但决定将它与这个问题合并。

【问题讨论】:

    标签: node.js firebase express google-cloud-functions passport.js


    【解决方案1】:

    在代码的第一部分,您需要将passport.authenticate 直接传递给get 处理程序。像这样:

    class Auth extends ParentClass {
      constructor(context, options) {
        super() 
        this.app.get('/callback/:service', (req, res) => 
        this.callbackEndpoint(req,res))
        this.app.get('/', passport.authenticate('twitter', { scope : ['email'] })(req,res,next))
      }
    }
    
    

    我假设这个“应用程序”安装在某个路径,例如 /auth/twitter

    在您的前端,您打开一个弹出窗口,其中的 URL 指向 /auth/twitter。像这样的:

    <button onclick="oauthPrompt('twitter')">Login with Twitter</button>
    <script>
        function oauthPrompt(service) {
          const url = '/auth/' + service
          var newWindow = window.open(url, 'name', 'height=600,width=450')
          if (window.focus) {
            newWindow.focus();
          }
        }
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 2021-12-10
      • 2022-10-05
      • 2013-04-23
      相关资源
      最近更新 更多