-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
115 lines (96 loc) · 2.89 KB
/
user.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package entity
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"golang.org/x/crypto/bcrypt"
"regexp"
"strings"
"time"
)
type User struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Name string `json:"name" bson:"name"`
Email string `json:"email" bson:"email"`
IsConfirmed bool `json:"isConfirmed" bson:"isConfirmed"`
Password string `json:"password" bson:"password"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
}
type UserDTO struct {
ID string `json:"id"`
Name string `json:"name"`
IsConfirmed bool `json:"isConfirmed"`
Email string `json:"email"`
Password string `json:"password"`
}
type UserResponse struct {
Name string `json:"name" bson:"name"`
Email string `json:"email" bson:"email"`
}
func ToUser(userDTO *UserDTO) *User {
id, _ := primitive.ObjectIDFromHex(userDTO.ID)
return &User{
ID: id,
Name: userDTO.Name,
Email: userDTO.Email,
IsConfirmed: userDTO.IsConfirmed,
Password: userDTO.Password,
}
}
func ToUserDTO(user *User) *UserDTO {
return &UserDTO{
ID: user.ID.Hex(),
Name: user.Name,
Email: user.Email,
IsConfirmed: user.IsConfirmed,
Password: user.Password,
}
}
func ToUserResponse(user *UserDTO) *UserResponse {
return &UserResponse{
Name: user.Name,
Email: user.Email,
}
}
func HashPassword(password string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
}
func VerifyPassword(hashedPassword string, userPassword string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(userPassword))
}
// Validates user's email
func ValidateEmail(email string) string {
email = strings.TrimSpace(email)
if email == "" {
return "Email is required"
}
emailRegex := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
if !emailRegex.MatchString(email) {
return "Email is not valid"
}
return ""
}
// Validates user's [name, password, email]
func ValidateUser(user *UserDTO) string {
user.Name = strings.TrimSpace(user.Name)
if user.Name == "" {
return "Name is required"
}
if len(user.Name) < 3 {
return "Name must be minimum 3 character long"
}
user.Password = strings.TrimSpace(user.Password)
if user.Password == "" {
return "Password is required"
}
if len(user.Password) < 8 {
return "Password must be minimum 8 character long"
}
user.Email = strings.TrimSpace(user.Email)
emailRegex := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
if user.Email == "" {
return "Email is required"
}
if !emailRegex.MatchString(user.Email) {
return "Email is not valid"
}
return ""
}