2

I've used this to setup auth in strapi and nuxt: Auth with Strapi and Nuxt

I'm currently trying to retrieve the items specific to a authenticated user (already checked out this strapi - restrict user to fetch only data related to him). To do this I created a custom route in Strapi (/api/routine/config/routes.json):

{
  "method": "GET",
  "path": "/routines/me",
  "handler": "Routine.me",
  "config": {
    "policies": []
  }
}

and a custom controller (/api/controllers/Routine.js):

module.exports = {
  me: async (ctx) => {
    const user = ctx.state.user;
    if (!user) {
      return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
    }

    const data = await strapi.services.routine.find({user:user.id});  

    if(!data){
      return ctx.notFound();
    }

    ctx.send(data);
  },
};

I already gave permission through Strapi admin for authenticated users to access 'me'. When I hit the endpoint from Nuxt:

const routines = await axios.get(http://localhost:1337/routines/me)

I get this error:

GET http://localhost:1337/routines/me 404 (Not Found)

Why is the custom route not found? Am I using the wrong endpoint?

joedoesnotknow
  • 73
  • 1
  • 2
  • 8

2 Answers2

1

Maybe you have already solved it, but it seems like you forget to send the authentication header with the request.

    const routines = await axios.get(
        'http://localhost:1337/routines/me', {
            headers: {
                Authorization:
                this.$auth.getToken('local'),
            },
        }
user3195845
  • 401
  • 1
  • 5
  • 14
  • Thanks @user3195845, I was missing the header! I'm getting a 403 forbidden error now though: GET http://localhost:1337/routines/me 403 (Forbidden). Does this have to do with my Strapi configuration? I updated the code with your tip, except I had to use context to access the auth because I'm using asyncData(): 'Authorization': context.app.$auth.getToken('local') – joedoesnotknow Dec 04 '20 at 10:34
  • 1
    Happy to help :) It seems like you are not authenticated? Are you sure that the token is passed with the request? What happens if you remove the first if statement if(!user) are you getting any results? – user3195845 Dec 04 '20 at 13:28
  • It gives the same result: 403 (forbidden). It seems that I am authenticated, if I log this.$auth.getToken('local') before sending the request it does print out a bearer token. I think you're right and somehow the headers aren't properly sent with the request. Do you have any idea why? – joedoesnotknow Dec 04 '20 at 14:29
0

It was a fault in my Strapi routes config. Answer was provided through the amazingly helpful Strapi forums: 403 forbidden when calling custom controller from Nuxt

Here is the problem:

{
  "method": "GET",
  "path": "/routines/:id",
  "handler": "routine.findOne",
  "config": {
    "policies": []
  }
},
{
  "method": "GET",
  "path": "/routines/me",
  "handler": "routine.me",
  "config": {
    "policies": []
  }

So basically you are hitting the first route right now and it assumes that me is actually an :id. Koa is making the verifications with regex so in this case it takes the first matched route. Move the route with /me above that one with /:id

joedoesnotknow
  • 73
  • 1
  • 2
  • 8