#Working with Actor Type - authentication not working

7 messages · Page 1 of 1 (latest)

lethal hatch
#

Currently following this guide
https://docs.medusajs.com/v2/resources/commerce-modules/auth/create-actor-type

When I'm trying to curl some endpoint specially on

curl -X POST 'http://localhost:9000/auth/manager/emailpass' \
-H 'Content-Type: application/json' \
--data-raw '{
    "email": "[email protected]",
    "password": "supersecret"
}'

Which returns a token and create a row on auth_identity table which has a null value on app_metadata

And when I'm try to create a manager with the token with

curl -X POST 'http://localhost:9000/manager' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {token}' \
--data-raw '{
    "first_name": "John",
    "last_name": "Doe",
    "email": "[email protected]"
}'

Keeps returning an error due to req.auth_context is undefined which I can't pass the auth_indentity_id. But when I'm trying to check the token on JWT.io website to verify the payload and I can confirm that token has payload. sample below.

{
  "actor_id": "",
  "actor_type": "manager",
  "auth_identity_id": "authid_01J48BCRJV3S8BEC7CPY7GN04T",
  "app_metadata": {},
  "iat": 1722562273,
  "exp": 1722648673
}

Explore Medusa's recipes, API references, configurations, and more.

#

Here is my middleware

import {
  defineMiddlewares,
  authenticate,
  MedusaRequest,
} from "@medusajs/medusa"

import {
  validateAndTransformBody,
} from "@medusajs/medusa/dist/api/utils/validate-body"
import { AdminCreateProduct } from "@medusajs/medusa/dist/api/admin/products/validators"

export default defineMiddlewares({
  routes: [
    {
      matcher: "/manager",
      method: "POST",
      middlewares: [
        (req: MedusaRequest, res, next) => {
          console.dir(req)
          console.log("after auth with actor type ")

          next()
        },
        authenticate("manager", ["session", "bearer"], {
          allowUnregistered: true,
        }),
      ],
    },
    {
      matcher: "/manager/me*",
      middlewares: [
        authenticate("manager", ["session", "bearer"]),
      ],
    }
  ],
})
lethal hatch
#

When I'm trying to log the req from the API. On request the auth_context, authTypes, actorTypes are missing

next ravine
#

You should move your middleware after the auth middleware

lethal hatch
#

Something like this? By removing the first index of "/manager" POST? When I tried it the issue still the same

export default defineMiddlewares({
  routes: [
    {
      matcher: "/manager",
      method: "POST",
      middlewares: [
        authenticate("manager", ["session", "bearer"], {
          allowUnregistered: true,
        }),
      ],
    },
    {
      matcher: "/manager/me*",
      middlewares: [
        authenticate("manager", ["session", "bearer"]),
      ],
    },
  ],
})
#

Is there any to add on medusa-config.js? Like auth module

lethal hatch
#

I got typo for middlewares filename; currently my file name is middleware.ts

So make sure to double check the typo for filename;