Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

koa-grant: How to get the user info from session #20

Closed
zhiyelee opened this issue Jun 24, 2015 · 4 comments
Closed

koa-grant: How to get the user info from session #20

zhiyelee opened this issue Jun 24, 2015 · 4 comments

Comments

@zhiyelee
Copy link

I want to find a way to get the user info from the session after login.

For example:

// suppose user have login
router.get('/demo', function *(next) {
  if (!this.session.grant) {
    this.redirect('/login');
  } else {
   // get the user info from DB which I saved after user login successfully,
   //    eg, saved  in `/hanlde_provider_callback
    user = yield getUserInfo(this.session.grant.userId);
  }
}

Seems I can't add a new property to session in the /handle_provider_callback, so I don't how to connect the special session with the db item (I want to store users in my db). Do you have any idea?

Thank you.

@simov
Copy link
Owner

simov commented Jun 25, 2015

I'm not sure that I understand your exact scenario, but here is what you can do - just store the access token somewhere in your database and link it to your user data.

@zhiyelee
Copy link
Author

@simov thank you.

What i want to do is, eg

  • user login with a provider, like github
  • /connect/github/callback // seems grant set session here.
  • /handle_github_callback // I want to fetch and store user info here, for example link session with access token.

After the above step, the login user access /demo page, then how can I get the user info from DB, as far as i known, the this.session only contains

{ 
  grant: { 
    provider: 'github',
    dynamic: {} 
  } 
}

That means i can't find current users access token or any other useful information besides provider.

Maybe a stupid question, thank you for your time.

@simov
Copy link
Owner

simov commented Jun 25, 2015

By default Grant returns the result data from the OAuth flow as a querystring, that's why you see only a meta data in the session that's used internally by Grant during the OAuth flow.

If you want the result data returned in the session instead, you have to set transport:'session' under the server key in your configuration, take a look at the configuration docs

Let me know if that solves your problem.

@zhiyelee
Copy link
Author

@simov That make sense to me. Thank you a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants