Documentation ¶
Index ¶
- Constants
- Variables
- func CreateUser(ctx context.Context, client *ent.Client, user ent.User) error
- func DeleteUser(ctx context.Context, client *ent.Client, user *ent.User) error
- func GetUserFromEmail(ctx context.Context, client *ent.Client, email string) (*ent.User, error)
- func GetUserFromID(ctx context.Context, client *ent.Client, id uuid.UUID) (*ent.User, error)
- func GetUserIDFromToken(r *http.Request) (uuid.UUID, error)
- func HandleCreate(w http.ResponseWriter, r *http.Request) (*api.Response, error)
- func HandleDelete(w http.ResponseWriter, r *http.Request) (*api.Response, error)
- func HandleLogin(w http.ResponseWriter, r *http.Request) (*api.Response, error)
- func HandleUpdate(w http.ResponseWriter, r *http.Request) (*api.Response, error)
- func UpdateUser(ctx context.Context, client *ent.Client, user *ent.User) error
- type CreateUserResponse
- type JWTResponse
- type LoginRequest
- type UserRequest
Constants ¶
const CreateHandler = "users.HandleCreate"
const DeleteHandler = "users.HandleDelete"
const LoginHandler = "users.HandleLogin"
const SuccessfulCreateUserMessage = "User created successfully"
const SuccessfulDeleteUserMessage = "Successfully deleted user"
const SuccessfulUpdateUserMessage = "Successfully updated user"
const SuccessfulUserLoginMessage = "User logged in successfully"
const UpdateHandler = "users.HandleUpdate"
Variables ¶
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
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 DeleteUser ¶
DeleteUser deletes an existing user entry in the database based on ent.User object provided
func GetUserFromEmail ¶
GetUserFromEmail queries the database for a user with the provided email address and returns a user object
func GetUserFromID ¶
GetUserFromID queries the database for a user with the given ID
func GetUserIDFromToken ¶
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 HandleDelete ¶
HandleDelete handles the DELETE request, calls DeleteUser if needed and returns a JSON encoded API response
func HandleLogin ¶
func HandleUpdate ¶
HandleCreate parses the PUT request form data, calls UpdateUser if needed and returns a JSON encoded API response
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 LoginRequest ¶
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.