A sleek and secure authentication system powered by Google OAuth 2.0, built using the MERN stack.
Modern UI, smooth login experience, and full session-based authentication β plug-and-play for any project. π
- π¨ Apple-Inspired UI - Clean, minimal, and responsive design
- π Google OAuth 2.0 - Secure authentication with Google accounts
- πΎ MongoDB Integration - User data stored securely in MongoDB Atlas
- π Session Management - Persistent sessions with Passport.js
- β‘ Fast & Modern - Built with React (Vite) and Express.js
- π± Fully Responsive - Works seamlessly on all devices
- π― Modular Architecture - Clean separation of concerns (controllers, routes, middleware)
- React (Vite)
- Axios for API calls
- Context API for state management
- CSS3 with Apple-inspired design system
- Node.js & Express.js
- MongoDB with Mongoose
- Passport.js (Google OAuth 2.0 Strategy)
- Express Session for session management
- CORS enabled for cross-origin requests
googleAuth/
βββ backend/
β βββ config/
β β βββ db.js # MongoDB connection
β β βββ passport.js # Passport Google OAuth config
β βββ controllers/
β β βββ authController.js # Authentication logic
β βββ middleware/
β β βββ auth.js # Auth middleware
β βββ models/
β β βββ User.js # User schema
β βββ routes/
β β βββ authRoutes.js # Auth routes
β βββ .env # Environment variables
β βββ app.js # Express app setup
β βββ index.js # Server entry point
β
βββ frontend/
βββ src/
β βββ components/
β β βββ Home.jsx # Dashboard/Profile page
β β βββ Home.css
β β βββ Login.jsx # Login page
β β βββ Login.css
β βββ context/
β β βββ AuthContext.jsx # Auth state management
β βββ App.jsx
β βββ index.css # Global styles
βββ vite.config.js # Vite configuration
- Node.js (v16 or higher)
- MongoDB Atlas account
- Google Cloud Console account
git clone https://github.com/yourusername/google-auth-mern.git
cd google-auth-mern- Go to Google Cloud Console
- Create a new project or select an existing one
- Navigate to APIs & Services > Credentials
- Click Create Credentials > OAuth client ID
- Select Web application
- Add the following:
- Authorized JavaScript origins:
http://localhost:5001http://localhost:5173
- Authorized redirect URIs:
http://localhost:5001/auth/google/callback
- Authorized JavaScript origins:
- Copy your Client ID and Client Secret
cd backend
npm installCreate a .env file in the backend folder:
MONGODB_URI=your_mongodb_connection_string
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
SESSION_SECRET=your_random_session_secret
PORT=5001
CLIENT_URL=http://localhost:5173Start the backend server:
npm run devBackend will run on http://localhost:5001
cd frontend
npm installStart the frontend:
npm run devFrontend will run on http://localhost:5173
- Open
http://localhost:5173in your browser - Click "Continue with Google"
- Sign in with your Google account
- You'll be redirected to your dashboard with your profile information
| Variable | Description |
|---|---|
MONGODB_URI |
MongoDB Atlas connection string |
GOOGLE_CLIENT_ID |
Google OAuth Client ID |
GOOGLE_CLIENT_SECRET |
Google OAuth Client Secret |
SESSION_SECRET |
Secret key for session encryption |
PORT |
Backend server port (default: 5001) |
CLIENT_URL |
Frontend URL (default: http://localhost:5173) |
Clean, minimal login interface with Google OAuth button.
User profile with name, email, profile photo, and account stats.
- β Secure session-based authentication
- β HTTP-only cookies
- β CORS protection
- β Environment variables for sensitive data
- β MongoDB user data encryption
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
This project is licensed under the MIT License.
Your Name
- GitHub: @yourusername
- Email: your.email@example.com
- Passport.js for authentication
- Google OAuth 2.0
- MongoDB Atlas
- Vite for blazing fast development
Made with β€οΈ and MERN Stack