Login With Google
This project demonstrates how to implement Google OAuth authentication using Passport.js in an Express application.
Table of Contents
Features
- Google OAuth 2.0 authentication
- Session management
- EJS as the templating engine
- Static file serving
Technologies Used
- Node.js: JavaScript runtime
- Express: Web application framework
- Passport.js: Authentication middleware for Node.js
- EJS: Templating engine
- dotenv: Environment variable management
Getting Started
Prerequisites
Make sure you have the following installed on your system:
- Node.js (version 14 or higher)
- npm or yarn (for package management)
Setup
-
Clone the repository:
git clone https://github.com/khadka27/LoginWithGoogle.git
cd LoginWithGoogle
-
Install dependencies:
npm install
-
Create a .env
file:
Copy the .env.example
file and rename it to .env
. Then, fill in your Google Client ID, Client Secret, and Session Secret.
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
SESSION_SECRET=your_session_secret
Google Client ID and Secret
To obtain your Google Client ID and Client Secret, follow these steps:
-
Go to the Google Cloud Console:
-
Create a new project:
- Click on the project dropdown at the top of the page and select "New Project."
- Enter a name for your project and click "Create."
-
Enable the Google People API:
- In the navigation menu, go to APIs & Services > Library.
- Search for "Google People API" and click on it.
- Click "Enable."
-
Create OAuth 2.0 credentials:
-
Copy your credentials:
- After creating the credentials, you will see your Client ID and Client Secret. Copy these values and paste them into your
.env
file.
Usage
-
Start the server:
npm start
-
Open your browser:
- Go to
http://localhost:3000/
to see the application.
-
Login with Google:
- Click the "Login with Google" button to initiate the authentication process.
-
Check success or failure:
- If the login is successful, you will be redirected to the Home page.
- If the login fails, you will be redirected to the failure page.
Troubleshooting
- Error: Unknown authentication strategy "google":
- Make sure you have defined the Google authentication strategy in your Passport setup correctly.
- Ensure that the
passport-google-oauth20
package is installed.
- Verify that the Google Client ID and Client Secret are set up properly in the
.env
file.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to copy and paste this into your `README.md` file!