Back

Handling state change with React and Google OAuth

  • 1
  • General
  • Web
Nevus
18 Jun, 2023, 12:18

I have a react application that checks user authentication on render with account.get() and redirects to login page if error code is 401 It has a sign in button to execute await account.createOAuth2Session('google', 'http://localhost:5173', 'http://localhost:5173/failed');

After logging out cookies are still set but account.get() returns 401 which makes sense After completing oauth process from google appwrite redirects to the homepage which makes a request to account.get() but this request does not have any cookies set and returns 401 so the user is constantly redirected to login page.

Waiting for a second and reloading the page resolves the issue. How do I handle this in a better way?

TL;DR
The user is facing an issue with state changes in their React application while using Google OAuth. After logging out, the cookies are still set but calling `account.get()` returns a 401 error, which is expected. After completing the OAuth process, the user is redirected to the homepage, where `account.get()` is called again, but this request doesn't have cookies set, leading to a constant redirection to the login page. The user is currently resolving the issue by waiting for a second and reloading the page. Solution: One possible solution is to have the new tab send a message to the original tab using the `Window.postMessage()` method.
Nevus
18 Jun, 2023, 12:20

^ request to account.get()

Drake
18 Jun, 2023, 15:43
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more