-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
51 lines (46 loc) · 1.31 KB
/
users.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
package main
import (
"fmt"
"github.com/securityfirst/umbrella-api/models"
"regexp"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
)
func (um *Umbrella) loginEndpoint(c *gin.Context) {
var json struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
}
if err := c.Bind(&json); err == nil {
var u models.User
err := um.Db.SelectOne(&u, "select id, name, email, password, token, role from users where email=?", json.Email)
if err != nil {
fmt.Println(err)
match, _ := regexp.MatchString("connection refused", err.Error())
if match {
um.checkErr(c, err)
} else {
c.JSON(401, gin.H{"error": "Email or password incorrect. Please try again"})
}
return
}
err1 := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(json.Password))
fmt.Println(err1)
if err1 == nil {
u.Token = randString(50)
count, err := um.Db.Update(&u)
um.checkErr(c, err)
if err == nil && count == 1 {
c.JSON(200, gin.H{"token": u.Token, "profile": u})
return
}
}
}
c.JSON(401, gin.H{"error": "Email or password incorrect. Please try again"})
}
func (um *Umbrella) loginCheck(c *gin.Context) {
user, err := um.checkUser(c)
um.checkErr(c, err)
loggedIn := user.Id != 0 && err == nil
c.JSON(200, gin.H{"response": loggedIn})
}