Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

mongodb token #9

Open
EduardoReolon opened this issue May 11, 2022 · 6 comments
Open

mongodb token #9

EduardoReolon opened this issue May 11, 2022 · 6 comments

Comments

@EduardoReolon
Copy link

EduardoReolon commented May 11, 2022

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

@EduardoReolon
Copy link
Author

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
Copy link
Owner

maxgalbu commented May 30, 2022

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
Copy link

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",
      },

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants