user

package
v0.0.0-...-683b713 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2024 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package user provides utilities for the authorization and authentication of users within the application. In addition, it provides a middleware that can be used by HTTP handlers to automatically authorize requests.

Index

Constants

View Source
const AuthCookieName = "x-putering-auth"

AuthCookieName is used to identify the cookie that contains the user's authentication token, stored in their browser.

Variables

View Source
var ErrInvalidLoginCode = errors.New("user: invalid login code")

ErrInvalidLoginCode is returned when the login code provided by the user is invalid or has expired.

View Source
var ErrNotAllowlisted = errors.New("user: email not allowlisted")

ErrNotAllowlisted is returned when a user attempts to login with an email that is not allowlisted. Caller should inspect this error and inform the user accordingly, i.e. redirecting them to a page that explains that they cannot login.

Functions

func AcknowledgeAllNotifications

func AcknowledgeAllNotifications(ctx context.Context, dbc *sql.DB, userID int64) error

AcknowledgeAllNotifications acknowledges all unacknowledged notifications for a user

func AcknowledgeNotification

func AcknowledgeNotification(ctx context.Context, dbc *sql.DB, notificationID int64) error

AcknowledgeNotification acknowledges a notification for a user

func AuthMiddleware

func AuthMiddleware(dbc *sql.DB, next handlerFuncWithError) handlerFuncWithError

AuthMiddleware is a middleware that attaches a user session to the context based on either a cookie or an Authorization header attached to the request.

func CountFollowers

func CountFollowers(ctx context.Context, dbc *sql.DB, userId int64) (int64, error)

CountFollowers returns the number of followers for a given user.

func CountFollowing

func CountFollowing(ctx context.Context, dbc *sql.DB, userId int64) (int64, error)

CountFollowing returns the number of users a given user is following.

func FollowUser

func FollowUser(ctx context.Context, dbc *sql.DB, userId, otherId int64) error

FollowUser follows the user with the given ID. No-op if the user is already following the other user.

func Login

func Login(ctx context.Context, dbc *sql.DB, mailerObj mailer.Mailer, email string, redirectAfter *string, emailTemplateSet *ssr.TemplateSet) error

Login intiates the login process for a user by sending them a login email. The login email contains a link (/ token) that the user can use to login. Once they've clicked the link, they are redirected to the application and `FinalizeLogin` is called to complete the login process, returning a session token that should be stored in the user's browser with a cookie.

func NotifyDidFollow

func NotifyDidFollow(ctx context.Context, dbc *sql.DB, userId, followedId int64) error

NotifyDidFollow notifies a user that another user followed them Note: The caller must have a valid username, as the notification will link to the follower's profile

func UnfollowUser

func UnfollowUser(ctx context.Context, dbc *sql.DB, userId, otherId int64) error

UnfollowUser unfollows the user with the given ID. No-op if the user is not following the other user.

func UpsertProfile

func UpsertProfile(ctx context.Context, dbc *sql.DB, userId int64, profile *Profile) error

UpsertProfile updates or creates the profile for the user with the given ID.

func UserIsFollowing

func UserIsFollowing(ctx context.Context, dbc *sql.DB, userId, otherId int64) (bool, error)

UserIsFollowing returns true if the user with the given ID is following the user with the given ID.

func UsernameExists

func UsernameExists(ctx context.Context, dbc *sql.DB, username string) (*int64, error)

UsernameExists checks if the given username is already in use.

func ValidBio

func ValidBio(bio string) error

ValidBio checks if the given bio is valid. Must be <= 160 characters long.

func ValidDisplayName

func ValidDisplayName(displayName string) error

ValidDisplayName checks if the given display name is valid. Rules: - Up to 50 characters long - Can contain any characters - Not empty

func ValidUsername

func ValidUsername(username string) error

ValidUsername checks if the given username is valid. Rules: - Must be between 5 and 15 characters long. - Must only contain lowercase alphanumeric characters and underscores. - Can't contain any of the reserved terms.

func ValidWebsiteURL

func ValidWebsiteURL(websiteUrl string) error

ValidWebsiteURL checks if the given website URL is valid.

Types

type LoginResult

type LoginResult struct {
	SessionToken  string
	ExpiresAt     time.Time
	User          *db.User
	RedirectAfter *string
	DidCreateUser bool
}

LoginResult is the result of a successful login. Contains the session token that should be stored in the user's browser.

func FinalizeLogin

func FinalizeLogin(ctx context.Context, dbc *sql.DB, code string) (*LoginResult, error)

FinalizeLogin completes the login process for a user by verifying the login code provided by the user and creating a session token that should be stored in the user's browser with a cookie.

type NewFollowerNotifMeta

type NewFollowerNotifMeta struct {
	FollowerUserID int64 `json:"follower_user_id"`
}

Metadata for each notification kind

type Notification

type Notification struct {
	ID             int64
	UserID         int64
	CreatedAt      time.Time
	AcknowledgedAt *time.Time
	Kind           NotificationKind
	Metadata       any // Type dependent on the kind of notification
}

Notification is a higher-level struct that represents a notification that a user can receive. The caller can use `HTMLString()` to get a renderable HTML string for the notification.

func GetUnacknowledgedNotifications

func GetUnacknowledgedNotifications(ctx context.Context, dbc *sql.DB, userID int64) ([]*Notification, error)

GetUnacknowledgedNotifications returns all unacknowledged notifications for a user The notifications are sorted by creation date in descending order

func (*Notification) HTMLString

func (n *Notification) HTMLString(ctx context.Context, dbc *sql.DB) (template.HTML, error)

HTMLString returns a renderable HTML string for the notification

type NotificationKind

type NotificationKind string

NotificationKind is a type for the kind of notification that a user can receive

const (
	// NotificationKindNewFollower is sent when someone follows a user
	NotificationKindNewFollower NotificationKind = "new_follower"
)

Supported notification kinds

type Profile

type Profile struct {
	UserID      int64
	Username    *string
	DisplayName *string
	Bio         *string
	WebsiteURL  *string
}

Profile is an internal version of the user's profile.

func GetProfile

func GetProfile(ctx context.Context, dbc *sql.DB, userId int64) (*Profile, error)

GetProfile retrieves the profile for the user with the given ID.

func GetProfileByUsername

func GetProfileByUsername(ctx context.Context, dbc *sql.DB, username string) (*Profile, error)

GetProfileByUsername retrieves the profile for the user with the given username. Returns nil if the profile doesn't exist.

func ListUserFollowers

func ListUserFollowers(ctx context.Context, dbc *sql.DB, userId int64) ([]*Profile, error)

ListUserFollowers returns a list of users following the user with the given ID.

func ListUserFollowing

func ListUserFollowing(ctx context.Context, dbc *sql.DB, userId int64) ([]*Profile, error)

ListUserFollowing returns a list of users the user with the given ID is following.

func QueryProfiles

func QueryProfiles(ctx context.Context, dbc *sql.DB, query string, topk int) ([]*Profile, error)

QueryProfiles searches for profiles that match the given query.

func (*Profile) String

func (p *Profile) String() string

type RequestAuthorization

type RequestAuthorization struct {
	Session db.Token
	User    db.User
}

RequestAuthorization is a structure that holds the user session and user information for a given request. Attached to the context.

func ExtractAuth

func ExtractAuth(ctx context.Context) *RequestAuthorization

ExtractAuth returns the user session from the context, if it exists.

Jump to

Keyboard shortcuts

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