From 447715e1af58fe02a5b743cd43e2978aeb3ebe3f Mon Sep 17 00:00:00 2001 From: fbonhomm Date: Sat, 24 Aug 2019 20:51:11 +0200 Subject: [PATCH] feat: add models folder with user model --- source/models/user.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 source/models/user.go diff --git a/source/models/user.go b/source/models/user.go new file mode 100644 index 0000000..645adf6 --- /dev/null +++ b/source/models/user.go @@ -0,0 +1,32 @@ +/** + * Created by fbonhomm + * Email: flo-github@outlook.fr + * Licence: MIT + */ + +package models + +import ( + "github.com/jinzhu/gorm" + "golang.org/x/crypto/bcrypt" +) + +type User struct { + gorm.Model + Name string `gorm:"type:varchar(50);not null" json:"name"` + Email string `gorm:"type:varchar(100);unique_index;not null" json:"email"` + Password string `gorm:"type:text;not null"` +} + +func (u *User) BeforeSave() (err error) { + var hash []byte + + if u.Password != "" { + hash, err = bcrypt.GenerateFromPassword([]byte(u.Password), 12) + + if err == nil { + u.Password = string(hash) + } + } + return err +}