【问题标题】:Best way to return token after github authenticationgithub身份验证后返回令牌的最佳方式
【发布时间】:2016-02-24 06:55:18
【问题描述】:

我有一个 Angular 应用程序,它使用 passport-github2 和 oauth2 来验证和访问 github 资源。我正在使用nodejs、koa和passport github,我的服务器代码在github调用的回调中看起来有点像这样:

router.get('/auth/github/callback', function* (next) {
  const ctx = this;

  yield passport.authenticate('github', {
    failureRedirect: '/login'
  }, function* (err, profile, failed) {
    if(!err && profile) {
      const token = jwtHelper(profile);
      ctx.redirect(config.client.syncUrl + '?token=' + token);  

在有角度的前端,我将做这样的事情来获取令牌:

const token = location.query.token

我宁愿在响应标头中发送令牌,但我不知道如何在前端角度代码中拉出标头。

是否可以以这种方式使用响应标头?

【问题讨论】:

  • 您如何为应用程序提供服务?你在用快递吗?
  • 我正在使用koa,我在问题中提到过,如果不清楚,请见谅。

标签: angularjs node.js passport.js


【解决方案1】:

来自koa's documentation

response.set(field, value)

将响应头字段设置为值:

this.set('Cache-Control', 'no-cache');

所以在你的情况下应该是这样的:

ctx.set('Token', token)

【讨论】:

  • 这解释了如何从服务器发送令牌,而不是如何在前端使用它,这是 OP 所要求的。
猜你喜欢
  • 1970-01-01
  • 2018-04-23
  • 1970-01-01
  • 2017-07-05
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
相关资源
最近更新 更多