-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
67 lines (54 loc) · 1.3 KB
/
utils.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
package main
import (
"golang.org/x/crypto/bcrypt"
"net/http"
"strings"
)
func isUserExists(u User) User {
var dbUser User
db.Debug().Select("email,password").Where("email = ?", u.Email).First(&User{}).Scan(&dbUser)
return dbUser
}
func validatePayload(u User, isLogin bool) string {
var message string
if isNullOrEmpty(u.Email) {
message = EMAIL_NOT_FOUND + SEPARATOR
}
if isNullOrEmpty(u.Password) {
message += PASSWORD_NOT_FOUND + SEPARATOR
}
if !isLogin && isNullOrEmpty(u.Name) {
message += NAME_NOT_FOUND + SEPARATOR
}
return message
}
func isNullOrEmpty(s string) bool {
return len(strings.Trim(s, " ")) == 0 || s == ""
}
func alreadyLoggedIn(req *http.Request) bool {
var userSession Session
var count int
ok := false
c, err := req.Cookie("uuid")
if err != nil {
return false
}
db.Debug().Select("email,session_id").Where("session_id = ?", c.Value).First(&Session{}).Scan(&userSession)
if (Session{}) != userSession {
db.Debug().Where("email = ?", userSession.Email).Find(&User{}).Count(&count)
ok = count > 0
}
return ok
}
func isValidLogin(u User) bool {
dbUser := isUserExists(u)
if (User{}) != dbUser {
err := bcrypt.CompareHashAndPassword([]byte(dbUser.Password), []byte(u.Password))
if err != nil {
return false
}
return true
} else {
return false
}
}