Repository navigation
fix: remove unused token localStorage reads and document WebSocket auth (#7622) - #7623
Closed
tusharkumar-coder wants to merge 1 commit into
Closed
tusharkumar-coder wants to merge 1 commit into
tusharkumar-coder wants to merge 1 commit into
Conversation
…th (PalisadoesFoundation#7622) The app authenticates via HTTP-only cookies set by talawa-api on login. Nothing in the codebase ever calls setItem('token'), so all getItem('token') and removeItem('token') calls were dead code that silently did nothing. Changes: - src/index.tsx: Remove getItem('token') from authLink; cookies are sent automatically by the browser. Remove dead token lookup from wsLink connectionParams; add a doc comment explaining that the WS handshake authenticates via the cookie on the HTTP upgrade request. Remove now-unused BEARER_PREFIX constant. - src/screens/AdminPortal/OrgList/OrgList.tsx: Remove getItem('token') and the dead context object passed to useQuery(CURRENT_USER). - src/components/AdminPortal/SecuredRoute/SecuredRoute.tsx: Remove removeItem('token') from session-timeout cleanup (key never set). - src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx: Same as above. - src/screens/Public/Invitation/AcceptInvitation.tsx: Remove AUTH_TOKEN_KEY ('token') constant. Fix isAuthenticated to use the IsLoggedIn localStorage flag (the real auth-state indicator) instead of Boolean(getItem('token')) which was always false. Remove dead removeItem(AUTH_TOKEN_KEY) call. - src/components/AdminPortal/Advertisements/AdvertisementsMocks.ts: Remove authLink mock that read getItem('token'); simplify mock client to plain HttpLink. Remove unused setContext/ApolloLink/useLocalStorage imports. Test updates: - securedRoute.spec.tsx: Remove setItem/getItem assertions for 'token'. - OrgList.spec.tsx: Remove setItem('token', 'mock-token') from setupUser. - AcceptInvitation.spec.tsx: Update renderComponent signature (3rd arg is now isLoggedIn: boolean instead of authToken: string), update mock to return 'TRUE' for the IsLoggedIn key, remove removeItem('token') assertion. Fixes PalisadoesFoundation#7622
Our Pull Request Approval ProcessThis PR will be reviewed according to our: Your PR may be automatically closed if:
Thanks for contributing! |
|
Thank you for your first contribution! We appreciate your effort and look forward to more contributions in the future. |
Contributor
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing (9)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The linked issue must be assigned to the PR author. |
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.
Summary
Closes #7622
Problem
The app authenticates via HTTP-only cookies set by talawa-api on login. However, several places still read or remove a
tokenitem from localStorage — and nothing insrc/ever callssetItem('token'). This meant theAuthorizationheader inauthLinkand the WebSocketconnectionParamstoken were always empty strings (dead code).Additionally, it was unclear how WebSocket subscriptions (e.g. chat) were authenticated, since the
connection_initpayload only containedAccept-Language.Investigation
Confirmed against
talawa-api(src/routes/graphql.ts):createContextreads thetalawa_access_tokenHTTP-only cookie. NoAuthorizationheader is needed from the client.onConnecthandler expectspayload.authorization: Bearer <token>. Since the access token is in an HTTP-only cookie (inaccessible to JS), the WS handshake relies on the browser sending the cookie automatically during the HTTP upgrade, which the API verifies increateContext.Changes
Source files
src/index.tsx: RemovegetItem('token')fromauthLink(cookies sent automatically). Remove dead token lookup fromwsLink connectionParams. Add a doc comment explaining the WebSocket auth mechanism. Remove the now-unusedBEARER_PREFIXconstant.src/screens/AdminPortal/OrgList/OrgList.tsx: RemovegetItem('token')and the deadcontextobject passed touseQuery(CURRENT_USER).src/components/AdminPortal/SecuredRoute/SecuredRoute.tsx: RemoveremoveItem('token')from session-timeout cleanup (key was never set).src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx: Same as above.src/screens/Public/Invitation/AcceptInvitation.tsx: RemoveAUTH_TOKEN_KEY = 'token'constant. FixisAuthenticatedto use theIsLoggedInlocalStorage flag (the real auth-state indicator) instead ofBoolean(getItem('token'))which was alwaysfalse. Remove deadremoveItem(AUTH_TOKEN_KEY)call.src/components/AdminPortal/Advertisements/AdvertisementsMocks.ts: Remove theauthLinkmock that readgetItem('token'). Simplify mock client to plainHttpLink. Remove unusedsetContext/ApolloLink/useLocalStorageimports.Test files
securedRoute.spec.tsx: RemovesetItem/getItemassertions for'token'.OrgList.spec.tsx: RemovesetItem('token', 'mock-token')fromsetupUser.AcceptInvitation.spec.tsx: UpdaterenderComponentsignature (3rd arg is nowisLoggedIn: booleaninstead ofauthToken: string), update mock to return'TRUE'for theIsLoggedInkey, removeremoveItem('token')assertion.How WebSocket Auth Works
The talawa-api
onConnecthandler validatespayload.authorization. The browser includes the HTTP-only access-token cookie on the WS HTTP upgrade request (same-origin). The API can read this cookie from the request to authenticate the WebSocket connection, similar to regular HTTP requests.