W
Wasp5mo ago
devrim

Tutorial API implementation doesn't work for me.

my main.wasp is
api upload_video { // APIs and their implementations don't need to (but can) have the same name.
fn: import { fooBar } from "@src/api/api",
httpRoute: (GET, "/api"),
auth: false,
middlewareConfigFn: import { apiMiddleware } from "@src/api/api"
}
api upload_video { // APIs and their implementations don't need to (but can) have the same name.
fn: import { fooBar } from "@src/api/api",
httpRoute: (GET, "/api"),
auth: false,
middlewareConfigFn: import { apiMiddleware } from "@src/api/api"
}
my src/api/api.jsx is
export const fooBar = (req, res, context) => {
console.log("req");
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 apiMiddleware = (config) => {
return config;
};
export const fooBar = (req, res, context) => {
console.log("req");
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 apiMiddleware = (config) => {
return config;
};
code compiles with no problems, but localhost:3000/api endpoint returns 401 instead of the api response. any ideas?
22 Replies
kapa.ai
kapa.ai5mo 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 issue you're experiencing might be related to the authentication setting in your API declaration. In your main.wasp file, you have set auth: false, which means that the API does not attempt to parse the JWT in the Authorization Header. However, if the request to the API includes an Authorization Header, and the JWT is invalid or expired, the server might still return a 401 Unauthorized error. If you're sure that the request does not include an Authorization Header, or that the JWT is valid, then the issue might be elsewhere. It could be related to the middleware configuration, or it could be a bug in Wasp. There is an open issue on the Wasp GitHub repository about providing more information during development when an error happens on the server, which might be related to your problem (source (https://github.com/wasp-lang/wasp/issues/91)). As a first step, I would suggest to check the request you're sending to the API, and make sure it does not include an Authorization Header, or that the JWT is valid. If that doesn't solve the problem, you might want to look into customizing the middleware configuration, as described in the Wasp documentation (https://wasp-lang.dev/docs/advanced/middleware-config). If none of these suggestions help, I would recommend to open a new issue on the Wasp GitHub repository (https://github.com/wasp-lang/wasp/issues), providing as much information as possible about your problem. The Wasp team is usually very responsive and will be able to help you troubleshoot the issue. ---------------------- 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?
devrim
devrimOP5mo ago
just hitting the url with localhost:3000/api with no JWT - github issue is not helpful. ok cursor AI helped, if i do localhost:3001/api then it works..
MEE6
MEE65mo ago
Wohooo @devrim, you just became a Waspeteer level 1!
devrim
devrimOP5mo ago
but this doesn't solve how i access that endpoint when the app is deployed...
Gwaggli
Gwaggli5mo ago
Depends where you deploy your backend. The API is part of your backend server. So you need to use the url of your backend server. If you used the standard wasp config and deployed it to fly.io your backend server address will look probably something like this: https://<yourAppname>-server.fly.dev So for example: https://devrim-server.fly.dev/api
devrim
devrimOP5mo ago
hi that makes sense, but i'm getting a forever spinning wheel when i hit the deployed URL
No description
devrim
devrimOP5mo ago
@Gwaggli if you can take a look.
devrim
devrimOP5mo ago
i think something else is up, with google auth.. debugging
devrim
devrimOP5mo ago
ok i'm sorry this is nothing to do with the api endpoint, it was complaining about the google oauth token, and not running the server at all. if you guys know what it's complaining about google token i'd appreciate that (followed the tutorial there fully as well) but if not please disregard the api not working request. thank you!
Gwaggli
Gwaggli5mo ago
I am not using google oAuth so not sure, but seems like you are missing some env variables? Does it work locally, right? Did you set the env variables in fly.io as well? Personal/Videoupdate-server/Secrets? (https://fly.io/apps/videoupdate-server/secrets)
miho
miho5mo ago
Which template are you using? How does your Wasp file look like, are you using Google auth? Did you set the env variables on Fly?
fustuk.
fustuk.5mo ago
refused connection. is your app listening on 0.0.0.0:8080? make sure it is not only listening on 127.0.0.1
refused connection. is your app listening on 0.0.0.0:8080? make sure it is not only listening on 127.0.0.1
Maybe this is your issue, is your redirect url on localhost rather than pointing to the hosted domain You also have these errors, indicating your env doesnt have the values for google auth have you added that yet? (keep in mind .env is in .gitignore you have to add it on the server)
throw new Error(`${envVarName} env variable is required when using the ${provider.displayName} auth provider.`);

Error: GOOGLE_CLIENT_ID env variable is required when using the Google auth provider.
throw new Error(`${envVarName} env variable is required when using the ${provider.displayName} auth provider.`);

Error: GOOGLE_CLIENT_ID env variable is required when using the Google auth provider.
devrim
devrimOP5mo ago
all problems here have been resolved with your help. thank you very much!
prof_mark
prof_mark4mo ago
im getting this error and cannot figure out what is going on. i have the GOOGLE_CLIENT_ID in my .env.server file, but somehow I guess fly is not picking it up on my redeploys ok it turns out my issue was fixed by setting the env vars on fly manually as described here: https://wasp-lang.dev/docs/advanced/deployment/cli#environment-variables-1 my question is then why I am also setting them in .env.server and/or why these are not pull from the .env.server file and set for my automatically as part of deployment. I think this is also related to the issue here: https://github.com/wasp-lang/wasp/issues/2053
fustuk.
fustuk.4mo ago
If I understand properly, you are asking why your env files are not pushed ? They are in .gitignore/.dockerignore so it doesnt leak info/keys.You should always set them manually when you deploy somewhere regardless of where you deploy and what frameworks you use
prof_mark
prof_mark4mo ago
ofc these should not be checked in to version control, but shouldnt it be fine to send the .env files (or at least the values from there) to the fly deployment automatically (when using the
wasp deploy fly
wasp deploy fly
command)? I dont think there is a need to set these manually rather than pulling them from the .env file and setting them automatically
fustuk.
fustuk.4mo ago
a quick search in the docs lead me to this
No description
Gwaggli
Gwaggli4mo ago
@prof_mark No, they should not be passed to the server environment. You need to set them in your different environments manually. This also allows you to define them per environment. If for example you set up a staging/test environment with a different url test.yourawesomeapp.com you would then use this url on your staging server and yourawesomeapp.com on your production server. Similar with other keys you might want to differentiate between environments. And regarding the fly deploy command: At some point you will want to setup some sort of CI/CD, where the deployment is triggered by for example github actions. So your local .env file would be lost anyways.
prof_mark
prof_mark4mo ago
Ah I see, that makes sense, thank you! I wonder if it might make sense to had a flag in fly deploy for beginners like me who are using opensaas - the flag could just tell the deployment script to grab all my env values and run the
fly set
fly set
command with these values for me.
miho
miho4mo ago
You write yourself a script that can do that for you smth like this
SECRETS=$(echo $(sed '/^#/d' < .env.server) | tr '\n' ' ' | tr -d "'")
wasp deploy fly cmd --context server secrets set $SECRETS
SECRETS=$(echo $(sed '/^#/d' < .env.server) | tr '\n' ' ' | tr -d "'")
wasp deploy fly cmd --context server secrets set $SECRETS
^ this is just what I use and I don't guarantee it's the best and it will always work
prof_mark
prof_mark4mo ago
Lol that works too haha
MEE6
MEE64mo ago
Wohooo @prof_mark, you just became a Waspeteer level 2!
Want results from more Discord servers?
Add your server