api_login

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApiLogin

func ApiLogin(w http.ResponseWriter, r *http.Request, dependencies Dependencies)

ApiLogin is the HTTP-level handler that combines passwordless and username+password login flows behind a shared interface.

func ApiLoginWithAuth

func ApiLoginWithAuth(w http.ResponseWriter, r *http.Request, a types.AuthSharedInterface)

ApiLoginWithAuth is a convenience wrapper that allows callers to pass a types.AuthSharedInterface (such as authImplementation) instead of manually wiring Dependencies. It constructs the Dependencies struct using the interface accessors and preserves the existing behaviour.

Types

type Dependencies

type Dependencies struct {
	// Passwordless controls which flow is executed. When true, the passwordless
	// email-code flow is used; otherwise the username+password flow is used.
	Passwordless bool

	// PasswordlessDependencies contains the business-logic dependencies for the
	// passwordless login flow.
	PasswordlessDependencies LoginPasswordlessDeps

	// LoginWithUsernameAndPassword performs the username+password login flow
	// and returns success message, token and error message. If error message is
	// non-empty, the operation is considered failed.
	LoginWithUsernameAndPassword func(
		ctx context.Context,
		email, password, ip, userAgent string,
	) (successMessage, token, errorMessage string)

	// UseCookies controls whether the auth token should be written as a cookie
	// when the username+password flow succeeds.
	UseCookies bool

	// SetAuthCookie writes the auth cookie. It is only used when UseCookies is
	// true and must be non-nil in that case.
	SetAuthCookie func(w http.ResponseWriter, r *http.Request, token string)

	// Logger is used to log internal errors. If nil, slog.Default() is used.
	Logger *slog.Logger
}

Dependencies aggregates all dependencies required for handling the /api/login endpoint for both passwordless and username+password flows.

type LoginPasswordlessDeps

type LoginPasswordlessDeps struct {
	DisableRateLimit bool

	TemporaryKeySet func(key string, value string, expiresSeconds int) error

	ExpiresSeconds int

	EmailTemplate func(ctx context.Context, email string, verificationCode string) string
	EmailSend     func(ctx context.Context, email string, subject string, body string) error
}

LoginPasswordlessDeps defines the dependencies required for the passwordless login flow. It is intentionally decoupled from the authImplementation type to avoid import cycles.

type LoginPasswordlessError

type LoginPasswordlessError struct {
	Code    LoginPasswordlessErrorCode
	Message string
	Err     error
}

LoginPasswordlessError represents a structured error for the passwordless login flow. Message is intended to be user-facing for validation errors. For internal failures, callers are expected to wrap Err into their own error types.

func (*LoginPasswordlessError) Error

func (e *LoginPasswordlessError) Error() string

type LoginPasswordlessErrorCode

type LoginPasswordlessErrorCode string

LoginPasswordlessErrorCode categorizes the possible error sources in the passwordless login flow.

const (
	LoginPasswordlessErrorCodeNone           LoginPasswordlessErrorCode = ""
	LoginPasswordlessErrorCodeValidation     LoginPasswordlessErrorCode = "validation"
	LoginPasswordlessErrorCodeCodeGeneration LoginPasswordlessErrorCode = "code_generation"
	LoginPasswordlessErrorCodeTokenStore     LoginPasswordlessErrorCode = "token_store"
	LoginPasswordlessErrorCodeEmailSend      LoginPasswordlessErrorCode = "email_send"
)

type LoginPasswordlessResult

type LoginPasswordlessResult struct {
	SuccessMessage string
}

LoginPasswordlessResult represents a successful passwordless login operation.

Jump to

Keyboard shortcuts

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