/
auth-repo.go
40 lines (33 loc) · 941 Bytes
/
auth-repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package repository
import (
"github.com/abdurraufraihan/golang-blog-api/internal/model"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
type AuthRepo interface {
Register(user model.User) (*gorm.DB, model.User)
FindByEmail(email string) (*gorm.DB, model.User)
}
type authRepo struct {
db *gorm.DB
}
func NewAuthRepo(db *gorm.DB) *authRepo {
return &authRepo{db: db}
}
func (repo *authRepo) Register(user model.User) (*gorm.DB, model.User) {
user.Password = hashAndSalt([]byte(user.Password))
userResult := repo.db.Create(&user)
return userResult, user
}
func (repo *authRepo) FindByEmail(email string) (*gorm.DB, model.User) {
user := model.User{}
userResult := repo.db.Where("email = ?", email).Take(&user)
return userResult, user
}
func hashAndSalt(password []byte) string {
hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost)
if err != nil {
panic("Failed to hash password")
}
return string(hash)
}