Skip to content

fix: make login work in the browser (CORS + web token storage) - #48

Merged
DarynaBb merged 2 commits into
mainfrom
fix/backend-cors
Nov 25, 2026
Merged

DarynaBb merged 2 commits into
mainfrom
fix/backend-cors

Conversation

@DarynaBb

Copy link
Copy Markdown
Collaborator

Why

When you run the app in the browser (npm run web), login and register did not
work. 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:8081 and the backend on port 8000.
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 special
OPTIONS request, 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 app
only 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 this
uses the secure storage of the system (Keychain on iOS, Keystore on Android).
A browser has no such storage, so expo-secure-store has no web version at
all. Every call failed with
ExpoSecureStore.default.getValueWithKeyAsync is not a function.

What

Backend

  • Added FastAPI's CORSMiddleware in backend/app/main.py. Now the backend
    answers the preflight question with "yes" for allowed origins.
  • The list of allowed origins comes from a new environment variable
    CORS_ORIGINS (comma-separated). The default is http://localhost:8081,
    the Expo web dev server. Other origins are still blocked.
  • Added CORS_ORIGINS to docker-compose.yml and .env.example.
  • Added backend/tests/test_cors.py: one test checks that localhost:8081
    is allowed, one checks that an unknown origin is not.

App

  • New folder mobile/src/storage/ with two files that have the same
    functions (getToken, setToken, deleteToken):
    • token.ts uses SecureStore. Used on iOS and Android, so nothing changes
      on phones.
    • token.web.ts uses the browser's localStorage. Expo picks the
      .web.ts file automatically when it builds for the browser.
  • AuthContext.tsx now uses these functions instead of calling SecureStore
    directly.

Note: localStorage is not secure storage (any script on the page can read
it). This is fine for testing on our own computer. If we ever ship a real
web version, the token should move to a safer place, for example an
httpOnly cookie.

How to test

  1. Start the backend: docker compose up. No rebuild is needed.
  2. In mobile/.env, set EXPO_PUBLIC_API_URL to http://localhost:8000 or
    to your computer's IP (ipconfig getifaddr en0 on macOS).
  3. Start the web app: cd mobile && npx expo start -c --web.
  4. Register or log in. You should land on the tabs, and the browser console
    should show no CORS error.
  5. Reload the page. You should still be logged in.
  6. Optional: check on a phone with Expo Go that login still works there.

Backend checks: cd backend && python -m ruff check . && python -m pytest
(19 tests pass).

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.
@DarynaBb
DarynaBb requested a review from Andreas7274 November 25, 2026 13:31

@Andreas7274 Andreas7274 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code should be fine to merge

@DarynaBb
DarynaBb merged commit 1e19932 into main Nov 25, 2026
2 checks passed
@DarynaBb
DarynaBb deleted the fix/backend-cors branch November 25, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants