Authentication
The scaffold includes a complete JWT-based authentication system.
Features
- JWT token with configurable expiration (default: 24 hours)
- bcrypt password hashing
- Role-based access (admin / user)
- Token refresh via re-login
API Endpoints
| Method | Path | Description |
|---|---|---|
| POST | /api/auth/login | Login with username + password |
| POST | /api/auth/register | Register a new user |
| GET | /api/user/profile | Get current user info (requires auth) |
Configuration
In config.yaml:
yaml
jwt:
secret: your-secret-key # Change this in production!
expire: 24 # Token expiry in hours
issuer: your-project-nameProtected Routes
Add the JWTAuth middleware to any route group:
go
authorized := api.Group("/")
authorized.Use(middleware.JWTAuth(&cfg.JWT))
{
authorized.GET("/user/profile", profileHandler())
}