Handler Reference
The Handler struct is responsible for handling all HTTP requests for ezauth. It uses chi for routing.
type Handler struct {
// contains filtered or unexported fields
Session *scs.SessionManager
}
Constructor
New
Creates a new Handler instance.
func New(svc *service.Auth, path string, options ...HandlerOption) *Handler
- svc: The
service.Authinstance. - path: The base path for the auth routes (e.g., "/auth").
- options: Functional options for configuration.
Methods
Run
Starts the HTTP server on the address configured in service.Config.
func (h *Handler) Run()
ServeHTTP
Implements the http.Handler interface, allowing the Handler to be mounted on any Go HTTP router.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP
Implements the http.Handler interface, allowing the Handler to be mounted on any Go HTTP router.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
GetSessionUser
Retrieves the authenticated user. It checks:
- Context (if
LoadUserMiddlewarewas used) - Session Cookies (extracts tokens and verifies with DB)
func (h *Handler) GetSessionUser(ctx context.Context) (*models.User, error)
GetSessionTokens
Retrieves the access and refresh tokens from the session cookies.
func (h *Handler) GetSessionTokens(ctx context.Context) (map[string]string, bool)
IsAuthenticated
Checks if the request is authenticated. It returns true if a user can be retrieved from the context or session.
func (h *Handler) IsAuthenticated(ctx context.Context) bool
HTTP Handlers
The following methods are attached to routes internally by New, but are public if you need to wrap or mock them.
Auth
Login(w, r): JSON LoginRegister(w, r): JSON RegisterLogout(w, r): User logoutRefreshToken(w, r): Refresh access tokenOAuth2Login(w, r): Initiate OAuth2 flowOAuth2Callback(w, r): OAuth2 callback handler
User Management
UserInfo(w, r): Get current user infoDeleteUser(w, r): Delete current user account
Password Management
PasswordResetRequest(w, r): Request password reset linkPasswordResetConfirm(w, r): Confirm password resetPasswordlessRequest(w, r): Request magic linkPasswordlessLogin(w, r): Login via magic link
Form Handlers
These handlers process application/x-www-form-urlencoded requests and return HTML redirects.
FormLogin(w, r)FormRegister(w, r)(Supportspassword_confirmandmeta_fields)FormLogout(w, r)FormPasswordResetRequest(w, r)FormPasswordResetConfirm(w, r)FormPasswordlessRequest(w, r)FormPasswordlessLogin(w, r)