Skip to content

Refresh tokens never work: frontend discards them and /api/auth/refresh returns 500 #331

Open

Description

@Yueqiao12Zhang

The refresh-token flow added in #100 has never worked end to end. There are three separate problems:

1. The frontend throws the refresh token away. /api/login and /api/register return {token, refresh_token, user}, but LoginForm.tsx and RegisterForm.tsx only destructure {token, user}, and App.tsx's handleLoginSuccess only calls setToken. mothra_refresh_token is never written to localStorage, so apiFetch's silent-refresh branch always takes the "no refresh token" path. Every session still hard-expires after 72h with "Your session has expired", despite the 30-day refresh token the backend issued.

2. /api/auth/refresh returns 500 on every call. refresh_tokens.expires_at is TIMESTAMPTZ, so psycopg2 returns it timezone-aware, but auth_api.py compares it with a naive datetime.utcnow():

File "landing-page/scripts/auth_api.py", line 736, in refresh_token
TypeError: can't compare offset-naive and offset-aware datetimes

create_refresh_token() also writes a naive utcnow(), which Postgres reads in the session's timezone. So on a database not running in UTC, the stored expiry is hours off. Problem 1 has hidden this one, because nothing ever calls the endpoint. Fixing only problem 1 would turn the quiet 72h logout into a server error.

3. Logout never revokes the refresh token. doLogout() in App.tsx only clears local state. It never calls POST /api/auth/logout, so a copied-out refresh token stays valid for its full 30 days after the user logs out.

Steps to reproduce

  1. Log in, then check localStorage: there's a mothra_token but no mothra_refresh_token.
  2. Set mothra_token to any invalid value and reload. You're logged out instead of being silently refreshed.
  3. Call the refresh endpoint directly with the refresh_token from a /api/login response: curl -X POST localhost:8001/api/auth/refresh -H "X-Refresh-Token: <token>". It returns 500 Internal Server Error, with the traceback above in the backend log.

Expected

The refresh token is stored at login, so an expired access token is renewed silently for as long as the refresh token is valid. /api/auth/refresh returns a new token pair. Logging out revokes the refresh token on the server.

Fixed on branch fix/auth-refresh-token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions