From 869f50db2fbc24826762fb61d35e5f8de79296c1 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Fri, 25 Aug 2023 22:35:47 +0200 Subject: feat: Handle login error --- pkg/service/auth.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'pkg/service') diff --git a/pkg/service/auth.go b/pkg/service/auth.go index 30e574a..2fc06e3 100644 --- a/pkg/service/auth.go +++ b/pkg/service/auth.go @@ -21,6 +21,8 @@ type AuthController struct { key []byte } +var InvalidLogin = errors.New("Invalid login") + func NewAuthController( authRepository repository.AuthRepository, userRepository repository.UserRepository, @@ -35,17 +37,21 @@ func NewAuthController( func (c *AuthController) Login(ctx context.Context, username, password []byte) ([]byte, error) { id, err := c.authRepository.GetIDByUsername(ctx, string(username)) - if err != nil { + if errors.Is(err, repository.ErrRecordNotFound) { + return nil, InvalidLogin + } else if err != nil { return nil, err } hashedPassword, err := c.authRepository.GetPassword(ctx, id) - if err != nil { + if errors.Is(err, repository.ErrRecordNotFound) { + return nil, InvalidLogin + } else if err != nil { return nil, err } if err := bcrypt.CompareHashAndPassword(hashedPassword, password); err != nil { - return nil, err + return nil, InvalidLogin } token := &Token{ -- cgit v1.2.3