Auth.generate(user) is not working

I use postman to test API, but post response status 500,

AuthencationController.js

"use strict";

const User = use("App/Models/User");
const Hash = use("Hash");

class AuthencationController {
  async store({ request, response, auth }) {
    const { email, password } = request.all();

    console.log(email, password);

    const user = await User.query()
      .where("email", email)
      .first();
    console.log(user);
    const passwordValid = await Hash.verify(password, user.password);

    if (!passwordValid) {
      return response.status(403).json({
        message: "Password not valid",
        user
      });
    }

    const token = await auth.generate(user);

    return response.status(200).json({
      message: "Successly created token",
      token
    });
  }
}

module.exports = AuthencationController;

Models/Token

'use strict'

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class Token extends Model {
}

module.exports = Token

app/config/auth.js

'use strict'

/** @type {import('@adonisjs/framework/src/Env')} */
const Env = use('Env')

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: 'session',

  /*
  |--------------------------------------------------------------------------
  | 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/User',
    scheme: 'jwt',
    uid: 'email',
    password: 'password',
    options: {
      secret: Env.get('APP_KEY')
    }
  },

  /*
  |--------------------------------------------------------------------------
  | Api
  |--------------------------------------------------------------------------
  |
  | The Api scheme makes use of API personal tokens to authenticate a user.
  |
  */
  api: {
    serializer: 'lucid',
    model: 'App/Models/User',
    scheme: 'api',
    uid: 'email',
    password: 'password'
  },

}

start/routes.js

  Route.post("/token", "Api/AuthencationController.store");

When i console.log(user) and console.log(passwordValid) then i have user and passwordValid is true if password correct but i console.log(token) and not anything . Any help is much appreciated.

Could not see any errors in the first pass. Do you have any error message (from header and such)?

Instead of -

… did you also try auth.attempt?

    const token = await auth.attempt(email, password);

https://adonisjs.com/docs/4.1/authentication#_authenticators

I had tried it but it return object and have not token. I don’t know why, i think because I added some assets inside file Models/User.

Hi @Buihongson

auth.generate() is JWT authentication method. Your config file uses authenticator: 'session', not jwt =)

1 Like

I will try it. Thanks you very much.