Using Persona & Social Auth

I have Persona setup and working nicely, now I need to integrate Social Auth (Google & Facebook). I’ve hooked up Google and it authenticates fine, but I’m not entirely sure how to approach creating a new user in my database. I can’t seem to find any information on this use case.

What I’m unsure of is that when using Persona there’s a password column, but when using Social Auth there is no password, instead we get a token.

My current User schema looks like:

this.create('users', (table) => {
      table.increments()
      table.string('name', 80).notNullable()
      table.string('email', 254).notNullable().unique()
      table.string('password', 60).notNullable()
      table.string('account_status', 60)
      table.string('login_source', 60)
      table.timestamps()
    })

should I be adding a token column and changing the password column so it can be set to null? Or should I be approaching things differently?

1 Like

in database/migrations/ you will find the tokens migration file. Inspect it and you will find your answer there:

this.create('tokens', (table) => {    
  # other columns                                                                                                           
  table.integer('user_id').unsigned().references('id').inTable('users')  
})

Thanks for your reply @begueradj, forgive me, I’m quite new to backend (I’m primarily FE), currently when I social auth, with Facebook for example, it returns a token. Based on the the suggested controller logic in the guide a token is passed to the user, but this token is inserted into my user table.

Are you saying that it should be inserted into the tokens table? That would make sense, but how should I go about doing that?

This is my logic:

async authenticated ({ response, ally, auth }) {
    try {
      const fbUser = await ally.driver('facebook').getUser()

      // user details to be saved
      const userDetails = {
        name: fbUser.getName(),
        email: fbUser.getEmail(),
        token: fbUser.getAccessToken(),
        login_source: 'facebook',
        account_status: 'verified'
      }

      // search for existing user
      const whereClause = {
        email: fbUser.getEmail()
      }

      const user = await User.findOrCreate(whereClause, userDetails)
      const jwt = await auth.generate(user)

      return response
        .ok({ ...jwt, user })
    } catch (err) {
      return response
        .status(err.status)
        .send(err)
    }
  }