From f814888f1ebaab23de8a3688be5f801d78244b5d Mon Sep 17 00:00:00 2001 From: nikurasu Date: Sun, 19 Nov 2023 16:37:52 +0100 Subject: [PATCH] Add login route --- src/app/controller/user_controller.go | 35 +++++++++++++++++++++++++++ src/app/routes/private_routes.go | 2 +- src/app/routes/public_routes.go | 1 + src/app/service/user.go | 11 +++++++++ src/entities/user.go | 2 +- 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/app/controller/user_controller.go b/src/app/controller/user_controller.go index 13a2c26..00651f0 100644 --- a/src/app/controller/user_controller.go +++ b/src/app/controller/user_controller.go @@ -46,3 +46,38 @@ func CreateUser(c *fiber.Ctx) error { }) return c.SendStatus(fiber.StatusOK) } + +func LoginUser(c *fiber.Ctx) error { + credentials := new(entities.User) + if err := c.BodyParser(credentials); err != nil { + return &fiber.Error{ + Code: fiber.ErrUnauthorized.Code, + Message: "Unauthorized", + } + } + user, err := service.GetUserByEmail(credentials.Email) + if err != nil { + return &fiber.Error{ + Code: fiber.ErrUnauthorized.Code, + Message: "Unauthorized", + } + } + err = bcrypt.CompareHashAndPassword(user.HashedPassword, []byte(credentials.Password)) + if err != nil { + return &fiber.Error{ + Code: fiber.ErrUnauthorized.Code, + Message: "Unauthorized", + } + } + token, err := util.CreateToken(&user) + if err != nil { + return &fiber.Error{ + Code: fiber.ErrInternalServerError.Code, + Message: "Internal Server Error", + } + } + c.JSON(responses.UserResponse{ + Token: token, + }) + return c.SendStatus(fiber.StatusOK) +} diff --git a/src/app/routes/private_routes.go b/src/app/routes/private_routes.go index 032cdab..0bc038f 100644 --- a/src/app/routes/private_routes.go +++ b/src/app/routes/private_routes.go @@ -9,7 +9,7 @@ import ( func PrivateRoutes(app *fiber.App) { apiv1 := app.Group("/api/v1") event := apiv1.Group("/event") - event.Use(config.AuthMiddleware) + event.Use(config.JwtMiddleware) //app.Get("/events", controller.ReturnEventsTableView) event.Get("", controller.ReturnAllEvents) event.Get("/:id", controller.ReturnEventById) diff --git a/src/app/routes/public_routes.go b/src/app/routes/public_routes.go index 256e8ad..503c485 100644 --- a/src/app/routes/public_routes.go +++ b/src/app/routes/public_routes.go @@ -8,6 +8,7 @@ import ( func PublicRoutes(app *fiber.App) { apiv1 := app.Group("/api/v1") attendies := apiv1.Group("/attendies") + apiv1.Post("/login", controller.LoginUser) apiv1.Get("/ping", controller.Ping) attendies.Get("/:name", controller.GetAttendiesByEvent) } diff --git a/src/app/service/user.go b/src/app/service/user.go index d33631f..599ec2d 100644 --- a/src/app/service/user.go +++ b/src/app/service/user.go @@ -1,6 +1,8 @@ package service import ( + "errors" + "ulmer-furs.de/pretix-proxy/v2/config" "ulmer-furs.de/pretix-proxy/v2/entities" ) @@ -12,3 +14,12 @@ func Create_User(user *entities.User) error { } return nil } + +func GetUserByEmail(email string) (entities.User, error) { + var user entities.User + result := config.Database.Where("email = ?", email).First(&user) + if result.RowsAffected == 0 { + return user, errors.New("user not found") + } + return user, nil +} diff --git a/src/entities/user.go b/src/entities/user.go index 47a0f5e..cdfe7f4 100644 --- a/src/entities/user.go +++ b/src/entities/user.go @@ -4,7 +4,7 @@ import "gorm.io/gorm" type User struct { gorm.Model - Email string `gorm:"column:email;not null" validate:"required"` + Email string `gorm:"column:email;not null;unique" validate:"required"` Password string `gorm:"-" validate:"required"` HashedPassword []byte `gorm:"column:hashed_password;not null"` }