A secure Node.js authentication system that allows users to sign up/login using a local email-password method or Google OAuth 2.0. Authenticated users can submit and view a personal "secret" stored in a PostgreSQL database.
- User registration & login with hashed passwords (bcrypt)
- Google OAuth 2.0 login (Sign in with Google)
- Session-based authentication using Passport.js
- Protected routes — only logged-in users can access
/secretsand/submit - Submit and view personal secrets stored in PostgreSQL
- Backend: Node.js, Express.js
- Authentication: Passport.js (Local Strategy + Google OAuth2 Strategy)
- Database: PostgreSQL
- Frontend: EJS templating, CSS
- Security: bcrypt (password hashing), express-session
├── views/ │ ├── home.ejs │ ├── login.ejs │ ├── register.ejs │ ├── submit.ejs │ └── secrets.ejs ├── public/ ├── index.js ├── .env └── package.json
- Node.js installed
- PostgreSQL database running
- Google Cloud Console project with OAuth 2.0 credentials
- Clone the repository
git clone https://github.com/your-username/repo-name.git
cd repo-name- Install dependencies
npm install-
Create a
.envfile in the root directory PG_USER=your_postgres_user PG_HOST=localhost PG_DATABASE=your_database_name PG_PASSWORD=your_postgres_password PG_PORT=5432 SESSION_SECRET=your_session_secret GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secret -
Set up the database table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL,
secret TEXT
);- Run the app
node index.jsApp will run at http://localhost:3000
- Go to Google Cloud Console
- Create OAuth 2.0 credentials
- Add
http://localhost:3000/auth/google/secretsas an Authorized redirect URI - Copy Client ID & Client Secret into your
.envfile
| Method | Route | Description |
|---|---|---|
| GET | / |
Home page |
| GET | /login |
Login page |
| GET | /register |
Register page |
| POST | /register |
Create new user |
| POST | /login |
Local login |
| GET | /auth/google |
Google login redirect |
| GET | /auth/google/secrets |
Google OAuth callback |
| GET | /secrets |
View secret (protected) |
| GET | /submit |
Submit secret form (protected) |
| POST | /submit |
Save secret to DB |
| GET | /logout |
Logout user |
Rahul Pawar — GitHub



