diff options
| author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-09-03 00:19:03 +0530 | 
|---|---|---|
| committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-09-03 00:19:03 +0530 | 
| commit | 616a151764fd1780eb0b6dc039d5a21539a8a01a (patch) | |
| tree | 454f73e06880c3635c3f88a5787fd8fbc1c2b363 /user | |
| parent | 65cb3603ad2682deacff47a72d9050e584a00488 (diff) | |
added login with id
Diffstat (limited to 'user')
| -rw-r--r-- | user/controller.go | 19 | ||||
| -rw-r--r-- | user/service.go | 47 | ||||
| -rw-r--r-- | user/user.go | 28 | 
3 files changed, 79 insertions, 15 deletions
diff --git a/user/controller.go b/user/controller.go index abdcc5b..4933ea3 100644 --- a/user/controller.go +++ b/user/controller.go @@ -37,3 +37,22 @@ func handleGetUser (ctx *gin.Context) {  		"data": user,  	})  } + +func handleDelUser (ctx *gin.Context) { +	id := uint(1) // get from JWT + +	var user User +	user.ID = id + +	// TODO: add a verification mechanism +	err := user.del() +	if err != nil { +		ctx.Error(err) +		ctx.Abort() +		return +	} + +	ctx.JSON(http.StatusOK, gin.H{ +		"message": "success", +	}) +} diff --git a/user/service.go b/user/service.go index 8b3b712..5e0632b 100644 --- a/user/service.go +++ b/user/service.go @@ -17,23 +17,42 @@  package user +import ( +	e "vidhukant.com/openbills/errors" +) +  func (u *User) Create() error {  	res := db.Create(u)  	// TODO: handle potential errors  	return res.Error  } -//func (c *Customer) del() error { -//	res := db.Delete(c) -// -//	// TODO: handle potential errors -//	if res.Error != nil { -//		return res.Error -//	} -// -//	if res.RowsAffected == 0 { -//		return e.ErrNotFound -//	} -// -//	return nil -//} +func GetUser(user *User, id uint) error { +	res := db.Find(&user, id) + +	// TODO: handle potential errors +	if res.Error != nil { +		return res.Error +	} + +	if res.RowsAffected == 0 { +		return e.ErrNotFound +	} + +	return nil +} + +func (u *User) del() error { +	res := db.Delete(u) + +	// TODO: handle potential errors +	if res.Error != nil { +		return res.Error +	} + +	if res.RowsAffected == 0 { +		return e.ErrNotFound +	} + +	return nil +} 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 +}  |