Documentation ¶
Index ¶
- Constants
- func AdminAuth(c *fiber.Ctx) error
- func CreateTokenFromUser(user *types.User) string
- func ErrorHandler(c *fiber.Ctx, err error) error
- func JWTAuthentication(userStore db.UserStore) fiber.Handler
- type AuthHandler
- type AuthParams
- type AuthResponse
- type BookRoomParams
- type BookingHandler
- type Error
- type HotelHandler
- type HotelQueryParams
- type Response
- type RoomHandler
- type UserHandler
Constants ¶
const TokenHeader = "X-Api-Token"
Variables ¶
This section is empty.
Functions ¶
func CreateTokenFromUser ¶
CreateTokenFromUser does the following: - Create a token that is valid for 4 hours - Sign it with the environment variable JWT_SECRET
func ErrorHandler ¶
func JWTAuthentication ¶
JWTAuthentication will:
- Expect that the HTTP request has a header of name `TokenHeader`
- Get the user claims (email, expiration date and user id)
- Verify that the token is valid
- Verify that the token hasn't expired
- Pass the user as types.User struct to the fiber context so that it may be used in the room handler.
Types ¶
type AuthHandler ¶
type AuthHandler struct {
// contains filtered or unexported fields
}
func NewAuthHandler ¶
func NewAuthHandler(userStore db.UserStore) *AuthHandler
func (*AuthHandler) HandleAuthenticate ¶
func (h *AuthHandler) HandleAuthenticate(c *fiber.Ctx) error
HandleAuthenticate will do the following: - Parse the body and expect it to be a JSON formated AuthParams - Get the user information based on the passed email - Verify that their password is correct - Return a authentication response with a token
type AuthParams ¶
AuthParams are used when the user is trying to authenticate, these fields are mandatory.
type AuthResponse ¶
type BookRoomParams ¶
type BookRoomParams struct { FromDate time.Time `json:"fromDate"` UntilDate time.Time `json:"untilDate"` NumPersons int `json:"numPersons"` }
The User ID and Room ID isn't specified here because the User ID is already in the Context
type BookingHandler ¶
type BookingHandler struct {
// contains filtered or unexported fields
}
func NewBookingHandler ¶
func NewBookingHandler(store *db.Store) *BookingHandler
func (*BookingHandler) HandleCancelBooking ¶
func (h *BookingHandler) HandleCancelBooking(c *fiber.Ctx) error
HandleCancelBooking will: - get the id from the HTTP URL - get the booking based on the id - get the user from the context (sent via JWT middelware) - check that the user owns it or the user is an admin - update the canceled field
func (*BookingHandler) HandleGetBooking ¶
func (h *BookingHandler) HandleGetBooking(c *fiber.Ctx) error
func (*BookingHandler) HandleGetBookings ¶
func (h *BookingHandler) HandleGetBookings(c *fiber.Ctx) error
HandleGetBookings will return all bookings with no filter
type Error ¶
func ErrorBadRequest ¶
func ErrorBadRequest() Error
func ErrorInvalidID ¶
func ErrorInvalidID() Error
func ErrorNotFound ¶
func ErrorTokenExpired ¶
func ErrorTokenExpired() Error
func ErrorUnauthorized ¶
func ErrorUnauthorized() Error
type HotelHandler ¶
type HotelHandler struct {
// contains filtered or unexported fields
}
func NewHotelHandler ¶
func NewHotelHandler(store *db.Store) *HotelHandler
func (*HotelHandler) HandleGetHotel ¶
func (h *HotelHandler) HandleGetHotel(c *fiber.Ctx) error
func (*HotelHandler) HandleGetHotels ¶
func (h *HotelHandler) HandleGetHotels(c *fiber.Ctx) error
func (*HotelHandler) HandleGetRooms ¶
func (h *HotelHandler) HandleGetRooms(c *fiber.Ctx) error
type HotelQueryParams ¶
type HotelQueryParams struct { db.Pagination Rating int }
type RoomHandler ¶
type RoomHandler struct {
// contains filtered or unexported fields
}
func NewRoomHandler ¶
func NewRoomHandler(store *db.Store) *RoomHandler
func (*RoomHandler) HandleBookRoom ¶
func (h *RoomHandler) HandleBookRoom(c *fiber.Ctx) error
HandleBookRoom will do the following: - Expect that the body is a JSON marshaled `BookRoomParams` - Validate the parameters - Convert the room ID from the HTTP URL to a primitive MongoDB id - Get the user data from the fiber context (this is being passed from the middleware) - Check that the room is available - Generate and insert a booking
func (*RoomHandler) HandleGetRoom ¶
func (h *RoomHandler) HandleGetRoom(c *fiber.Ctx) error
HandleGetRoom will return th rooms that matches id
func (*RoomHandler) HandleGetRooms ¶
func (h *RoomHandler) HandleGetRooms(c *fiber.Ctx) error
HandleGetRooms will return all the rooms with no filter
type UserHandler ¶
type UserHandler struct {
// contains filtered or unexported fields
}
func NewUserHandler ¶
func NewUserHandler(userStore db.UserStore) *UserHandler
func (*UserHandler) HandleDeleteUser ¶
func (h *UserHandler) HandleDeleteUser(c *fiber.Ctx) error
func (*UserHandler) HandleGetUser ¶
func (h *UserHandler) HandleGetUser(c *fiber.Ctx) error
func (*UserHandler) HandleGetUsers ¶
func (h *UserHandler) HandleGetUsers(c *fiber.Ctx) error
func (*UserHandler) HandlePostUser ¶
func (h *UserHandler) HandlePostUser(c *fiber.Ctx) error
func (*UserHandler) HandlePutUser ¶
func (h *UserHandler) HandlePutUser(c *fiber.Ctx) error