W
Wasp4mo ago
devrim

context.user is empty on api endpoint

export const fooBar = (req, res, context) => {
res.set("Access-Control-Allow-Origin", "*"); // Example of modifying headers to override Wasp default CORS middleware.
res.json({ msg: `Hello, ${context.user ? "registered user" : "stranger"}!` });
};
export const fooBar = (req, res, context) => {
res.set("Access-Control-Allow-Origin", "*"); // Example of modifying headers to override Wasp default CORS middleware.
res.json({ msg: `Hello, ${context.user ? "registered user" : "stranger"}!` });
};
this example i'm trying from the tutorial, regardless of auth:true/false has null as context.user, any ideas why?
2 Replies
kapa.ai
kapa.ai4mo ago
Hi @devrim. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The context.user object is populated when authentication is enabled and a valid JWT token is present in the request. If context.user is null, it could be due to one of the following reasons: 1. Authentication is not enabled: Ensure that you have enabled authentication in your Wasp application. If auth is set to false, the JWT token in the Authorization Header will not be parsed and context.user will not be populated. 2. No valid JWT token in the request: If there is no valid JWT token in the request, context.user will be null. Make sure that you are sending a valid JWT token in the Authorization Header of your request. 3. The user does not exist in the database: If the JWT token is valid but the user does not exist in the database, context.user will be null. Make sure that the user exists in the database. If you have checked all of the above and context.user is still null, it could be a bug. In that case, you might want to open an issue on the Wasp Github repository (https://github.com/wasp-lang/wasp/issues). For more information, you can refer to the following sources: - Defining the API's NodeJS Implementation (https://wasp-lang.dev/docs/advanced/apis#defining-the-apis-nodejs-implementation) - API Reference (https://wasp-lang.dev/docs/advanced/apis#api-reference) - Using the context.user object (https://wasp-lang.dev/docs/auth/overview#using-the-contextuser-object) - Revisit our auth middleware behaviour (https://github.com/wasp-lang/wasp/issues/1133) - Provide more information during development when error happens on server (https://github.com/wasp-lang/wasp/issues/91) - Use declare global to declare Wasp specific fields in the Express request (https://github.com/wasp-lang/wasp/issues/1632) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @devrim, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho4mo ago
Read this pls: https://wasp-lang.dev/docs/advanced/apis#using-the-api-from-the-client
To use the API from your client, including with auth support, you can import the Axios wrapper from wasp/client/api and invoke a call.
Custom HTTP API Endpoints | Wasp
In Wasp, the default client-server interaction mechanism is through Operations. However, if you need a specific URL method/path, or a specific response, Operations may not be suitable for you. For these cases, you can use an api. Best of all, they should look and feel very familiar.
Want results from more Discord servers?
Add your server