Skip to content

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

MethodPathDescription
POST/api/auth/loginLogin with username + password
POST/api/auth/registerRegister a new user
GET/api/user/profileGet 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-name

Protected Routes

Add the JWTAuth middleware to any route group:

go
authorized := api.Group("/")
authorized.Use(middleware.JWTAuth(&cfg.JWT))
{
    authorized.GET("/user/profile", profileHandler())
}