Users get logged out automatically
Users get logged out automatically and I can't really see the issue.
In the Chrome Browser tools, it gives me these error messages:
index-DyjaD6qb.js:90 TypeError: Cannot read properties of undefined (reading 'length')
at onAccept (index-DyjaD6qb.js:90:21778)
at vg (index-DyjaD6qb.js:90:4501)
at g6 (index-DyjaD6qb.js:90:21205)
onAccept @ index-DyjaD6qb.js:90Fehler nachvollziehen
injected.js:6 Caught error handling <hide-notification> message
(anonym) @ injected.js:6Diese Warnung nachvollziehen
server-production-41e1.up.railway.app/auth/me:1
Failed to load resource: the server responded with a status of 401 () In Railway, I get this error which happens because of: if (!user) { throw new Error("User not authenticated!") } --> at checkSlackUserEmail (/app/src/server/queries.ts:269:11) at checkSlackUserEmail$1 (/app/.wasp/build/server/src/queries/checkSlackUserEmail.ts:7:11) at <anonymous> (/app/.wasp/build/server/src/middleware/operations.ts:14:30) at <anonymous> (/app/.wasp/out/sdk/wasp/server/utils.ts:26:11) at Layer.handle [as handle_request] (/app/.wasp/build/server/node_modules/express/lib/router/layer.js:95:5) at next (/app/.wasp/build/server/node_modules/express/lib/router/route.js:149:13) at <anonymous> (/app/.wasp/out/sdk/wasp/core/auth.ts:25:12) at <anonymous> (/app/.wasp/out/sdk/wasp/server/utils.ts:26:11) at Layer.handle [as handle_request] (/app/.wasp/build/server/node_modules/express/lib/router/layer.js:95:5) at next (/app/.wasp/build/server/node_modules/express/lib/router/route.js:149:13)
Failed to load resource: the server responded with a status of 401 () In Railway, I get this error which happens because of: if (!user) { throw new Error("User not authenticated!") } --> at checkSlackUserEmail (/app/src/server/queries.ts:269:11) at checkSlackUserEmail$1 (/app/.wasp/build/server/src/queries/checkSlackUserEmail.ts:7:11) at <anonymous> (/app/.wasp/build/server/src/middleware/operations.ts:14:30) at <anonymous> (/app/.wasp/out/sdk/wasp/server/utils.ts:26:11) at Layer.handle [as handle_request] (/app/.wasp/build/server/node_modules/express/lib/router/layer.js:95:5) at next (/app/.wasp/build/server/node_modules/express/lib/router/route.js:149:13) at <anonymous> (/app/.wasp/out/sdk/wasp/core/auth.ts:25:12) at <anonymous> (/app/.wasp/out/sdk/wasp/server/utils.ts:26:11) at Layer.handle [as handle_request] (/app/.wasp/build/server/node_modules/express/lib/router/layer.js:95:5) at next (/app/.wasp/build/server/node_modules/express/lib/router/route.js:149:13)
3 Replies
Hi @mb23. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the error logs you've shared, it appears you're experiencing an issue where users are being automatically logged out of your Wasp application. This is a known behavior in Wasp related to how authentication and 401 responses are handled.
Why Users Are Getting Logged Out
The key issue is in how Wasp handles 401 responses. When your application receives a 401 Unauthorized response from the server (as shown in your logs with
/auth/me
returning 401), Wasp's default behavior is to clear the authentication token from localStorage, effectively logging the user out.
This behavior is documented in a GitHub issue: Revisit auth token clearing behavior on 401 responses. The issue explains that when a 401 response is received, Wasp clears the auth token, which can cause unexpected logouts.
Common Scenarios That Cause This Issue
There are a few scenarios that commonly trigger this behavior:
1. Multiple tabs: If you have multiple tabs open and one tab is not authenticated while another is, switching between them can cause the authenticated tab to become logged out.
2. Operations requiring authentication: If an operation that requires authentication is called when the user isn't properly authenticated, it will return 401 and trigger the logout.
3. Token expiration: If your JWT token has expired, verification will fail and return a 401.
Possible Solutions
While this is a known issue in Wasp that hasn't been fully resolved yet, you can try these approaches:
1. Check authentication status before making requests: Ensure you're checking the user's authentication status before making requests that require authentication.
2. Use proper error codes: Make sure you're using the correct HTTP status codes in your operations. For authentication failures, use 401 (not 403):
3. Avoid unnecessary auth checks: If you have pages that don't require authentication, make sure they're not making unnecessary calls to /auth/me
.
The Wasp team is aware of this issue and has it open for improvement. The current behavior is that when a 401 response is received, the auth token is cleared from localStorage, which affects all tabs sharing that localStorage.
If you need more specific help with your implementation, you might want to share more details about your application structure and authentication flow.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @mb23, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?it was the multiple tabs open thing
Glad its sorted out.