maxgalbu / adonis5-jwt

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

mongodb token #9

Open EduardoReolon opened 2 years ago

EduardoReolon commented 2 years ago

I'm followed this project:

Auth-mongoose-provider

everything worked fine.

but token is still trying to access config/database.ts.

what's the right way of registering the provider for the token?

maxgalbu commented 2 years ago

Hi @EduardoReolon,

yes, it's trying to access database due to this configuration in your config/auth.ts:

tokenProvider: {
  type: 'api',
  driver: 'database',
  table: 'jwt_tokens',
  foreignKey: 'user_id',
}

You need to write a mongodb TokenProvider which you need to implement yourself by looking at one of these files: https://github.com/maxgalbu/adonis5-jwt/tree/master/lib/TokenProviders

Then edit the config/auth.ts to use the new TokenProvider:

tokenProvider: {
  type: 'api',
  driver: 'mongo',
  collection: 'your_tokens_collection',
}

Then import this new TokenProvider here, by adding another else if: https://github.com/maxgalbu/adonis5-jwt/blob/master/providers/JwtProvider.ts#L38-L40

You then need to modify the UserProvider used by the JWT guard, which you can change in config/auth.ts, for example:

provider: {
  driver: "mongo",
  identifierKey: "id",
  uids: [],
  model: () => import("App/Models/YourUserModel"),
}

Not an easy task, I can concede

EduardoReolon commented 2 years ago

I can see. I started doing what u just said but waited if there where a easier way. By now I can keep the tokens in a mysql, because these are unrelated to everything else and I still need mysql in the server. Later on I'll try to pull it to mongoDB as well. Thx.

maxgalbu commented 2 years ago

If you manage to do that, please open a pull request :)

EduardoReolon commented 2 years ago

I made de pull request. Idk if I did everything right. It seams that all worked with Mongo, please check if I haven't messed something up.

I couldn't see how to install the dependency in my project from my git (npm i user/adonis5-jwt, but it didn't create build folder). So let me know if you will accept the changes, if not I'll include this folder in my GitHub.

Thx.

maxgalbu commented 2 years ago

You should be able to use your own fork of the library by building it, committing the build folder (not in the pull request branch please 😄 ) and then do something like this:

"dependencies" : {
    ...
    "adonis5-jwt": "https://github.com/EduardoReolon/adonis5-jwt.git#yourbranch",
    ...
}

then run npm install again

henriquetroiano commented 1 year ago

you can do it with prisma, if you are intended to use mongodb in adonisjs maybe it's the best way.

provider: {
        driver: "prisma",
        identifierKey: "id",
        uids: ["email"],
        model: "user",
      },