A RESTful API built with Deno, Hono, and PostgreSQL featuring complete authentication and user management.
- ✅ User Authentication (Login, Logout, Refresh Token)
- ✅ User CRUD Operations
- ✅ JWT Token-based Authentication
- ✅ PostgreSQL Database Integration
- ✅ Password Hashing with bcrypt
- ✅ Input Validation with Zod
- ✅ Error Handling
- Runtime: Deno
- Framework: Hono
- Database: PostgreSQL
- Validation: Zod
- Authentication: JWT (djwt)
- Password Hashing: bcrypt
- Deno 1.30 or higher
- PostgreSQL 12 or higher
-
Clone the repository
-
Create a
.envfile from.env.example:
cp .env.example .env-
Update the
.envfile with your database credentials and JWT secrets -
Create a PostgreSQL database:
CREATE DATABASE stackdev;- Run the application:
deno task startThe database tables will be created automatically on first run.
POST /api/auth/login- Login userPOST /api/auth/logout- Logout user (invalidate refresh token)POST /api/auth/logout-all- Logout from all devices (requires auth)POST /api/auth/refresh- Refresh access token
POST /api/users- Register new user (public)GET /api/users/me- Get current user (requires auth)GET /api/users- Get all users (requires auth)GET /api/users/:id- Get user by ID (requires auth)PUT /api/users/:id- Update user (requires auth)DELETE /api/users/:id- Delete user (requires auth)
- Install Deno:
curl -fsSL https://deno.land/install.sh | sh
export DENO_INSTALL="/home/your_username/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
# Create a symbolic link to make Deno globally accessible
sudo ln -s ~/.deno/bin/deno /usr/local/bin/deno
- Verify Deno installation:
which deno
deno --version- Test it works:
deno run -A /var/www/pphat/api.pphat.stackdev.cloud/src/server.ts- Restart PM2:
sudo pm2 delete api.pphat.stackdev.cloud
sudo pm2 start /var/www/pphat/api.pphat.stackdev.cloud/start.sh --name="api.pphat.stackdev.cloud" --interpreter bash
sudo pm2 save├── config/
│ └── env.ts # Environment configuration
├── src/
│ ├── controllers/
│ │ ├── auth.controller.ts # Authentication controllers
│ │ └── user.controller.ts # User CRUD controllers
│ ├── interfaces/
│ │ └── user.ts # TypeScript interfaces
│ ├── middlewares/
│ │ └── auth.middleware.ts # JWT authentication middleware
│ ├── models/
│ │ └── db.ts # Database connection and initialization
│ ├── routes/
│ │ ├── auth.route.ts # Authentication routes
│ │ └── user.route.ts # User routes
│ ├── services/
│ │ ├── auth.service.ts # Authentication business logic
│ │ └── user.service.ts # User business logic
│ ├── utils/
│ │ ├── error.ts # Custom error classes
│ │ ├── jwt.ts # JWT utilities
│ │ ├── password.ts # Password hashing utilities
│ │ └── validator.ts # Zod validation schemas
│ ├── app.ts # Application setup
│ └── server.ts # Server entry point
├── .env.example # Environment variables example
├── deno.json # Deno configuration
└── README.md
| Variable | Description | Default |
|---|---|---|
| DB_HOST | PostgreSQL host | localhost |
| DB_PORT | PostgreSQL port | 5432 |
| DB_NAME | Database name | stackdev |
| DB_USER | Database user | postgres |
| DB_PASSWORD | Database password | postgres |
| JWT_ACCESS_SECRET | Secret for access tokens | (change in production) |
| JWT_REFRESH_SECRET | Secret for refresh tokens | (change in production) |
| PORT | Server port | 3000 |
- Change JWT secrets in production
- Use strong passwords
- Access tokens expire in 15 minutes
- Refresh tokens expire in 7 days
- Passwords are hashed using bcrypt
- All user endpoints (except registration) require authentication
MIT