Skip to content

Commit

Permalink
chore: tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Aulia Wiguna committed Feb 19, 2023
1 parent bc59722 commit c40ab40
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/repositories/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,11 @@ func LoginUsingGooleOneTap(c *fiber.Ctx) (models.User, string, error) {
db.Preload("RoleUser.Role").Find(&user, "email = ?", email)

if user.ID == 0 {
// Remove all reset password token
db.Unscoped().Where("user_id = ?", user.ID).Where("type = ?", "reset_password").Delete(&models.UserToken{})

return user, "", fmt.Errorf("credential cannot be found")
}

// Remove all reset password token
db.Unscoped().Where("user_id = ?", user.ID).Where("type = ?", "reset_password").Delete(&models.UserToken{})
removeResetPasswordTokens(fmt.Sprint(user.ID))

return GenerateJwt(&user)
}
Expand Down Expand Up @@ -98,7 +95,7 @@ func Login(c *fiber.Ctx) (models.User, string, error) {

// Remove all reset password token
go func() {
db.Unscoped().Where("user_id = ?", user.ID).Where("type = ?", "reset_password").Delete(&models.UserToken{})
removeResetPasswordTokens(fmt.Sprint(user.ID))
}()

return GenerateJwt(&user)
Expand Down Expand Up @@ -206,7 +203,7 @@ func ResetPassword(c *fiber.Ctx) (string, error) {
Password: password,
})
// Remove all reset password token
db.Unscoped().Where("user_id = ?", user.ID).Where("type = ?", "reset_password").Delete(&models.UserToken{})
removeResetPasswordTokens(fmt.Sprint(user.ID))
}

return "success", nil
Expand Down Expand Up @@ -825,3 +822,8 @@ func UpdateEmailAddress(c *fiber.Ctx, id string) (models.User, error) {

return user, nil
}

func removeResetPasswordTokens(id string) {
db := database.DB
db.Unscoped().Where("user_id = ?", id).Where("type = ?", "reset_password").Delete(&models.UserToken{})
}

0 comments on commit c40ab40

Please sign in to comment.