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
- Variables
- func AcknowledgeAllNotifications(ctx context.Context, dbc *sql.DB, userID int64) error
- func AcknowledgeNotification(ctx context.Context, dbc *sql.DB, notificationID int64) error
- func AuthMiddleware(dbc *sql.DB, next handlerFuncWithError) handlerFuncWithError
- func CountFollowers(ctx context.Context, dbc *sql.DB, userId int64) (int64, error)
- func CountFollowing(ctx context.Context, dbc *sql.DB, userId int64) (int64, error)
- func FollowUser(ctx context.Context, dbc *sql.DB, userId, otherId int64) error
- func Login(ctx context.Context, dbc *sql.DB, mailerObj mailer.Mailer, email string, ...) error
- func NotifyDidFollow(ctx context.Context, dbc *sql.DB, userId, followedId int64) error
- func UnfollowUser(ctx context.Context, dbc *sql.DB, userId, otherId int64) error
- func UpsertProfile(ctx context.Context, dbc *sql.DB, userId int64, profile *Profile) error
- func UserIsFollowing(ctx context.Context, dbc *sql.DB, userId, otherId int64) (bool, error)
- func UsernameExists(ctx context.Context, dbc *sql.DB, username string) (*int64, error)
- func ValidBio(bio string) error
- func ValidDisplayName(displayName string) error
- func ValidUsername(username string) error
- func ValidWebsiteURL(websiteUrl string) error
- type LoginResult
- type NewFollowerNotifMeta
- type Notification
- type NotificationKind
- type Profile
- func GetProfile(ctx context.Context, dbc *sql.DB, userId int64) (*Profile, error)
- func GetProfileByUsername(ctx context.Context, dbc *sql.DB, username string) (*Profile, error)
- func ListUserFollowers(ctx context.Context, dbc *sql.DB, userId int64) ([]*Profile, error)
- func ListUserFollowing(ctx context.Context, dbc *sql.DB, userId int64) ([]*Profile, error)
- func QueryProfiles(ctx context.Context, dbc *sql.DB, query string, topk int) ([]*Profile, error)
- type RequestAuthorization
Constants ¶
const AuthCookieName = "x-putering-auth"
AuthCookieName is used to identify the cookie that contains the user's authentication token, stored in their browser.
Variables ¶
var ErrInvalidLoginCode = errors.New("user: invalid login code")
ErrInvalidLoginCode is returned when the login code provided by the user is invalid or has expired.
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 ¶
AcknowledgeAllNotifications acknowledges all unacknowledged notifications for a user
func AcknowledgeNotification ¶
AcknowledgeNotification acknowledges a notification for a user
func AuthMiddleware ¶
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 ¶
CountFollowers returns the number of followers for a given user.
func CountFollowing ¶
CountFollowing returns the number of users a given user is following.
func FollowUser ¶
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 ¶
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 ¶
UnfollowUser unfollows the user with the given ID. No-op if the user is not following the other user.
func UpsertProfile ¶
UpsertProfile updates or creates the profile for the user with the given ID.
func UserIsFollowing ¶
UserIsFollowing returns true if the user with the given ID is following the user with the given ID.
func UsernameExists ¶
UsernameExists checks if the given username is already in use.
func ValidDisplayName ¶
ValidDisplayName checks if the given display name is valid. Rules: - Up to 50 characters long - Can contain any characters - Not empty
func ValidUsername ¶
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 ¶
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 ¶
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 ¶
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 ¶
GetProfile retrieves the profile for the user with the given ID.
func GetProfileByUsername ¶
GetProfileByUsername retrieves the profile for the user with the given username. Returns nil if the profile doesn't exist.
func ListUserFollowers ¶
ListUserFollowers returns a list of users following the user with the given ID.
func ListUserFollowing ¶
ListUserFollowing returns a list of users the user with the given ID is following.
func QueryProfiles ¶
QueryProfiles searches for profiles that match the given query.
type RequestAuthorization ¶
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.