-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
126 lines (95 loc) · 2.9 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
116
117
118
119
120
121
122
123
124
125
126
package models
import (
"os"
"strings"
u "redcoins/utils"
jwt "github.com/dgrijalva/jwt-go"
"github.com/jinzhu/gorm"
)
type Token struct {
UserId uint
jwt.StandardClaims
}
type User struct {
gorm.Model
Email string `gorm:"unique_index;not null" json:"email"`
Password string `gorm:"not null" json:"password"`
Name string `gorm:"not null" json:"name"`
Birthday string `gorm:"not null" json:"birthday"`
Token string `json:"token"; sql:"-"`
}
func (user *User) Validate() (map[string]interface{}, bool) {
if !strings.Contains(user.Email, "@") {
return u.Message(false, "Email já cadastrado"), false
}
userTemp := &User{}
err := GetDB().Table("users").Where("email = ?", user.Email).First(userTemp).Error
if err != nil && err != gorm.ErrRecordNotFound {
return u.Message(false, "Erro de conexão. Tente Novamente"), false
}
if userTemp.Email != "" {
return u.Message(false, "Email já cadastrado"), false
}
return u.Message(false, "Request Validado!"), true
}
func (user *User) Create() map[string]interface{} {
if resp, ok := user.Validate(); !ok {
return resp
}
GetDB().Create(user)
if user.ID <= 0 {
return u.Message(false, "Falha ao criar usuario, erro de conexao.")
}
tk := &Token{UserId: user.ID}
token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), tk)
tokenString, _ := token.SignedString([]byte(os.Getenv("token_password")))
user.Token = tokenString
response := u.Message(true, "Usuario criado com sucesso")
response["user"] = user
return response
}
func Login(email, password string) map[string]interface{} {
user := &User{}
err := GetDB().Table("users").Where("email = ?", email).First(user).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return u.Message(false, "Email nao encontrado")
}
return u.Message(false, "Erro de conexao. Tente novamente")
}
if password != user.Password {
return u.Message(false, "Credenciais invalidas. Tente novamente")
}
tk := &Token{UserId: user.ID}
token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), tk)
tokenString, _ := token.SignedString([]byte(os.Getenv("TOKEN_PASSWORD")))
user.Token = tokenString
resp := u.Message(true, "Login realizado com sucesso")
resp["user"] = user
return resp
}
func RecoverPassword(email string, password string) map[string]interface{} {
user := &User{}
err := GetDB().Table("users").Where("email = ?", email).First(user).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return u.Message(false, "Usuario nao encontrado")
}
return u.Message(false, "Erro de conexao. Tente novamente")
}
user.Password = password
GetDB().Save(&user)
if err != nil {
return u.Message(false, "Erro de conexao. Tente novamente")
}
resp := u.Message(true, "Senha alterada com sucesso")
return resp
}
func GetUser(email string) *User {
user := &User{}
GetDB().Table("users").Where("email = ?", email).First(user)
if user.Email == "" {
return nil
}
return user
}