diff options
Diffstat (limited to 'user/user.go')
-rw-r--r-- | user/user.go | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/user/user.go b/user/user.go index 1324c0e..68ceb47 100644 --- a/user/user.go +++ b/user/user.go @@ -18,15 +18,22 @@ package user import ( - "gorm.io/gorm" d "vidhukant.com/openbills/db" + e "vidhukant.com/openbills/errors" + "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" + "github.com/spf13/viper" + "errors" ) +var COST int var db *gorm.DB func init() { db = d.DB db.AutoMigrate(&User{}) + + COST = viper.GetInt("cryptography.password_hashing_cost") } type User struct { @@ -36,3 +43,22 @@ type User struct { Password string IsVerified bool } + +func CheckPassword(id uint, pass string) error { + var user User + err := GetUser(&user, id) + if err != nil { + return err + } + + err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pass)) + if err != nil { + if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) { + return e.ErrWrongPassword + } + + return err + } + + return nil +} |