Repository navigation
fix: make login work in the browser (CORS + web token storage) - #48
Merged
Merged
Conversation
Browsers blocked API calls from the Expo web dev server because the preflight OPTIONS request returned 405. Allowed origins are configurable via CORS_ORIGINS (default: http://localhost:8081).
expo-secure-store has no web implementation, so restoring the session and signing in failed in the browser. Native builds keep using SecureStore.
Andreas7274
approved these changes
Nov 25, 2026
Andreas7274
left a comment
Collaborator
There was a problem hiding this comment.
the code should be fine to merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
When you run the app in the browser (
npm run web), login and register did notwork. The app showed "Server nicht erreichbar. Bitte Verbindung prüfen.", even
though the backend was running. On a phone everything worked. There were two
separate problems, and both only happen in the browser.
Problem 1: the backend did not allow requests from the browser (CORS)
The web app runs on
http://localhost:8081and the backend on port8000.For a browser these are two different "origins" (address + port). Before the
browser sends a request to a different origin, it first asks the server:
"Am I allowed to call you from
localhost:8081?" This question is a specialOPTIONSrequest, called a preflight.Our backend did not know how to answer this question. It returned
405 Method Not Allowed, so the browser blocked the real request. The apponly saw a failed request and showed the "server not reachable" message.
Phones and emulators do not do this check. That is why the bug only showed up
in the browser.
Problem 2: the login token could not be saved in the browser
After login, the app saves the token with
expo-secure-store. On a phone thisuses the secure storage of the system (Keychain on iOS, Keystore on Android).
A browser has no such storage, so
expo-secure-storehas no web version atall. Every call failed with
ExpoSecureStore.default.getValueWithKeyAsync is not a function.What
Backend
CORSMiddlewareinbackend/app/main.py. Now the backendanswers the preflight question with "yes" for allowed origins.
CORS_ORIGINS(comma-separated). The default ishttp://localhost:8081,the Expo web dev server. Other origins are still blocked.
CORS_ORIGINStodocker-compose.ymland.env.example.backend/tests/test_cors.py: one test checks thatlocalhost:8081is allowed, one checks that an unknown origin is not.
App
mobile/src/storage/with two files that have the samefunctions (
getToken,setToken,deleteToken):token.tsuses SecureStore. Used on iOS and Android, so nothing changeson phones.
token.web.tsuses the browser'slocalStorage. Expo picks the.web.tsfile automatically when it builds for the browser.AuthContext.tsxnow uses these functions instead of calling SecureStoredirectly.
How to test
docker compose up. No rebuild is needed.mobile/.env, setEXPO_PUBLIC_API_URLtohttp://localhost:8000orto your computer's IP (
ipconfig getifaddr en0on macOS).cd mobile && npx expo start -c --web.should show no CORS error.
Backend checks:
cd backend && python -m ruff check . && python -m pytest(19 tests pass).