A simple example demonstrating how to implement cookie-based session authentication in Go using Gin. This project showcases basic authentication flows including login, logout, and protected routes.
- Cookie-based session management
- Protected routes using middleware
- Basic authentication flow (login/logout)
- Session persistence using encrypted cookies
# Clone the repository
git clone https://github.com/depado/gin-auth-example
cd gin-auth-example
# Run the server
go run main.goThe server will start on localhost:8080
POST /login: Authenticate user- Body:
{"username": "hello", "password": "itsme"}
- Body:
GET /logout: End user session
GET /private/me: Get current user informationGET /private/status: Get login status
- Send a POST request to
/loginwith credentials - On successful login, a session cookie is set
- Use this cookie for subsequent requests to protected routes
- Call
/logoutto end the session
Run the test suite:
go test -vThis is a demonstration project. For production use:
- Replace the hard-coded secret key
- Use secure password hashing
- Implement proper user storage
- Use HTTPS