A full-stack Login & Registration system built with Node.js + Express, PostgreSQL, and React.
| Layer | Tech |
|---|---|
| Backend | Node.js, Express |
| Database | PostgreSQL |
| Auth | JWT (jsonwebtoken) + bcryptjs |
| Frontend | React 18, React Router v6 |
| HTTP | Axios |
auth-system/
├── backend/
│ ├── src/
│ │ ├── config/db.js # PostgreSQL connection pool
│ │ ├── middleware/auth.js # JWT verification middleware
│ │ ├── controllers/
│ │ │ └── authController.js # register, login, getProfile
│ │ ├── routes/auth.js # Route definitions
│ │ └── index.js # Express server entry
│ ├── schema.sql # Database schema
│ ├── .env.example # Environment variable template
│ └── package.json
│
└── frontend/
├── public/index.html
├── src/
│ ├── context/AuthContext.js # Global auth state
│ ├── components/
│ │ └── ProtectedRoute.js # Route guard
│ ├── pages/
│ │ ├── Register.js
│ │ ├── Login.js
│ │ └── Profile.js
│ ├── utils/api.js # Axios instance + interceptors
│ ├── App.js # Router setup
│ ├── App.css # Global styles
│ └── index.js
└── package.json
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);- Node.js v18+
- PostgreSQL v14+
# Connect to PostgreSQL
psql -U postgres
# Create the database
CREATE DATABASE auth_db;
\c auth_db
# Run the schema
\i backend/schema.sqlcd backend
# Install dependencies
npm install
# Create your .env file
cp .env.example .envEdit .env with your actual values:
PORT=5000
DB_HOST=localhost
DB_PORT=5432
DB_NAME=auth_db
DB_USER=postgres
DB_PASSWORD=your_postgres_password
JWT_SECRET=some_long_random_secret_string
JWT_EXPIRES_IN=7d# Start the backend
npm run dev # development (with nodemon)
npm start # productionBackend will run at: http://localhost:5000
cd frontend
# Install dependencies
npm install
# Start the React app
npm startFrontend will run at: http://localhost:3000
The
"proxy": "http://localhost:5000"inpackage.jsonhandles CORS in dev.
Register a new user.
Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "secret123"
}Response (201):
{
"message": "Account created successfully.",
"token": "<JWT>",
"user": { "id": 1, "name": "John Doe", "email": "john@example.com" }
}Login with email and password.
Request Body:
{
"email": "john@example.com",
"password": "secret123"
}Response (200):
{
"message": "Login successful.",
"token": "<JWT>",
"user": { "id": 1, "name": "John Doe", "email": "john@example.com" }
}Get the authenticated user's profile.
Headers:
Authorization: Bearer <JWT>
Response (200):
{
"user": { "id": 1, "name": "John Doe", "email": "john@example.com", "createdAt": "..." }
}- Password hashing: bcryptjs with salt rounds of 12 — industry standard, computationally expensive for attackers.
- JWT: Stateless authentication. Token signed with a secret key, expires in 7 days. Sent as
Bearertoken inAuthorizationheader. - No password in response: The
passwordfield is never returned in any API response. - Generic error messages: Login returns
"Invalid email or password"for both wrong email and wrong password — prevents user enumeration.
- Controller-Route separation: Business logic stays in controllers, routes only define HTTP mappings — clean and scalable.
- Input validation: All inputs validated before hitting the DB (format, length, required fields).
- Global error handling: Express
use()error middleware catches unhandled errors gracefully. - Connection pooling:
pg.Poolreuses database connections efficiently.
- React Context API: Global
AuthContextstores user state. Avoids prop-drilling. - Axios interceptors: Automatically attaches JWT to every request. Handles 401/403 by logging out the user.
- Protected Routes:
ProtectedRoutecomponent redirects unauthenticated users to/login. - Session persistence: On app load, if a token exists in
localStorage, the profile is fetched to restore the session. - React Router v6: Declarative routing with
<Navigate>for redirects and nested route structure.
MIT