diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-25 22:35:47 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-25 22:35:47 +0200 |
commit | 869f50db2fbc24826762fb61d35e5f8de79296c1 (patch) | |
tree | 10bd08d7d4b34b61f74e5758e9157c2e10e069fc /pkg | |
parent | c9e5d242cbba5e3b9d3ddc4465a9a1367e44cd16 (diff) | |
download | lens-869f50db2fbc24826762fb61d35e5f8de79296c1.tar.gz lens-869f50db2fbc24826762fb61d35e5f8de79296c1.tar.bz2 lens-869f50db2fbc24826762fb61d35e5f8de79296c1.zip |
feat: Handle login error
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/database/repository/base.go | 5 | ||||
-rw-r--r-- | pkg/database/sql/user.go | 28 | ||||
-rw-r--r-- | pkg/service/auth.go | 12 | ||||
-rw-r--r-- | pkg/view/auth.go | 10 |
4 files changed, 42 insertions, 13 deletions
diff --git a/pkg/database/repository/base.go b/pkg/database/repository/base.go new file mode 100644 index 0000000..a9d69c9 --- /dev/null +++ b/pkg/database/repository/base.go @@ -0,0 +1,5 @@ +package repository + +import "errors" + +var ErrRecordNotFound = errors.New("record not found") diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go index 6b1cf0f..2ec8622 100644 --- a/pkg/database/sql/user.go +++ b/pkg/database/sql/user.go @@ -2,6 +2,7 @@ package sql import ( "context" + "errors" "golang.org/x/crypto/bcrypt" "gorm.io/gorm" @@ -82,7 +83,7 @@ func (self *UserRepository) List(ctx context.Context) ([]*repository.User, error Find(&users) if result.Error != nil { - return nil, result.Error + return nil, wrapError(result.Error) } return users.ToModel(), nil @@ -95,7 +96,7 @@ func (self *UserRepository) Get(ctx context.Context, id uint) (*repository.User, First(user) if result.Error != nil { - return nil, result.Error + return nil, wrapError(result.Error) } return user, nil @@ -113,7 +114,7 @@ func (self *UserRepository) GetIDByUsername(ctx context.Context, username string First(&userID) if result.Error != nil { - return 0, result.Error + return 0, wrapError(result.Error) } return userID.ID, nil @@ -131,7 +132,7 @@ func (self *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, e First(&userPassword) if result.Error != nil { - return nil, result.Error + return nil, wrapError(result.Error) } return userPassword.Password, nil @@ -150,7 +151,7 @@ func (self *UserRepository) Create(ctx context.Context, createUser *repository.C WithContext(ctx). Create(user) if result.Error != nil { - return 0, result.Error + return 0, wrapError(result.Error) } return user.Model.ID, nil @@ -172,7 +173,7 @@ func (self *UserRepository) Update(ctx context.Context, id uint, update *reposit Omit("password"). Updates(user) if result.Error != nil { - return result.Error + return wrapError(result.Error) } return nil @@ -189,7 +190,7 @@ func (self *UserRepository) Delete(ctx context.Context, id uint) error { WithContext(ctx). Delete(user) if result.Error != nil { - return result.Error + return wrapError(result.Error) } return nil } @@ -203,7 +204,7 @@ func (u *UserRepository) Any(ctx context.Context) (bool, error) { Find(&exists) if result.Error != nil { - return false, result.Error + return false, wrapError(result.Error) } return exists, nil @@ -220,7 +221,7 @@ func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string First(&userPath) if result.Error != nil { - return "", result.Error + return "", wrapError(result.Error) } return userPath, nil @@ -233,5 +234,12 @@ func (u *UserRepository) UpdatePassword(ctx context.Context, id uint, password [ Where("id = ?", id). Update("password", password) - return result.Error + return wrapError(result.Error) +} + +func wrapError(err error) error { + if errors.Is(err, gorm.ErrRecordNotFound) { + return repository.ErrRecordNotFound + } + return err } 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{ diff --git a/pkg/view/auth.go b/pkg/view/auth.go index 1b87235..8d87035 100644 --- a/pkg/view/auth.go +++ b/pkg/view/auth.go @@ -2,6 +2,7 @@ package view import ( "encoding/base64" + "errors" "net/http" "git.sr.ht/~gabrielgio/img/pkg/ext" @@ -45,6 +46,15 @@ func (v *AuthView) Login(w http.ResponseWriter, r *http.Request) error { ) auth, err := v.userController.Login(r.Context(), username, password) + + if errors.Is(err, service.InvalidLogin) { + templates.WritePageTemplate(w, &templates.LoginPage{ + Username: r.FormValue("username"), + Err: err.Error(), + }) + return nil + } + if err != nil { return err } |