Overview
POS Kasir implements a secure JWT-based authentication system using HTTP-only cookies for token storage. The system supports role-based access control (RBAC) with three user roles: Admin, Manager, and Cashier.Authentication Flow
Login Process
The login endpoint authenticates users and returns access and refresh tokens via cookies. Endpoint:POST /auth/login
internal/user/auth_handler.go:125-221
Token Verification
The authentication middleware verifies JWT tokens on protected routes.internal/common/middleware/auth.go:12-44
Token Refresh
The system uses refresh tokens to obtain new access tokens without re-authentication. Endpoint:POST /auth/refresh
internal/user/auth_handler.go:517-583
Refresh Token Service Logic
The service implements single-session enforcement by validating tokens against the database.internal/user/auth_service.go:382-443
Logout
Logout clears authentication cookies. Endpoint:POST /auth/logout
internal/user/auth_handler.go:223-260
User Profile
Authenticated users can retrieve their profile information. Endpoint:GET /auth/me
internal/user/auth_handler.go:262-328
Password Update
Users can update their password while authenticated. Endpoint:PUT /auth/me/password
Request Body:
internal/user/auth_service.go:88-137
Security Features
HTTP-Only Cookies
Tokens are stored in HTTP-only cookies to prevent XSS attacks:access_token- Short-lived JWT for API authenticationrefresh_token- Long-lived token for obtaining new access tokens
Single Session Enforcement
Only one refresh token per user is valid at a time. When a new refresh token is issued:- The old refresh token is invalidated
- Only the latest token stored in the database is accepted
Token Rotation
Each refresh request generates a new pair of tokens, limiting the window of opportunity for token theft.Role-Based Access Control
TheRoleMiddleware enforces minimum role requirements for routes:
internal/common/middleware/role.go:15-47
Best Practices
- Always use HTTPS in production - The
Secureflag is automatically set based on environment - Handle token expiration gracefully - Implement automatic refresh on 401 responses
- Clear tokens on logout - Both client and server should invalidate tokens
- Validate user permissions - Check role requirements before performing sensitive operations
- Log authentication events - All login attempts (success/failure) are logged for audit trails