I am building a web and mobile app with react/react native for front end, Java-spring boot for backend with OAuth2 authorization.
Google OAuth Login from the react web app works fine. How do I manage login sessions and invalidate them on logout.
Any suggestions/advice will be helpful.
Thanks in advance.
Haven’t really tried anything yet; want to understand how this needs to be done so I can check for the same on the mobile app as well.
2
Answers
Reference to spring security
Here I am trying to post skeleton that can be improved upon as per your project requirements.
Spring Security Config
RestController to expose login/logout endpoints
properties file
React Logout component
It would be a bad experience for users to be disconnected from Google when they log out from your app. But if their Google session remains active, then their next "login with Google" from within your app will run silently and they’ll feel like they never logged out from your app.
A solution is to use an authorization server of your own in front of Google. Almost all OIDC Providers implement "Login with …" (Keycloak, Auth0, or in this case, maybe Spring Authorization Server). Like that, you can end the session on the authorization without closing the social login provider session. Also, this will have several side benefits like enabling you to manage user data (add stuff like roles).
I wrote a tutorial for getting started with Spring & Keycloak on Baeldung.
I wrote another tutorial about the OAuth2 BFF pattern for single-page applications. It covers React web apps.
In this other answer, I detailed what it takes to authorize requests with sessions (and CSRF token) from a mobile app.