Instance flipped for Admin and User models in auth

I have created two models User and Admin
I have implemented the authentication for both of these models.
On routes, I have put

For User:

Route.get("user/secure", "UserController.showSecret").middleware([
    "auth:user"
  ]);

For Admin:

Route.get("/secure", "AdminController.showSecret").middleware([
    "auth:admin"
  ]);

This seems to work fine. However, when I create both user and admin. With the same id, the model gets flipped. If I am logged in as a user, and I enter the admin route, the auth instance is flipped with that of admin instead of restricting me.

1 Like

Seems there isn’t any solution for this. What I did was use this package.

And in the admin model. I set the increments to false

 static get incrementing() {
    return false;
  }
  static get primaryKey() {
    return "id";
  }

And inside migrations, I created an “id” field just with notNullable and unique constraints and type of string.

this.create("admins", table => {
      table
        .string("id")
        .notNullable()
        .unique();

And then when creating an admin user. I make use of the uuid library to set the id to.

adminData.id = uuid();
    const admin = await Admin.create(adminData);

This works, and I guess it is one possible way to deal with this bug.

1 Like