-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
62 lines (49 loc) · 1.55 KB
/
model.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package usersvc
import (
"context"
"github.com/sisukasco/commons/utils"
"github.com/sisukasco/henki/pkg/db"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
)
func NewUser(ctx context.Context, Q *db.Queries,
email string, password string, userData utils.JSONMap) (*db.User, error) {
email = utils.CleanupString(email)
pw, err := hashPassword(password)
if err != nil {
return nil, errors.Wrap(err, "Couldn't create password hash")
}
user, err := Q.NewUser(ctx, db.NewUserParams{Email: email, EncryptedPassword: pw})
if err != nil {
return nil, errors.Wrap(err, "NewUser Error creating DB Entry")
}
return &user, nil
}
func (usvc *UserService) GetUser(ctx context.Context, strID string) (*db.User, error) {
ID := strID
user, err := usvc.svc.DB.Q.GetUser(ctx, ID)
if err != nil {
return nil, errors.Wrap(err, "Error getting User Record")
}
return &user, nil
}
func DoesUserExist(ctx context.Context, Q *db.Queries, email string) (bool, error) {
email = utils.CleanupString(email)
return Q.DoesUserExist(ctx, email)
}
func GetUserByEmail(ctx context.Context, Q *db.Queries, email string) (*db.User, error) {
email = utils.CleanupString(email)
user, err := Q.GetUserByEmail(ctx, email)
if err != nil {
return nil, errors.Wrapf(err, "GetUserByEmail %v", email)
}
return &user, nil
}
// hashPassword generates a hashed password from a plaintext string
func hashPassword(password string) (string, error) {
pw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(pw), nil
}