CORS issue for curling / requesting OGB script functions

I am trying to request the authenticate_user function, it is public. Here is the code:
import axios from "axios";

export interface Response {
userId: string;
}

/**
* Get the user id from a token if it is valid
*
* Doc ref: https://opengb.dev/modules/users/scripts/get_user
* @param token string
* @returns User[]
*/
export async function authenticateUser(token: string): Promise<Response | null> {

try {
// Request the OpenGB endpoint (should be localhost:8080)
const { data } = await axios.post(
`http://localhost:8080/modules/users/scripts/authenticate_user/call`,
{
userToken: token
}
);
return data;
} catch(e) {
console.log(`[OGB] ERR: ${e}`)
return null;
}

}
import axios from "axios";

export interface Response {
userId: string;
}

/**
* Get the user id from a token if it is valid
*
* Doc ref: https://opengb.dev/modules/users/scripts/get_user
* @param token string
* @returns User[]
*/
export async function authenticateUser(token: string): Promise<Response | null> {

try {
// Request the OpenGB endpoint (should be localhost:8080)
const { data } = await axios.post(
`http://localhost:8080/modules/users/scripts/authenticate_user/call`,
{
userToken: token
}
);
return data;
} catch(e) {
console.log(`[OGB] ERR: ${e}`)
return null;
}

}
No description
33 Replies
Avery
Avery2y ago
Hey there! What URL are you making the OpenGB API request from? So here's what I think happened: - We (erroneously) did not add the header "Access-Control-Allow-Origin": "*" to the base case of serverHandler— essentially a 404 meant a CORS error. - You probably do not have users specified in your backend.yaml - This would mean that any /modules/users/scripts/*/call would be 404s because the runtime wasn't aware of the users module I think the solution is: 1. Add users to your backend.yaml (should work after this) 2. Wait for us to merge a 1-line change into main 😅
brendanthebigboi
brendanthebigboiOP2y ago
Hey thanks for your response, I do have users in the backend.yaml, I can fetch from postman but it just says invalid JSON body
Avery
Avery2y ago
Hm... Oh, I think I see the issue. By default, axios uses multipart/form-data as the encoding type.
Avery
Avery2y ago
Can you send me what's under the Network tab in chrome, specifically the Headers and Payload sub-tabs?
No description
Avery
Avery2y ago
(Also, under the payload sub-tab, click view source)
brendanthebigboi
brendanthebigboiOP2y ago
Yes sorry I’m afk but will get back to you
Avery
Avery2y ago
Nevermind on that actually, the issue is axios defaults to CORS preflight, and we don't handle that. I know the fix.
brendanthebigboi
brendanthebigboiOP2y ago
On my end?
Avery
Avery2y ago
Reinstalling opengb with this command should fix it for now.
deno install --name opengb --force --allow-net --allow-read --allow-env --allow-run --allow-write --allow-sys https://raw.githubusercontent.com/rivet-gg/opengb/05-22-chore_fix_--global_in_cli_install_and_return_cors_header_on_404/src/cli/main.ts
deno install --name opengb --force --allow-net --allow-read --allow-env --allow-run --allow-write --allow-sys https://raw.githubusercontent.com/rivet-gg/opengb/05-22-chore_fix_--global_in_cli_install_and_return_cors_header_on_404/src/cli/main.ts
brendanthebigboi
brendanthebigboiOP2y ago
Do you mind explaining the issue? That way I can learn from it too
Avery
Avery2y ago
Essentially, when axios sends a POST or PUT request, it actually sends two requests— first an OPTIONS request, and then the actual original request. The first request is called a "preflight" request. The point of the OPTIONS request is to check whether the server can be communicated with using the settings the original request (originating URL, method, and headers). However, we were returning incorrect headers in the case of an OPTIONS request, making the browser assume that CORS should not allow the requesting webpage to make that request.
brendanthebigboi
brendanthebigboiOP2y ago
Got ya Thank you man Makes a lot of sense Ran this, still getting CORs error
brendanthebigboi
brendanthebigboiOP2y ago
No description
brendanthebigboi
brendanthebigboiOP2y ago
It's a different one though
Avery
Avery2y ago
Try running it again. I didn't realize that sending a 404 would make the preflieght fail.
brendanthebigboi
brendanthebigboiOP2y ago
Wouldn’t sending a 404 give an error as well? It’s the correct endpoint according to the doc
Avery
Avery2y ago
We have to work on when we send 404 errors— we need to start sending 405 errors instead in some cases.
brendanthebigboi
brendanthebigboiOP2y ago
No description
brendanthebigboi
brendanthebigboiOP2y ago
Still same error I haven't changed anything on my end, still same code as op
Avery
Avery2y ago
Did you rerun the install command?
brendanthebigboi
brendanthebigboiOP2y ago
yes
Nathan
Nathan2y ago
GitHub
Allow configuring CORS from the backend.yaml · Issue #313 · rivet-g...
This is a backend-wide configuration for the HTTP server itself, not something specific to modules. So this needs to be configured for the entire project. This should have a default of no CORS in o...
brendanthebigboi
brendanthebigboiOP2y ago
Anything I can do atm?
Nathan
Nathan2y ago
let me push a temp patch that does cors * give me 5m
Nathan
Nathan2y ago
actually, it'd be faster for both of us if you just used this plugin for a bit https://chromewebstore.google.com/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf
Allow CORS: Access-Control-Allow-Origin
Easily add (Access-Control-Allow-Origin: *) rule to the response header.
Nathan
Nathan2y ago
@Sky do you have time today to impl #313?
brendanthebigboi
brendanthebigboiOP2y ago
Ok sounds good thanks Alright im starting on this For some reason, even with the extension on, I am still getting the same error
Nathan
Nathan2y ago
hmm
Nathan
Nathan2y ago
sky has a wip pr here that you can probably use instead: https://github.com/rivet-gg/opengb/pull/314
Nathan
Nathan2y ago
so install that and add to your backend yaml:
runtime:
cors:
origins:
- "localhost:3000"
runtime:
cors:
origins:
- "localhost:3000"
whatever the localhost your next thing is running on ^
brendanthebigboi
brendanthebigboiOP2y ago
Do I just rerun this? Sorry I'm not familiar with forking @Nathan Is it cool if I just hop in the vc rq and get this thing solved before I gotta head out?
Nathan
Nathan2y ago
yep
brendanthebigboi
brendanthebigboiOP2y ago
No description

Did you find this page helpful?