fix: login service should test IsEnabled

This commit is contained in:
Paul Pan 2022-09-20 15:15:28 +08:00
parent 92dc04041e
commit 5b5bf0a070
2 changed files with 9 additions and 3 deletions

View File

@ -22,6 +22,7 @@ const (
UserDuplicated Status = 302
UserUnauthenticated Status = 303
UserUnauthorized Status = 304
UserDisabled Status = 305
RedisError Status = 400
)
@ -48,6 +49,7 @@ var msgText = map[Status]string{
UserDuplicated: "User Duplicated",
UserUnauthenticated: "User Unauthenticated",
UserUnauthorized: "User Unauthorized",
UserDisabled: "User Disabled",
RedisError: "Redis Error",
}

View File

@ -13,15 +13,19 @@ func (s *service) Login(data *model.User) (*model.User, e.Status) {
err := s.db.Where(user).First(&user).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return user, e.UserNotFound
return nil, e.UserNotFound
}
if err != nil {
return user, e.DatabaseError
return nil, e.DatabaseError
}
if !user.IsEnabled {
return nil, e.UserDisabled
}
err = bcrypt.CompareHashAndPassword(user.Password, data.Password)
if err != nil {
return user, e.UserWrongPassword
return nil, e.UserWrongPassword
}
return user, e.Success