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.go
The 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
/login
with credentials - On successful login, a session cookie is set
- Use this cookie for subsequent requests to protected routes
- Call
/logout
to end the session
Run the test suite:
go test -v
This is a demonstration project. For production use:
- Replace the hard-coded secret key
- Use secure password hashing
- Implement proper user storage
- Use HTTPS