lundi 12 avril 2021

What ID do I use to create user in the backend when using Google Oauth2 's implicit grant login?

I am a Front End Developer with limited experience setting up backends.

I'm building a simple Ember application (with one single route) and I want to use Google's Login button.

When someone logs in the first time I want to create a record of them in the backend. From there on, every time they log back in (same gmail address), I want to be able to query/update their record in the backend.

  1. When the user clicks to log in with Google, Google returns me an id_token which is the one I'm supposed to use in the Backend.
  2. I then create/authenticate the session for that user by using ember-simple-auth:
this.session.authenticate('authenticator:oauth2-implicit-grant', {
  access_token: id_token // the one received by Google
})
  1. Then I get the access_token generated by ember-simple-auth to find that user in the backend (or create them if this is a new user):
let { access_token }  = this.session.data.authenticated // ember-simple-auth
let user = await this.store.findRecord('user', access_token)
  1. And then in the backend I'm doing:
this.get('/users/:access_token', ({ users }, { params }) => {
  let user = users.find(*params.access_token or something else?*)
  
  // is the user doesn't exist yet I create them
  if (!user) {
    // I realised that if I use params.access_token
    // this will change in the future for the same Gmail account
    // so what identifier do I need to use to create the user the 1st time so that it's permanent ? 
    user = users.create({ id: *params.access_token or something else?* })
  }

  return new Response(200, {}, { user })
})

My question is about which identifier to use in the backend route handler you see above, in order to create the user and then query/update their record during future visits.

Thank you in advance for your help!

EDIT (thinking it about it a bit more): is the access_token only supposed to be used by ember-simple-auth and not by my backend/database at all? In which case is it google's id_token that I should use in the backend ? Is the id_token I received bound to that email address permanently?




Aucun commentaire:

Enregistrer un commentaire