hey SuperTokens team https://supertokens.com/docs/...
# support-questions-legacy
a
hey SuperTokens team https://supertokens.com/docs/thirdparty/troubleshooting/cors-issues do we have the same docs for python fast-api based apps ?
r
hey @akrem.yes not really. But the same error messages apply
a
thanks
hey hey
I am having a really really weird cors issue. I followed all tutorials and tried 1 million solution and am always getting the same error
I enabled the debug mode so you can maybe have a look with me because it's really crazy
these are the logs from frontend
these are from backend
and this is the backend file
Copy code
from agenta_backend.routers import (
    health_router,
)


from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

origins = [
    "http://localhost:3000",
]

print("are we even here?")

app = FastAPI()

allow_headers = ["Content-Type"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:3000"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=['Content-Type', 'anti-csrf', 'authorization', 'st-auth-mode', 'rid', 'fdi-version'],
)



app.include_router(health_router.router, prefix="/health")
any idea ?
r
what is the cors issue? What are the response headers from the preflight API call?
a
Copy code
INFO:     Started server process [7]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
are we even here???????
are we even here???????
are we even here?
INFO:     192.168.65.1:30408 - "OPTIONS /api/auth/session/refresh HTTP/1.1" 400 Bad Request
INFO:     192.168.65.1:30408 - "OPTIONS /api/auth/session/refresh HTTP/1.1" 400 Bad Request
INFO:     192.168.65.1:30443 - "OPTIONS /api/auth/session/refresh HTTP/1.1" 400 Bad Request
INFO:     192.168.65.1:30444 - "OPTIONS /api/auth/session/refresh HTTP/1.1" 400 Bad Request
r
whats the browser console show?
a
r
this means something is wrong with how you have setup the cors middleware
in the code above, where have you added the supertokens middelware?
a
Copy code
from supertokens_python import get_all_cors_headers

from agenta_backend.routers import (
    health_router,
)

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from supertokens_python.framework.fastapi import get_middleware

origins = [
    "http://localhost:3000",
]

print("are we even here?")

app = FastAPI()
app.add_middleware(get_middleware())
allow_headers = ["Content-Type"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:3000"],
    allow_credentials=True,
    allow_methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"],
    allow_headers=["Content-Type"] + get_all_cors_headers(),
)


app.include_router(health_router.router, prefix="/health")
r
you should add the cors middelware before the supertokens middleware
and try again
a
even the import ?
r
no
a
Copy code
from supertokens_python import get_all_cors_headers

from agenta_backend.routers import (
    health_router,
)

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from supertokens_python.framework.fastapi import get_middleware

origins = [
    "http://localhost:3000",
]

print("are we even here?")

app = FastAPI()
allow_headers = ["Content-Type"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:3000"],
    allow_credentials=True,
    allow_methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"],
    allow_headers=["Content-Type"] + get_all_cors_headers(),
)
app.add_middleware(get_middleware())

app.include_router(health_router.router, prefix="/health")
same error
r
yeaa.. not sure anymore
you migth wanna ask in fastapi community about cors
a
hey let's say we integrated supertokens in frontend but not in backend, and our backend is well configured to handle. will the integration in frontend cause a cors issue ?
r
yea
2 Views