-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.go
110 lines (87 loc) · 2.77 KB
/
accounts.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
package models
import (
u "../utils"
"github.com/dgrijalva/jwt-go"
"github.com/jinzhu/gorm"
"golang.org/x/crypto/bcrypt"
"os"
"strings"
)
type Token struct {
UserId uint
jwt.StandardClaims
}
type Account struct {
gorm.Model
Email string `json:"email"`
Password string `json:"password"`
Token string `json:"token";sql:"-"`
}
func (account *Account) ValidateAccount() (map[string]interface{}, bool){
if !strings.Contains(account .Email,"@"){
return u.Message(false, "Email address is required"), false
}
if len(account.Password) < 6 {
return u.Message(false, "Password is required"), false
}
temp := &Account{}
err := GetDB().Table("accounts").Where("email = ?", account.Email).First(temp).Error
if err != nil && err != gorm.ErrRecordNotFound{
return u.Message(false, "Connection error!, Please retry"), false
}
if temp.Email != ""{
return u.Message(false, "Email address already in use by another user."), false
}
return u.Message(false, "Requirement passed"), true
}
func (account *Account) CreateAccount()(map[string]interface{}){
if resp, ok := account.ValidateAccount(); !ok{
return resp
}
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(account.Password), bcrypt.DefaultCost)
account.Password = string(hashedPassword)
GetDB().Create(account)
if account.ID <= 0{
return u.Message(false, "Failed to create account, connection error.")
}
tk:=&Token{UserId: account.ID}
token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"),tk)
tokenString, _ := token.SignedString([]byte (os.Getenv("token_password")))
account.Token = tokenString
resp := u.Message(true,"Logged In")
resp["account"] = account
return resp
}
func Login(email, password string) (map[string]interface{}) {
account := &Account{}
err := GetDB().Table("accounts").Where("email = ?", email).First(account).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return u.Message(false, "Email address not found")
}
return u.Message(false, "Connection error. Please retry")
}
err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(password))
if err != nil && err == bcrypt.ErrMismatchedHashAndPassword { //Password does not match!
return u.Message(false, "Invalid login credentials. Please try again")
}
//Worked! Logged In
account.Password = ""
//Create JWT token
tk := &Token{UserId: account.ID}
token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), tk)
tokenString, _ := token.SignedString([]byte(os.Getenv("token_password")))
account.Token = tokenString //Store the token in the response
resp := u.Message(true, "Logged In")
resp["account"] = account
return resp
}
func GetUser(u uint) * Account {
acc := &Account{}
GetDB().Table("accounts").Where("id=?",u).First(acc)
if acc.Email == "" {
return nil
}
acc.Password = ""
return acc
}