A RESTful authentication service built with Go, PostgreSQL, JWT, and Docker. Designed for integration with a SvelteKit + Material 3 frontend.
cd /path/to/userPanel
Create or update backend/.env:
PORT=8080
DATABASE_URL=postgres://postgres:password@db:5432/authdb?sslmode=disable
JWT_SECRET=your-super-secret-jwt-key-change-in-production
CORS_ORIGIN=http://localhost:5173
# Start with Docker Compose
docker compose up --build
# Or in detached mode
docker compose up --build -d
# View logs
docker compose logs -f api
The API will be available at http://localhost:8080
GET /api/healthCheck service status and uptime.
Response:
{
"success": true,
"data": {
"status": "ok",
"uptime": "5m 23s"
}
}
POST /api/registerRegister a new user account.
Request Body:
{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}
Validation:
Success Response (200):
{
"success": true,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"created_at": "2025-11-01T17:51:14Z",
"updated_at": "2025-11-01T17:51:14Z"
}
}
}
Error Response (409):
{
"success": false,
"message": "Email already registered"
}
POST /api/loginAuthenticate and receive a JWT token.
Request Body:
{
"email": "[email protected]",
"password": "password123"
}
Success Response (200):
{
"success": true,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"created_at": "2025-11-01T17:51:14Z",
"updated_at": "2025-11-01T17:51:14Z"
}
}
}
Error Response (401):
{
"success": false,
"message": "Invalid email or password"
}
All profile endpoints require authentication via JWT token in the Authorization header:
Authorization: Bearer <your-jwt-token>
GET /api/profileGet authenticated user's profile.
Headers:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Success Response (200):
{
"success": true,
"data": {
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"avatar": "https://example.com/avatar.jpg",
"created_at": "2025-11-01T17:51:14Z",
"updated_at": "2025-11-01T17:51:14Z"
}
}
Error Response (401):
{
"success": false,
"message": "Invalid or expired token"
}
PUT /api/profileUpdate authenticated user's profile.
Headers:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json
Request Body:
{
"name": "Jane Doe",
"avatar": "https://example.com/new-avatar.jpg"
}
Success Response (200):
{
"success": true,
"data": {
"id": 1,
"name": "Jane Doe",
"email": "[email protected]",
"avatar": "https://example.com/new-avatar.jpg",
"created_at": "2025-11-01T17:51:14Z",
"updated_at": "2025-11-01T17:52:05Z"
}
}
DELETE /api/profileDelete authenticated user's account (soft delete).
Headers:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Success Response (200):
{
"success": true,
"message": "Account deleted successfully"
}
# Health check
curl http://localhost:8080/api/health
# Register
curl -X POST http://localhost:8080/api/register \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"[email protected]","password":"password123"}'
# Login
curl -X POST http://localhost:8080/api/login \
-H "Content-Type: application/json" \
-d '{"email":"[email protected]","password":"password123"}'
# Get Profile (replace TOKEN with your JWT)
curl -X GET http://localhost:8080/api/profile \
-H "Authorization: Bearer <TOKEN>"
# Update Profile
curl -X PUT http://localhost:8080/api/profile \
-H "Authorization: Bearer <TOKEN>" \
-H "Content-Type: application/json" \
-d '{"name":"Jane Doe","avatar":"https://example.com/avatar.jpg"}'
# Delete Account
curl -X DELETE http://localhost:8080/api/profile \
-H "Authorization: Bearer <TOKEN>"
backend/
โโโ cmd/
โ โโโ auth-service/
โ โโโ main.go # Application entry point
โโโ config/
โ โโโ config.go # Configuration management
โโโ internal/
โ โโโ handlers/
โ โ โโโ auth.go # Auth endpoints (register, login)
โ โ โโโ user.go # User profile endpoints
โ โ โโโ health.go # Health check endpoint
โ โโโ middleware/
โ โ โโโ auth.go # JWT authentication middleware
โ โ โโโ logger.go # Request logging middleware
โ โโโ models/
โ โ โโโ user.go # User database model
โ โโโ utils/
โ โโโ jwt.go # JWT utilities
โ โโโ password.go # Password hashing
โ โโโ response.go # JSON response helpers
โโโ routes/
โ โโโ routes.go # Route definitions
โโโ db/
โ โโโ migrations/ # SQL migrations (optional)
โ โโโ queries/ # SQL queries (if using sqlc)
โโโ Dockerfile
โโโ .env # Environment variables
โโโ .env.example # Example environment file
# Install dependencies
go mod download
# Run database migrations (ensure PostgreSQL is running)
# Update DATABASE_URL in .env to point to your local DB
# Run the server
go run ./backend/cmd/auth-service/main.go
The User model is auto-migrated on startup:
type User struct {
ID uint // Primary key
Name string // User's full name
Email string // Unique email address
PasswordHash string // bcrypt hashed password
Avatar string // Profile avatar URL (optional)
CreatedAt time.Time // Account creation timestamp
UpdatedAt time.Time // Last update timestamp
DeletedAt *time.Time // Soft delete timestamp
}
# Build and start
docker compose up --build
# Stop services
docker compose down
# View logs
docker compose logs -f api
# Restart API only
docker compose restart api
# Remove volumes (caution: deletes database data)
docker compose down -v
// Login function
async function login(email, password) {
const response = await fetch('http://localhost:8080/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
const data = await response.json();
if (data.success) {
localStorage.setItem('token', data.data.token);
return data.data.user;
}
throw new Error(data.message);
}
// Authenticated request
async function getProfile() {
const token = localStorage.getItem('token');
const response = await fetch('http://localhost:8080/api/profile', {
headers: { 'Authorization': `Bearer ${token}` }
});
const data = await response.json();
if (data.success) return data.data;
throw new Error(data.message);
}
| Variable | Description | Default | Required |
|---|---|---|---|
PORT |
Server port | 8080 |
No |
DATABASE_URL |
PostgreSQL connection string | - | Yes |
JWT_SECRET |
Secret key for JWT signing | - | Yes |
CORS_ORIGIN |
Allowed CORS origin | http://localhost:5173 |
No |
This project is part of the userPanel application.
# Check what's using port 8080
sudo lsof -i :8080
# Or change the port in .env
PORT=8081
docker compose ps.env matches docker-compose.yml settingsdocker network lsBearer <token>Built with โค๏ธ using Go, PostgreSQL, GORM, Chi, and Docker