user

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeSuperuser = "superuser"
	TypeRegular   = "regular"
)
View Source
const (
	// UserResourceSingular is the singular name for the built-in user resource.
	UserResourceSingular = "user"
	// UserResourcePlural is the plural name for the built-in user resource.
	UserResourcePlural = "users"
)

Variables

This section is empty.

Functions

func CountUsers

func CountUsers(d *sql.DB) (int, error)

CountUsers returns the number of users.

func CreateTokensTable

func CreateTokensTable(d *sql.DB) error

CreateTokensTable creates the _tokens table if it does not exist.

func CreateUsersTable

func CreateUsersTable(d *sql.DB) error

CreateUsersTable creates the _users table if it does not exist.

func DeleteToken

func DeleteToken(d *sql.DB, token string) error

DeleteToken removes a specific token.

func DeleteTokensByUser

func DeleteTokensByUser(d *sql.DB, userID string) error

DeleteTokensByUser removes all tokens for a user.

func DeleteUser

func DeleteUser(d *sql.DB, id string) (bool, error)

DeleteUser deletes a user by ID. Returns true if a row was deleted.

func GenerateID

func GenerateID() string

GenerateID creates a hex-encoded nanosecond timestamp ID (matches aepbase pattern).

func GenerateToken

func GenerateToken() (string, error)

GenerateToken creates a cryptographically random token string.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword returns a bcrypt hash of the given password.

func InsertToken

func InsertToken(d *sql.DB, token, userID string) error

InsertToken stores a new auth token.

func InsertUser

func InsertUser(d *sql.DB, u *User, passwordHash string) error

InsertUser inserts a new user into the database.

func Middleware

func Middleware(d *sql.DB) func(http.Handler) http.Handler

Middleware returns an http.Handler that validates the Authorization header, looks up the token in the database, and injects the User into the request context. Requests without a valid token receive a 401 response.

The login endpoint (POST /users/:login) and the OpenAPI spec endpoint (GET /openapi.json) are exempt from auth.

func RegisterRoutes

func RegisterRoutes(mux *http.ServeMux, d *sql.DB)

RegisterRoutes registers user CRUD and auth endpoints on the mux.

func UpdateUser

func UpdateUser(d *sql.DB, id string, fields map[string]string, updateTime string) error

UpdateUser updates mutable user fields.

func WithContext

func WithContext(ctx context.Context, u *User) context.Context

WithContext returns a new context carrying the authenticated user.

Types

type User

type User struct {
	ID          string `json:"id"`
	Path        string `json:"path"`
	Email       string `json:"email"`
	DisplayName string `json:"display_name,omitempty"`
	Type        string `json:"type"`
	CreateTime  string `json:"create_time"`
	UpdateTime  string `json:"update_time"`
}

User represents an authenticated user of the API.

func FromContext

func FromContext(ctx context.Context) *User

FromContext extracts the authenticated User from the request context. Returns nil if no user is present.

func GetUserByEmail

func GetUserByEmail(d *sql.DB, email string) (*User, string, error)

GetUserByEmail retrieves a user by email.

func GetUserByID

func GetUserByID(d *sql.DB, id string) (*User, string, error)

GetUserByID retrieves a user by ID.

func GetUserByPath

func GetUserByPath(d *sql.DB, path string) (*User, string, error)

GetUserByPath retrieves a user by path.

func GetUserByToken

func GetUserByToken(d *sql.DB, token string) (*User, error)

GetUserByToken looks up the user associated with a token.

func ListUsers

func ListUsers(d *sql.DB, pageSize int, pageToken string) ([]User, string, error)

ListUsers returns a page of users ordered by ID.

Jump to

Keyboard shortcuts

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