You need the id for 'params.id' on the route and I already have the id in auth.user.id? Adonisjs

Asked

Viewed 221 times

0

I have the following question when creating my user’s Routes and Controller.

I see that when I create the route with Route.Source automatically Adonis creates the CRUD routes and passing the ID in the Update/Show/Delete operations, but I don’t need this ID on the route, once I get the ID by auth.user.id id.

How is my creation of the route:

Route.group(() => {
  Route.resource("users", "UserController")
    .apiOnly()
    .except("store")
    .validator(new Map([[["users.update"], ["User/Update"]]]));
}).middleware(["auth"]);

After creating the routes running Adonis route:list it appears that the routes of Show, Update, Destroy need id on the routes.

To what extent is this necessary?
If not, this is how I do not need to pass the ID by parameter on the route?

That’s it at first. I thank you in advance.

1 answer

0


After studies it was understood that since we already have access to the user ID through the authentication middleware, we do not need to go through params on the route.

However, since the Resource() route creation method is automatic, we cannot set so there are no parameters on the routes.

We conclude that to not need to pass the ID on the routes we need to create the routes manually within the

Route.group() 

Thus remaining:

Route.group(() => {
  //Update de usuário
  Route.put("users", "UserController").validator(
    new Map([[["users.update"], ["User/Update"]]])
  );

  //CRUD de documentos
  Route.resource("documents", "DocumentController").apiOnly();

  //CRUD de assinaturas
  Route.resource("signatures", "SignatureController").apiOnly();

  //Update Config
  Route.put("configurations", "ConfigurationController").validator(
    new Map([[["configurations.update"], ["UserConfig/Update"]]])
  );
}).middleware(["auth"]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.