JWT Won't Generate Token


#1

I’m Migrating everything from Adonis 3.2 to Adonis 4.1. Here is everything I’ve coded to get to this point.

SessionController.js

    'use strict'

const User = use('App/Models/Pid')
const E = require('node-exceptions')
const Hash = use('Hash')

class SessionController {

  async store ({ request, response }) {
    const { username: email, password } = request.all()

      const user = await User.findBy('email', email)
      const passwordValid = await Hash.verify(password, user.password)

      if (!passwordValid) {
        throw new E()
      }

      const token = await request.auth.generate(user)
      response.status(201).json({ token })

  }
}

module.exports = SessionController

Models/Token.js

'use strict'

const Model = use('Model')

class Token extends Model {


  Pid() {
    return this.belongsTo('App/Models/Pid')
  }


}

module.exports = Token

app/config/auth.js

'use strict'
const Config = use('Config')

module.exports = {
  /*
  |--------------------------------------------------------------------------
  | Authenticator
  |--------------------------------------------------------------------------
  |
  | Authentication is a combination of serializer and scheme with extra
  | config to define on how to authenticate a user.
  |
  | Available Schemes - basic, session, jwt, api
  | Available Serializers - lucid, database
  |
  */
  authenticator: 'jwt',

  /*
  |--------------------------------------------------------------------------
  | Session
  |--------------------------------------------------------------------------
  |
  | Session authenticator makes use of sessions to authenticate a user.
  | Session authentication is always persistent.
  |
  */
  session: {
    serializer: 'lucid',
    model: 'App/Models/User',
    scheme: 'session',
    uid: 'email',
    password: 'password'
  },

  /*
  |--------------------------------------------------------------------------
  | Basic Auth
  |--------------------------------------------------------------------------
  |
  | The basic auth authenticator uses basic auth header to authenticate a
  | user.
  |
  | NOTE:
  | This scheme is not persistent and users are supposed to pass
  | login credentials on each request.
  |
  */
  basic: {
    serializer: 'lucid',
    model: 'App/Models/User',
    scheme: 'basic',
    uid: 'email',
    password: 'password'
  },

  /*
  |--------------------------------------------------------------------------
  | Jwt
  |--------------------------------------------------------------------------
  |
  | The jwt authenticator works by passing a jwt token on each HTTP request
  | via HTTP `Authorization` header.
  |
  */
  jwt: {
    serializer: 'lucid',
    model: 'App/Models/Pid',
    scheme: 'jwt',
    uid: 'email',
    password: 'password',
    options: {
     secret: Config.get('app.appKey'),
     // Options to be used while generating token
   }
  },
}

start/routes.js

Route.post('/api/token-auth', 'SessionController.store')

The error that I’m currently running into:

Any help is much appreciated.


#2

auth is not part of the request object, and instead is the root key on the context object that is passed into the controller.

In other words:

async store ({ request, response, auth }) {
  auth.generate(...)
}

#3

ok. @moltar i will remove request and include auth at the top… like this?:

'use strict'

const User = use('App/Models/Pid')
const E = require('node-exceptions')
const Hash = use('Hash')

class SessionController {

  async store ({ request, response, auth }) {
    const { username: email, password } = request.all()

      const user = await User.findBy('email', email)
      const passwordValid = await Hash.verify(password, user.password)

      if (!passwordValid) {
        throw new E()
      }

      const token = await auth.generate(user)
      response.status(201).json({ token })

  }
}

module.exports = SessionController

#4

Sure, that looks about right


#5

it worked!!! now I’m moving on to user controller. thank you for your help!!


#6

Also make sure to read the upgrade guide. http://adonisjs.com/docs/4.0/upgrade-guide