aboutsummaryrefslogtreecommitdiff
path: root/user/user.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2023-09-03 00:19:03 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2023-09-03 00:19:03 +0530
commit616a151764fd1780eb0b6dc039d5a21539a8a01a (patch)
tree454f73e06880c3635c3f88a5787fd8fbc1c2b363 /user/user.go
parent65cb3603ad2682deacff47a72d9050e584a00488 (diff)
added login with id
Diffstat (limited to 'user/user.go')
-rw-r--r--user/user.go28
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
+}