maxgalbu / adonis5-jwt

JWT Authentication service for Adonisjs v5
MIT License
86 stars 15 forks source link

Invalid auth config, missing "provider" or "provider.driver" property #12

Closed saminanirmala closed 2 years ago

saminanirmala commented 2 years ago

I am getting this error and don't know how to resolve this error since my contract auth.ts looks like this

/**
 * Contract source: https://git.io/JvyKD
 *
 * Feel free to let us know via PR, if you find something broken in this
 * file.
 */

import CmsUser from 'App/Models/System/CmsUser'
import { JWTGuardConfig, JWTGuardContract } from "@ioc:Adonis/Addons/Jwt";

import ApiUsers from 'App/Models/Api/ApiUser'

declare module '@ioc:Adonis/Addons/Auth' {
  /*
  |--------------------------------------------------------------------------
  | Providers
  |--------------------------------------------------------------------------
  |
  | The providers are used to fetch users. The Auth module comes pre-bundled
  | with two providers that are `Lucid` and `Database`. Both uses database
  | to fetch user details.
  |
  | You can also create and register your own custom providers.
  |
  */
  interface ProvidersList {
    /*
    |--------------------------------------------------------------------------
    | User Provider
    |--------------------------------------------------------------------------
    |
    | The following provider uses Lucid models as a driver for fetching user
    | details from the database for authentication.
    |
    | You can create multiple providers using the same underlying driver with
    | different Lucid models.
    |
    */
    user: {
      implementation: LucidProviderContract<typeof CmsUser>,
      config: LucidProviderConfig<typeof CmsUser>,
    }
    user_using_database: {
      implementation: DatabaseProviderContract<ApiUsers>,
      config: DatabaseProviderConfig,
    };
  }

  /*
  |--------------------------------------------------------------------------
  | Guards
  |--------------------------------------------------------------------------
  |
  | The guards are used for authenticating users using different drivers.
  | The auth module comes with 4 different guards.
  |
  | - SessionGuardContract
  | - BasicAuthGuardContract
  | - JwtGuardContract
  | - OATGuardContract ( Opaque access token )
  |
  | Every guard needs a provider for looking up users from the database.
  |
  */
  interface GuardsList {
    /*
    |--------------------------------------------------------------------------
    | Web Guard
    |--------------------------------------------------------------------------
    |
    | The web guard uses sessions for maintaining user login state. It uses
    | the `user` provider for fetching user details.
    |
    */
    web: {
      implementation: SessionGuardContract<'user', 'web'>,
      config: SessionGuardConfig<'user'>,
    }
    jwt: {
      implementation: JWTGuardContract<'user_using_database', 'api'>,
      config: JWTGuardConfig<'user_using_database'>,
    };
  }
}

and my config/auth.ts looks like this

/**
 * Config source: https://git.io/JvyKy
 *
 * Feel free to let us know via PR, if you find something broken in this config
 * file.
 */

import { AuthConfig } from '@ioc:Adonis/Addons/Auth'

/*
|--------------------------------------------------------------------------
| Authentication Mapping
|--------------------------------------------------------------------------
|
| List of available authentication mapping. You must first define them
| inside the `contracts/auth.ts` file before mentioning them here.
|
*/
const authConfig: AuthConfig = {
  guard: 'web',
  list: {
    /*
    |--------------------------------------------------------------------------
    | Web Guard
    |--------------------------------------------------------------------------
    |
    | Web guard uses classic old school sessions for authenticating users.
    | If you are building a standard web application, it is recommended to
    | use web guard with session driver
    |
    */
    web: {
      driver: 'session',

      provider: {
        /*
        |--------------------------------------------------------------------------
        | Driver
        |--------------------------------------------------------------------------
        |
        | Name of the driver
        |
        */
        driver: 'lucid',

        /*
        |--------------------------------------------------------------------------
        | Identifier key
        |--------------------------------------------------------------------------
        |
        | The identifier key is the unique key on the model. In most cases specifying
        | the primary key is the right choice.
        |
        */
        identifierKey: 'id',

        /*
        |--------------------------------------------------------------------------
        | Uids
        |--------------------------------------------------------------------------
        |
        | Uids are used to search a user against one of the mentioned columns. During
        | login, the auth module will search the user mentioned value against one
        | of the mentioned columns to find their user record.
        |
        */
        uids: ['email'],

        /*
        |--------------------------------------------------------------------------
        | Model
        |--------------------------------------------------------------------------
        |
        | The model to use for fetching or finding users. The model is imported
        | lazily since the config files are read way earlier in the lifecycle
        | of booting the app and the models may not be in a usable state at
        | that time.
        |
        */
        model: () => import('App/Models/System/CmsUser'),
      },
    },
    jwt: {
      serializer: 'lucid',
      model: 'App/Models/ApiUser',
      scheme: 'jwt',
      uid: 'email',
      password: 'password',
      options: {
        secret: 'sP5j9XnCe1oBU44dsHWV3iajXnTxghF'
      }
    },

  },

}

export default authConfig

Please help me out

maxgalbu commented 2 years ago

You're missing quite a chunk of config from your config/auth.ts. Looks like you copied from adonis4 auth?

It should look like this:

jwt: {
    driver: "jwt",
    publicKey: Env.get('JWT_PUBLIC_KEY', '').replace(/\\n/g, '\n'),
    privateKey: Env.get('JWT_PRIVATE_KEY', '').replace(/\\n/g, '\n'),
    persistJwt: false,
    jwtDefaultExpire: '10m',
    refreshTokenDefaultExpire: '10d',
    tokenProvider: {
        type: 'api',
        driver: 'database',
        table: 'jwt_tokens',
        foreignKey: 'user_id',
    },
    provider: {
        driver: "lucid",
        identifierKey: "id",
        uids: [],
        model: () => import('App/Models/User'),
    },
},

Try to run node ace configure adonis5-jwt again