A secure authentication service built with Node.js, Express, PostgreSQL, and JWT tokens.
- ✅ User registration with email and password
- ✅ Secure login with JWT tokens
- ✅ Role-based access control (User/Admin)
- ✅ Password hashing with bcrypt
- ✅ Protected routes with middleware
- ✅ Clean frontend UI for testing
- Backend: Node.js, Express
- Database: PostgreSQL
- Authentication: JWT, bcrypt
- Frontend: Vanilla HTML/CSS/JavaScript
- Node.js (v14 or higher)
- PostgreSQL (v12 or higher)
- npm or yarn
cd backend
npm installCreate a .env file in the root directory:
cp .env.example .envEdit .env with your configuration:
DATABASE_URL=postgresql://username:password@localhost:5432/auth_db
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
PORT=5000Create the database and run the schema:
# Create database (using psql)
createdb auth_db
# Or using psql command line
psql -U postgres -c "CREATE DATABASE auth_db;"
# Run the schema
psql -U postgres -d auth_db -f backend/schema.sqlcd backend
npm run devThe server will start on http://localhost:3001
Open your browser and navigate to:
http://localhost:3001
-
POST /auth/register- Register a new user{ "email": "user@example.com", "password": "password123", "role": "user" } -
POST /auth/login- Login and get JWT token{ "email": "user@example.com", "password": "password123" }
GET /auth/me- Get current user info (requires JWT token)GET /auth/admin- Admin-only endpoint (requires admin role)
GET /- Service health checkGET /db-check- Database connection check
auth-service-main/
├── backend/
│ ├── src/
│ │ ├── controllers/
│ │ │ └── auth.controller.js
│ │ ├── middleware/
│ │ │ ├── auth.middleware.js
│ │ │ └── role.middleware.js
│ │ ├── routes/
│ │ │ └── auth.routes.js
│ │ ├── db.js
│ │ └── server.js
│ ├── package.json
│ └── schema.sql
├── public/
│ ├── index.html
│ ├── app.js
│ └── style.css
├── .env
├── .env.example
├── .gitignore
└── README.md
- ✅ Password hashing with bcrypt (10 salt rounds)
- ✅ JWT tokens with 15-minute expiration
- ✅ SQL injection protection via parameterized queries
- ✅ Role-based access control
- ✅ Environment variable protection
- Register a new user: Use the Signup tab
- Login: Use the Login tab with your credentials
- View profile: Check the Profile tab after logging in
- Test admin access: Create an admin user and check the Admin tab
- Ensure PostgreSQL is running
- Verify DATABASE_URL in
.envis correct - Check if the database exists
- Ensure JWT_SECRET is set in
.env - Check if token is being sent in Authorization header
- The default port is 3001 (to avoid conflict with macOS AirPlay on port 5000)
- Change PORT in
.envif needed - Kill the process using the port:
lsof -ti:3001 | xargs kill -9
ISC