users

package
v0.0.0-...-a746138 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const CreateHandler = "users.HandleCreate"
View Source
const DeleteHandler = "users.HandleDelete"
View Source
const LoginHandler = "users.HandleLogin"
View Source
const SuccessfulCreateUserMessage = "User created successfully"
View Source
const SuccessfulDeleteUserMessage = "Successfully deleted user"
View Source
const SuccessfulUpdateUserMessage = "Successfully updated user"
View Source
const SuccessfulUserLoginMessage = "User logged in successfully"
View Source
const UpdateHandler = "users.HandleUpdate"

Variables

View Source
var (
	ErrUsernameAlreadyRegistered = errors.New("username has already been taken up by someone else")
	ErrEmailAlreadyRegistered    = errors.New("email has already been used to register for another account")
)

Custom errors

View Source
var (
	WrapErrParseUserID       = api.ErrorMessage{Message: "Failed to parse user ID", Code: 500}
	WrapErrCreateUser        = api.ErrorMessage{Message: "Failed to create user", Code: 500}
	WrapErrDeleteUser        = api.ErrorMessage{Message: "Failed to delete user", Code: 500}
	WrapErrUpdateUser        = api.ErrorMessage{Message: "Failed to update user", Code: 500}
	WrapErrCheckEmail        = api.ErrorMessage{Message: "Something went wrong while checking if email has been used to register for another account", Code: 500}
	WrapErrCheckUsername     = api.ErrorMessage{Message: "Something went wrong while checking if username has already been taken up by someone else", Code: 500}
	WrapErrHashPassword      = api.ErrorMessage{Message: "Failed to hash given password", Code: 500}
	WrapErrGetUser           = api.ErrorMessage{Message: "Failed to retrieve user", Code: 500}
	WrapErrInvalidEmail      = api.ErrorMessage{Message: "Invalid email", Code: 401}
	WrapErrIncorrectPassword = api.ErrorMessage{Message: "Invalid password", Code: 401}
	WrapErrSetExpiry         = api.ErrorMessage{Message: "Failed to set JWT expiration", Code: 500}
	WrapErrEncodeJWT         = api.ErrorMessage{Message: "Failed to encode JWT", Code: 500}
	WrapErrRetrieveIDFromJWT = api.ErrorMessage{Message: "Failed to retrieve user ID from JWT", Code: 500}
	WrapErrGetUserFromID     = api.ErrorMessage{Message: "Failed to get username from ID", Code: 500}
)

Custom error message wrappers

Functions

func CreateUser

func CreateUser(ctx context.Context, client *ent.Client, user ent.User) error

func DeleteUser

func DeleteUser(ctx context.Context, client *ent.Client, user *ent.User) error

DeleteUser deletes an existing user entry in the database based on ent.User object provided

func GetUserFromEmail

func GetUserFromEmail(ctx context.Context, client *ent.Client, email string) (*ent.User, error)

GetUserFromEmail queries the database for a user with the provided email address and returns a user object

func GetUserFromID

func GetUserFromID(ctx context.Context, client *ent.Client, id uuid.UUID) (*ent.User, error)

GetUserFromID queries the database for a user with the given ID

func GetUserIDFromToken

func GetUserIDFromToken(r *http.Request) (uuid.UUID, error)

GetUserIDFromToken checks request context for a valid JWT and returns the parsed user id or a nil UUID if an error occurs.

func HandleCreate

func HandleCreate(w http.ResponseWriter, r *http.Request) (*api.Response, error)

func HandleDelete

func HandleDelete(w http.ResponseWriter, r *http.Request) (*api.Response, error)

HandleDelete handles the DELETE request, calls DeleteUser if needed and returns a JSON encoded API response

func HandleLogin

func HandleLogin(w http.ResponseWriter, r *http.Request) (*api.Response, error)

func HandleUpdate

func HandleUpdate(w http.ResponseWriter, r *http.Request) (*api.Response, error)

HandleCreate parses the PUT request form data, calls UpdateUser if needed and returns a JSON encoded API response

func UpdateUser

func UpdateUser(ctx context.Context, client *ent.Client, user *ent.User) error

UpdateUser updates an existing user entry in the database based on user ID provided via the ent.User object

Types

type CreateUserResponse

type CreateUserResponse struct {
	ID               int       `json:"id"`
	Title            string    `json:"title"`
	Slug             string    `json:"slug"`
	Description      string    `json:"description"`
	ShortDescription string    `json:"shortDescription"`
	ProfilePicURL    string    `json:"profilePicURL"`
	CreatedAt        time.Time `json:"createdAt"`
}

CreateUserResponse is derived from ent.User with modified json keys in camelCase.

type JWTResponse

type JWTResponse struct {
	Token  string `json:"token"`
	UserID string `json:"userId"`
}

type LoginRequest

type LoginRequest struct {
	Email    string `json:"email"`
	Password string `json:"password"`
}

LoginRequest models the JSON request body for logging in a user.

type UserRequest

type UserRequest struct {
	Email    string `json:"email"`
	Password string `json:"password"`
	Username string `json:"username"`
}

UserRequest models the JSON request body for creating or updating a new user.

Jump to

Keyboard shortcuts

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