medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.93k stars 2.6k forks source link

Medusa v2 Docs: Issue in 5.7. Custom CLI Scripts - Medusa v2 Docs #9268

Closed amaster507 closed 1 month ago

amaster507 commented 1 month ago

What Medusa version and documentation are you using?

v2

Preliminary Checks

Issue Summary

In the example code under # Authentication-opt-out, it uses both these lines of code:

req: AuthenticatedMedusaRequest,

and

export const AUTHENTICATE = false

After doing some testing, by setting the AUTHENTICATE to false you lose the access to the auth_context even if the user/customer is logged in.

How can this issue be resolved?

Make mention somehow in the docs that by disabling AUTHENTICATE on an /admin/* page, you no longer have access to the authentication context. Maybe change the example code snippet too where these are used together.

Are you interested in working on this issue?

shahednasser commented 1 month ago

Thanks for reporting this. Will add a note to clarify this.

However, may I ask what your use case is that requires you to opt out of authentication but still access the admin user?

amaster507 commented 1 month ago

My intention when testing this was to use it as a way to switch off from a free version of an api route vs more features if authentication context is added. Then in the code decide throw errors if certain parameters were met without authentication. Like limit to 25 items for the free context, but allow a full featured request with authentication request up to 500 items in a single query.

shahednasser commented 1 month ago

Got it, why don't you go for an API route under a custom path (not /admin)? Feels more appropriate logically.

Anyway, as mentioned in this section, you can add the authentication middleware to your routes setting allowUnauthenticated to true. That way you don't need the authentication opt-out