#1505·photoview

A database error while checking the session token logs users out (401 instead of 500)

Author: try-and-succeedCreated Sep 22, 2026Updated Sep 22, 2026

Describe the bug When the database fails while the API looks up the user behind an auth-token cookie, the request is answered with 401 Unauthorized. A 401 tells the UI that the token is no longer valid, so a short database outage logs every active user out. Their sessions are fine; the server just couldn't check them.

api/graphql/auth/auth.go, in Middleware:

go
user, err := loaders.UserFromAccessToken.Load(tokenCookie.Value)
if err != nil {
    log.Error(r.Context(), "Error loading user from token", "error", err)
    http.Error(w, INVALID_AUTH_TOKEN, http.StatusUnauthorized)   // line 47
    return
}

The branch right after it, where the token simply isn't found (user == nil), is where a 401 belongs. AuthWebsocketInit has the same pair of branches for subscriptions, and both return the same error there too.

To Reproduce

  1. Log in.
  2. Make the database briefly unavailable (restart the DB container, or drop the connection).
  3. Any request made in that window returns 401 invalid authorization token, and the UI clears the session.

Expected behavior A failed lookup is a server error (500). Only a token that was looked up and not found (or expired) is a 401.

Additional context Found while working on #1486, which makes the UI end the session on HTTP 401/403 only, and no longer on network errors or 5xx. With this bug the API still sends a 401 for an outage, so the UI can't tell the two apart. I'm happy to send the fix as a small PR (500 for the lookup error, both in the HTTP middleware and in the websocket init), with a test.

A related detail from the same analysis: AddMediaShare and AddAlbumShare (share_token_actions.go) return auth.ErrUnauthorized for a logged-in user trying to share something that isn't theirs. That is really a "forbidden", but it uses the same error as "not logged in". After #1486 the UI no longer reacts to that message, so it's only a naming issue now, but a distinct error would make the API's answers unambiguous.