Authentication is a critical part of modern web applications. OAuth is a secure and popular way to authenticate users via third-party providers like Google, GitHub, or Facebook. In this tutorial, we'll walk through setting up OAuth authentication using Node.js on the backend and React on the frontend.
🛠️ Tech Stack
Frontend: React (with Axios)
Backend: Node.js with Express
OAuth Provider: Google (though others like GitHub work similarly)
Passport.js: Middleware for authentication in Node.js
📦 Step 1: Set Up the Backend (Node.js + Express)
1. Initialize your project
2. Configure your environment variables
Create a .env file:
3. Set up the Express server
Create a file: index.js
⚛️ Step 2: Set Up the Frontend (React)
1. Create your React app
2. Create an AuthButton component
Create src/AuthButton.js:
3. Use the AuthButton in App.js
🚀 Running the App
Start the backend:
Start the frontend:
Make sure your Google OAuth credentials allow http://localhost:5000/auth/google/callback as an authorized redirect URI.
âś… Conclusion
Now you've set up a simple OAuth authentication system using Node.js, Passport, and React. This setup can be extended to include user sessions, persistent storage, different OAuth providers, and much more.
Want to take it further? Try integrating:
JWT tokens instead of sessions
MongoDB to store users
GitHub or Facebook OAuth