You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] /api/login is called on every dashboard page visit #22
While working on #7, I realized that whenever I'm visiting any page on the dashboard, the /api/login route is called again and again.
Ideally, it should only be called when the user tries to log in. After that, every time I request something to be sent, it should be sent just through the API with JWT headers, and the backend should verify if the person is logged in or not.
I'm not very familiar with that part of the codebase, though. Raising this issue for future reference, when we refactor the auth.
I asked / searched and seems like using HttpOnly token is better and is the approach we currently use instead of just storing the token in browser local storage and appending to Authorization header for every client fetch.
I will try to explain my understanding here, but I might be wrong and lmk if anything does not make sense since this is my first time doing this @inclinedadarsh
What we actually do is:
The frontend on login POST api/auth/login via auth-provider.tsx and the backend returns with a HttpOnly cookie
The browser appends this cookie to every fetch request
The middleware first checks if this cookie exists otherwise ask you to login again (middleware.ts)
The backend then extracts this cookie and set the Authorization header see backendFetch function, then proxies it to the GCP endpoint (api/utils.ts)
The GCP backend finally uses firebase_admin to verify the token with firebase
The reasons of this over LocalStorage are:
The forntend does not need a authFetch function to get from localStorage and attach header everytime
The token is kept out of reach of javascript by avoiding localstorage to avoid vulnerabilities like XSS attacks
If my understanding is correct:
The behaviour of posting api/auth/loginevery time you refresh the page is expected because AuthProvider actually reloads and gets the ID again by user.getIdToken() and POST the backend to "inform" it of the latest ID which will be set as cookie. However, this should NOT POST everytime you land on that page by routing only when you refresh via browser (and this is the correct behaviour that i've observed).
By the way:
This getIdToken function tries to use the short-lived JWT and if expired automatically uses long-lived refresh_token stored in browser to regenerate one, so no need manual handling as well.
No auth with firebase is done on the api/auth/login in the backend so it is cheap (i.e. we trust what the frontend gives to be valid token). In the future, this can be made to verify once in the next backend and create a session token first instead of blindly trusting GCP to verify.
Activity
supreme-gg-gg commentedon Aug 21, 2025
I asked / searched and seems like using HttpOnly token is better and is the approach we currently use instead of just storing the token in browser local storage and appending to Authorization header for every client fetch.
I will try to explain my understanding here, but I might be wrong and lmk if anything does not make sense since this is my first time doing this @inclinedadarsh
What we actually do is:
api/auth/loginviaauth-provider.tsxand the backend returns with a HttpOnly cookiemiddleware.ts)backendFetchfunction, then proxies it to the GCP endpoint (api/utils.ts)firebase_adminto verify the token with firebaseThe reasons of this over LocalStorage are:
authFetchfunction to get from localStorage and attach header everytimeIf my understanding is correct:
The behaviour of posting
api/auth/loginevery time you refresh the page is expected becauseAuthProvideractually reloads and gets the ID again byuser.getIdToken()and POST the backend to "inform" it of the latest ID which will be set as cookie. However, this should NOT POST everytime you land on that page by routing only when you refresh via browser (and this is the correct behaviour that i've observed).By the way:
This
getIdTokenfunction tries to use the short-lived JWT and if expired automatically uses long-livedrefresh_tokenstored in browser to regenerate one, so no need manual handling as well.No auth with firebase is done on the
api/auth/loginin the backend so it is cheap (i.e. we trust what the frontend gives to be valid token). In the future, this can be made to verify once in the next backend and create a session token first instead of blindly trusting GCP to verify.