Documentation
¶
Index ¶
- Variables
- type Config
- type ErrorResponse
- type Handler
- type Plugin
- func (p *Plugin) ID() string
- func (p *Plugin) Init(authInst core.Authsome) error
- func (p *Plugin) Migrate() error
- func (p *Plugin) RegisterHooks(hookRegistry *hooks.HookRegistry) error
- func (p *Plugin) RegisterRoutes(router forge.Router) error
- func (p *Plugin) RegisterServiceDecorators(_ *registry.ServiceRegistry) error
- type PluginOption
- func WithAutoLoginAfterVerify(enable bool) PluginOption
- func WithAutoSendOnSignup(enable bool) PluginOption
- func WithDefaultConfig(cfg Config) PluginOption
- func WithExpiryHours(hours int) PluginOption
- func WithMaxResendPerHour(max int) PluginOption
- func WithTokenLength(length int) PluginOption
- func WithVerificationURL(url string) PluginOption
- type ResendRequest
- type ResendResponse
- type SendRequest
- type SendResponse
- type Service
- func (s *Service) CleanupExpiredTokens(ctx context.Context) (int64, error)
- func (s *Service) GetStatus(ctx context.Context, userID xid.ID) (*StatusResponse, error)
- func (s *Service) ResendVerification(ctx context.Context, appID xid.ID, email string) error
- func (s *Service) SendVerification(ctx context.Context, appID, userID xid.ID, email string) (string, error)
- func (s *Service) VerifyToken(ctx context.Context, appID xid.ID, token string, autoLogin bool, ip, ua string) (*VerifyResponse, error)
- type StatusResponse
- type VerificationRepository
- func (r *VerificationRepository) CountRecentByUser(ctx context.Context, userID xid.ID, since time.Time) (int, error)
- func (r *VerificationRepository) Create(ctx context.Context, appID, userID xid.ID, token string, expiresAt time.Time) error
- func (r *VerificationRepository) DeleteExpired(ctx context.Context, before time.Time) (int64, error)
- func (r *VerificationRepository) FindByToken(ctx context.Context, token string) (*schema.Verification, error)
- func (r *VerificationRepository) FindByUserID(ctx context.Context, userID xid.ID) (*schema.Verification, error)
- func (r *VerificationRepository) InvalidateOldTokens(ctx context.Context, userID xid.ID) error
- func (r *VerificationRepository) MarkAsUsed(ctx context.Context, verificationID xid.ID) error
- type VerifyRequest
- type VerifyResponse
Constants ¶
This section is empty.
Variables ¶
var ( ErrTokenNotFound = errs.New("TOKEN_NOT_FOUND", "Verification token not found or invalid", 404) ErrTokenExpired = errs.New("TOKEN_EXPIRED", "Verification token has expired", 410) ErrTokenAlreadyUsed = errs.New("TOKEN_USED", "Verification token has already been used", 410) ErrAlreadyVerified = errs.New("ALREADY_VERIFIED", "Email address is already verified", 400) ErrRateLimitExceeded = errs.New("RATE_LIMIT_EXCEEDED", "Too many verification requests, please try again later", 429) ErrUserNotFound = errs.New("USER_NOT_FOUND", "User not found", 404) ErrInvalidEmail = errs.New("INVALID_EMAIL", "Invalid email address", 400) )
Error definitions
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// TokenLength is the length of the verification token in bytes
TokenLength int `json:"tokenLength"`
// ExpiryHours is the token expiry time in hours
ExpiryHours int `json:"expiryHours"`
// MaxResendPerHour is the maximum resend requests per hour per user
MaxResendPerHour int `json:"maxResendPerHour"`
// AutoSendOnSignup automatically sends verification email after signup
AutoSendOnSignup bool `json:"autoSendOnSignup"`
// AutoLoginAfterVerify creates a session after successful verification
AutoLoginAfterVerify bool `json:"autoLoginAfterVerify"`
// VerificationURL is the frontend URL template for verification links
VerificationURL string `json:"verificationURL"`
// DevExposeToken exposes token in response for development/testing
DevExposeToken bool `json:"devExposeToken"`
}
Config holds the email verification plugin configuration
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default email verification plugin configuration
type ErrorResponse ¶
type ErrorResponse = responses.ErrorResponse
Response types - use shared responses from core
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles email verification HTTP endpoints
func NewHandler ¶
NewHandler creates a new email verification handler
func (*Handler) Resend ¶
Resend handles resending verification email POST /email-verification/resend
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin implements the email verification plugin
func NewPlugin ¶
func NewPlugin(opts ...PluginOption) *Plugin
NewPlugin creates a new email verification plugin instance
func (*Plugin) RegisterHooks ¶
func (p *Plugin) RegisterHooks(hookRegistry *hooks.HookRegistry) error
func (*Plugin) RegisterServiceDecorators ¶
func (p *Plugin) RegisterServiceDecorators(_ *registry.ServiceRegistry) error
type PluginOption ¶
type PluginOption func(*Plugin)
PluginOption is a functional option for configuring the email verification plugin
func WithAutoLoginAfterVerify ¶
func WithAutoLoginAfterVerify(enable bool) PluginOption
WithAutoLoginAfterVerify sets whether to auto-login after verification
func WithAutoSendOnSignup ¶
func WithAutoSendOnSignup(enable bool) PluginOption
WithAutoSendOnSignup sets whether to automatically send verification on signup
func WithDefaultConfig ¶
func WithDefaultConfig(cfg Config) PluginOption
WithDefaultConfig sets the default configuration for the plugin
func WithExpiryHours ¶
func WithExpiryHours(hours int) PluginOption
WithExpiryHours sets the token expiry time in hours
func WithMaxResendPerHour ¶
func WithMaxResendPerHour(max int) PluginOption
WithMaxResendPerHour sets the maximum resend requests per hour
func WithTokenLength ¶
func WithTokenLength(length int) PluginOption
WithTokenLength sets the verification token length
func WithVerificationURL ¶
func WithVerificationURL(url string) PluginOption
WithVerificationURL sets the frontend verification URL
type ResendRequest ¶
type ResendRequest struct {
Email string `json:"email" validate:"required,email" example:"user@example.com"`
}
type ResendResponse ¶
type ResendResponse struct {
Status string `json:"status" example:"sent"`
}
type SendRequest ¶
type SendRequest struct {
Email string `json:"email" validate:"required,email" example:"user@example.com"`
}
Request types
type SendResponse ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service implements email verification logic
func NewService ¶
func NewService( repo *VerificationRepository, userSvc user.ServiceInterface, sessionSvc session.ServiceInterface, notifAdapter *notificationPlugin.Adapter, cfg Config, logger forge.Logger, ) *Service
NewService creates a new email verification service
func (*Service) CleanupExpiredTokens ¶
CleanupExpiredTokens removes expired verification tokens (for scheduled cleanup)
func (*Service) ResendVerification ¶
ResendVerification sends a new verification email
type StatusResponse ¶
type VerificationRepository ¶
type VerificationRepository struct {
// contains filtered or unexported fields
}
VerificationRepository handles database operations for email verification tokens
func NewVerificationRepository ¶
func NewVerificationRepository(db *bun.DB) *VerificationRepository
NewVerificationRepository creates a new verification repository
func (*VerificationRepository) CountRecentByUser ¶
func (r *VerificationRepository) CountRecentByUser(ctx context.Context, userID xid.ID, since time.Time) (int, error)
CountRecentByUser counts recent verification requests by a user (for rate limiting)
func (*VerificationRepository) Create ¶
func (r *VerificationRepository) Create(ctx context.Context, appID, userID xid.ID, token string, expiresAt time.Time) error
Create creates a new email verification record
func (*VerificationRepository) DeleteExpired ¶
func (r *VerificationRepository) DeleteExpired(ctx context.Context, before time.Time) (int64, error)
DeleteExpired deletes expired verification tokens
func (*VerificationRepository) FindByToken ¶
func (r *VerificationRepository) FindByToken(ctx context.Context, token string) (*schema.Verification, error)
FindByToken finds a verification record by token
func (*VerificationRepository) FindByUserID ¶
func (r *VerificationRepository) FindByUserID(ctx context.Context, userID xid.ID) (*schema.Verification, error)
FindByUserID finds the latest email verification for a user
func (*VerificationRepository) InvalidateOldTokens ¶
InvalidateOldTokens marks all unused tokens for a user as used (when sending new verification)
func (*VerificationRepository) MarkAsUsed ¶
MarkAsUsed marks a verification token as used
type VerifyRequest ¶
type VerifyRequest struct {
Token string `query:"token" validate:"required" example:"abc123xyz789"`
}