Skip to content

Commit

Permalink
feat: user sign in route
Browse files Browse the repository at this point in the history
  • Loading branch information
moonpatel committed Nov 12, 2024
1 parent 0fa1e29 commit 80a8e9d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
22 changes: 21 additions & 1 deletion internal/controllers/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,27 @@ func NewAuthController(as *services.AuthService, ss *services.SessionService) *A
}

func (ac *AuthController) SignIn(c *fiber.Ctx) error {
return c.SendString("Login successful!")
body := new(validators.SignInUser)

if err := c.BodyParser(body); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request body"})
}

isValid, userDetails, err := ac.authService.VerifyPassword(body.Email, body.Password)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error"})
}
if !isValid {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid email or password"})
}

sessionId, err := ac.sessionService.CreateSession(userDetails.ID.String())
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to create session"})
}
utils.SetSessionCookie(c, sessionId)

return c.JSON(fiber.Map{"message": "signed in successfully"})
}

func (ac *AuthController) SignUp(c *fiber.Ctx) error {
Expand Down
13 changes: 13 additions & 0 deletions internal/repositories/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ func (r *UserRepository) GetUser(uuid string) (*models.User, error) {
return user, nil
}

func (r *UserRepository) GetUserByStruct(query *models.User) (*models.User, error) {
user := new(models.User)
result := r.db.Where(query).First(user)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return nil, nil
}
return nil, fmt.Errorf("error in getting user: %w", result.Error)
}

return user, nil
}

func (r *UserRepository) UpdateUser(id string, updates *models.User) error {
return r.db.Model(&models.User{}).Where("id = ?", id).Updates(updates).Error
}
20 changes: 20 additions & 0 deletions internal/services/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ func (as *AuthService) RegisterUser(userRegister *validators.SignUpUser) error {
return nil
}

func (as *AuthService) VerifyPassword(email string, password string) (bool, *models.User, error) {
user, err := as.userRepo.GetUserByStruct(&models.User{Email: email})
if err != nil {
return false, nil, err
}
if user == nil {
return false, nil, err
}

isValid, err := utils.VerifyPassword(password, user.PasswordHash)
if err != nil {
return false, nil, err
}
if !isValid {
return false, nil, nil
}

return true, user, nil
}

func (as *AuthService) VerifyOTP(verifyOtpBody *validators.VerifyOTP) (bool, error) {
val, err := as.redisRepo.Get("registration-verification-otp-" + verifyOtpBody.Email)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/validators/sign_in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package validators

type SignInUser struct {
Email string `validate:"required|email" label:"Email"`
Password string `validate:"required|minLen:8|password" label:"Password"`
}

0 comments on commit 80a8e9d

Please sign in to comment.