handler

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultCookiePrefix is the default prefix for all cookies.
	DefaultCookiePrefix = "go-better-auth"
	// SessionCookieName is the default name for the session cookie.
	SessionCookieName = "session_token"
)

Variables

This section is empty.

Functions

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, code int, message string)

ErrorResponse writes a JSON error response

func NewAuthHandler

func NewAuthHandler(service *auth.Service, cookieManager *CookieManager) http.Handler

NewAuthHandler creates a new auth handler

func SuccessResponse

func SuccessResponse(w http.ResponseWriter, code int, data any)

SuccessResponse writes a JSON success response

Types

type AuthHandler

type AuthHandler struct {
	// contains filtered or unexported fields
}

AuthHandler implements http.Handler for all auth endpoints

func (*AuthHandler) ChangeEmailHandler

func (h *AuthHandler) ChangeEmailHandler(w http.ResponseWriter, r *http.Request)

ChangeEmailHandler handles POST /auth/change-email Requires AuthMiddleware to be applied to extract user ID from context Initiates an email change by sending a verification token to the new email

func (*AuthHandler) DeleteProfileHandler

func (h *AuthHandler) DeleteProfileHandler(w http.ResponseWriter, r *http.Request)

DeleteProfileHandler handles DELETE /auth/me Requires AuthMiddleware to be applied to extract user ID from context

func (*AuthHandler) GetMeHandler

func (h *AuthHandler) GetMeHandler(w http.ResponseWriter, r *http.Request)

GetMeHandler handles GET /auth/me Requires AuthMiddleware to be applied to extract user ID from context

func (*AuthHandler) RefreshTokenHandler

func (h *AuthHandler) RefreshTokenHandler(w http.ResponseWriter, r *http.Request)

RefreshTokenHandler handles POST /auth/refresh

func (*AuthHandler) ResetPasswordHandler

func (h *AuthHandler) ResetPasswordHandler(w http.ResponseWriter, r *http.Request)

ResetPasswordHandler handles POST /auth/reset-password

func (*AuthHandler) SendEmailVerificationHandler

func (h *AuthHandler) SendEmailVerificationHandler(w http.ResponseWriter, r *http.Request)

SendEmailVerificationHandler handles POST /auth/email-verification

func (*AuthHandler) SendPasswordResetHandler

func (h *AuthHandler) SendPasswordResetHandler(w http.ResponseWriter, r *http.Request)

SendPasswordResetHandler handles POST /auth/password-reset

func (*AuthHandler) ServeHTTP

func (h *AuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches requests to appropriate handlers

func (*AuthHandler) SignInHandler

func (h *AuthHandler) SignInHandler(w http.ResponseWriter, r *http.Request)

SignInHandler handles POST /auth/signin

func (*AuthHandler) SignOutHandler

func (h *AuthHandler) SignOutHandler(w http.ResponseWriter, r *http.Request)

SignOutHandler handles POST /auth/signout

func (*AuthHandler) SignUpHandler

func (h *AuthHandler) SignUpHandler(w http.ResponseWriter, r *http.Request)

SignUpHandler handles POST /auth/signup

func (*AuthHandler) UpdateProfileHandler

func (h *AuthHandler) UpdateProfileHandler(w http.ResponseWriter, r *http.Request)

UpdateProfileHandler handles PATCH /auth/me Requires AuthMiddleware to be applied to extract user ID from context

func (*AuthHandler) ValidateSessionHandler

func (h *AuthHandler) ValidateSessionHandler(w http.ResponseWriter, r *http.Request)

ValidateSessionHandler handles GET /auth/validate

func (*AuthHandler) VerifyEmailHandler

func (h *AuthHandler) VerifyEmailHandler(w http.ResponseWriter, r *http.Request)

VerifyEmailHandler handles GET /auth/verify-email?token={token}&callbackURL={callbackURL} OR POST /auth/verify-email This unified handler verifies all types of verification tokens: - Email verification - Email change confirmation - Password reset confirmation It extracts the token from query parameters or POST body, validates it, and redirects to a success page or returns a response.

type ChangeEmailRequest

type ChangeEmailRequest struct {
	NewEmail    string `json:"new_email"`
	CallbackURL string `json:"callback_url,omitempty"`
}

type ChangeEmailResponse

type ChangeEmailResponse struct {
	Status  bool   `json:"status"`
	Message string `json:"message"`
}

type CookieManager

type CookieManager struct {
	// contains filtered or unexported fields
}

CookieManager handles setting and clearing cookies with configurable options.

func NewCookieManager

func NewCookieManager(cfg *domain.Config) *CookieManager

NewCookieManager creates a new CookieManager based on the application configuration. It centralizes the logic for determining cookie names, security, and domain settings.

func (*CookieManager) ClearSessionCookie

func (cm *CookieManager) ClearSessionCookie(w http.ResponseWriter)

ClearSessionCookie clears the session cookie using the configured settings.

func (*CookieManager) GetSessionCookieName

func (cm *CookieManager) GetSessionCookieName() string

GetSessionCookieName returns the configured name for the session cookie. This is useful for middleware or other parts of the app that need to read the cookie.

func (*CookieManager) SetSessionCookie

func (cm *CookieManager) SetSessionCookie(w http.ResponseWriter, token string, expiresAt time.Time)

SetSessionCookie sets the session token as a cookie using the configured settings.

type DeleteProfileRequest

type DeleteProfileRequest struct {
	ConfirmPassword string `json:"confirm_password"`
}

type DeleteProfileResponse

type DeleteProfileResponse struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
}

type GetMeRequest

type GetMeRequest struct {
	UserID string `json:"user_id"`
}

type GetMeResponse

type GetMeResponse struct {
	User *user.User `json:"user"`
}

type HTTPError

type HTTPError struct {
	Code    int
	Message string
}

HTTPError represents an HTTP error with status code

func BadRequest

func BadRequest(message string) *HTTPError

BadRequest creates a 400 error

func Conflict

func Conflict(message string) *HTTPError

Conflict creates a 409 error

func InternalServerError

func InternalServerError(message string) *HTTPError

InternalServerError creates a 500 error

func NewHTTPError

func NewHTTPError(code int, message string) *HTTPError

NewHTTPError creates a new HTTP error

func NotFound

func NotFound(message string) *HTTPError

NotFound creates a 404 error

func Unauthorized

func Unauthorized(message string) *HTTPError

Unauthorized creates a 401 error

type OAuthAuthorizeRequest

type OAuthAuthorizeRequest struct {
	// RedirectURI is where the user should be redirected after OAuth flow completes
	RedirectURI string `json:"redirect_uri"`
	// State is optional additional state to preserve through the OAuth flow
	State string `json:"state"`
}

OAuthAuthorizeRequest represents the query parameters for OAuth authorization

type OAuthCallbackRequest

type OAuthCallbackRequest struct {
	Code  string `json:"code"`
	State string `json:"state"`
	Error string `json:"error"`
}

OAuthCallbackRequest represents the query parameters for OAuth callback

type OAuthHandler

type OAuthHandler struct {
	// contains filtered or unexported fields
}

OAuthHandler handles OAuth authentication endpoints

func NewOAuthHandler

func NewOAuthHandler(
	service *auth.Service,
	stateManager *storage.OAuthStateManager,
	providerReg account.OAuthProviderRegistry,
) *OAuthHandler

NewOAuthHandler creates a new OAuth handler

func (*OAuthHandler) HandleOAuthAuthorize

func (h *OAuthHandler) HandleOAuthAuthorize(w http.ResponseWriter, r *http.Request)

HandleOAuthAuthorize initiates the OAuth authorization flow GET /auth/oauth/{provider}

func (*OAuthHandler) HandleOAuthCallback

func (h *OAuthHandler) HandleOAuthCallback(w http.ResponseWriter, r *http.Request)

HandleOAuthCallback handles the OAuth callback from the provider GET /auth/oauth/{provider}/callback

func (*OAuthHandler) HandleOAuthLinkedAccounts

func (h *OAuthHandler) HandleOAuthLinkedAccounts(w http.ResponseWriter, r *http.Request)

HandleOAuthLinkedAccounts returns all OAuth accounts linked to the user GET /auth/oauth/accounts

func (h *OAuthHandler) HandleOAuthUnlink(w http.ResponseWriter, r *http.Request)

HandleOAuthUnlink unlinks an OAuth account from the user DELETE /auth/oauth/{provider}

func (*OAuthHandler) ServeHTTP

func (h *OAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler for OAuth routes

type RefreshTokenRequest

type RefreshTokenRequest struct {
	Token string `json:"token"`
}

type RefreshTokenResponse

type RefreshTokenResponse struct {
	Token     string    `json:"token"`
	ExpiresAt time.Time `json:"expires_at"`
}

type RequestPasswordResetRequest

type RequestPasswordResetRequest struct {
	Email       string `json:"email"`
	CallbackURL string `json:"callback_url,omitempty"`
}

type RequestPasswordResetResponse

type RequestPasswordResetResponse struct {
	Message string `json:"message"`
}

type ResetPasswordRequest

type ResetPasswordRequest struct {
	Token       string `json:"token"`
	NewPassword string `json:"new_password"`
}

type ResetPasswordResponse

type ResetPasswordResponse struct {
	Message string `json:"message"`
}

type Response

type Response struct {
	Success bool   `json:"success"`
	Data    any    `json:"data,omitempty"`
	Error   string `json:"error,omitempty"`
}

Response is the standard API response envelope

type SendEmailVerificationRequest

type SendEmailVerificationRequest struct {
	Email       string `json:"email"`
	CallbackURL string `json:"callback_url,omitempty"`
}

type SendEmailVerificationResponse

type SendEmailVerificationResponse struct {
	Status bool `json:"status"`
}

type SignInRequest

type SignInRequest struct {
	Email       string `json:"email"`
	Password    string `json:"password"`
	CallbackURL string `json:"callback_url,omitempty"`
}

type SignInResponse

type SignInResponse struct {
	Token string     `json:"token"`
	User  *user.User `json:"user"`
}

type SignOutRequest

type SignOutRequest struct {
	Token string `json:"token"`
}

type SignUpRequest

type SignUpRequest struct {
	Email       string `json:"email"`
	Password    string `json:"password"`
	Name        string `json:"name"`
	CallbackURL string `json:"callback_url,omitempty"`
}

type SignUpResponse

type SignUpResponse struct {
	Token string     `json:"token"`
	User  *user.User `json:"user"`
}

type UpdateProfileRequest

type UpdateProfileRequest struct {
	Name  *string `json:"name,omitempty"`
	Image *string `json:"image,omitempty"`
}

type UpdateProfileResponse

type UpdateProfileResponse struct {
	ID            string `json:"id"`
	Email         string `json:"email"`
	Name          string `json:"name"`
	EmailVerified bool   `json:"email_verified"`
	Image         string `json:"image,omitempty"`
}

type ValidateSessionRequest

type ValidateSessionRequest struct {
	Token string `json:"token"`
}

type ValidateSessionResponse

type ValidateSessionResponse struct {
	Valid     bool      `json:"valid"`
	UserID    string    `json:"user_id"`
	ExpiresAt time.Time `json:"expires_at"`
}

type VerifyEmailResponse

type VerifyEmailResponse struct {
	Status bool   `json:"status"`
	Type   string `json:"type"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL