简体   繁体   English

github身份验证后返回令牌的最佳方法

[英]Best way to return token after github authentication

I have an angular app that is using passport-github2 and oauth2 to both authenticate and gain access to github resources. 我有一个有角度的应用程序,它使用passport-github2和oauth2进行身份验证并获得对github资源的访问权限。 I am using nodejs, koa and passport github and my server code looks a bit like this in the callback that is called by github: 我使用的是nodejs,koa和通行证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);  

In the frontend with angular, I am going to do something like this to get the token: 在带有angular的前端中,我将执行以下操作来获取令牌:

const token = location.query.token

I would rather send the token in a response header but I don't know how to pull the header out in the frontend angular code. 我宁愿在响应标头中发送令牌,但不知道如何在前端角度代码中拉出标头。

Is it possible to work with a response header in this way? 是否可以通过这种方式使用响应标头?

From koa's documentation : koa的文档中

response.set(field, value) response.set(字段,值)

Set response header field to value: 将响应标头字段设置为值:

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

So in your case it should be something like: 因此,在您的情况下,应为:

ctx.set('Token', token)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM