Skip to content

A sleek and secure authentication system powered by Google OAuth 2.0, built using the MERN stack. Modern UI, smooth login experience, and full JWT-based sessions β€” plug-and-play for any project. πŸš€

Notifications You must be signed in to change notification settings

CodexAde/Google-Auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” Google Auth MERN

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. πŸš€


✨ Features

  • 🎨 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)

πŸ› οΈ Tech Stack

Frontend

  • React (Vite)
  • Axios for API calls
  • Context API for state management
  • CSS3 with Apple-inspired design system

Backend

  • 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

πŸ“ Project Structure

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

πŸš€ Getting Started

Prerequisites

  • Node.js (v16 or higher)
  • MongoDB Atlas account
  • Google Cloud Console account

1️⃣ Clone the Repository

git clone https://github.com/yourusername/google-auth-mern.git
cd google-auth-mern

2️⃣ Setup Google OAuth Credentials

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Navigate to APIs & Services > Credentials
  4. Click Create Credentials > OAuth client ID
  5. Select Web application
  6. Add the following:
    • Authorized JavaScript origins:
      • http://localhost:5001
      • http://localhost:5173
    • Authorized redirect URIs:
      • http://localhost:5001/auth/google/callback
  7. Copy your Client ID and Client Secret

3️⃣ Backend Setup

cd backend
npm install

Create 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:5173

Start the backend server:

npm run dev

Backend will run on http://localhost:5001


4️⃣ Frontend Setup

cd frontend
npm install

Start the frontend:

npm run dev

Frontend will run on http://localhost:5173


🎯 Usage

  1. Open http://localhost:5173 in your browser
  2. Click "Continue with Google"
  3. Sign in with your Google account
  4. You'll be redirected to your dashboard with your profile information

πŸ”‘ Environment Variables

Backend .env

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)

πŸ“Έ Screenshots

Login Page

Clean, minimal login interface with Google OAuth button.

Dashboard

User profile with name, email, profile photo, and account stats.


πŸ”’ Security Features

  • βœ… Secure session-based authentication
  • βœ… HTTP-only cookies
  • βœ… CORS protection
  • βœ… Environment variables for sensitive data
  • βœ… MongoDB user data encryption

🀝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests

πŸ“ License

This project is licensed under the MIT License.


πŸ‘¨β€πŸ’» Author

Your Name


πŸ™ Acknowledgments


Made with ❀️ and MERN Stack

About

A sleek and secure authentication system powered by Google OAuth 2.0, built using the MERN stack. Modern UI, smooth login experience, and full JWT-based sessions β€” plug-and-play for any project. πŸš€

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors