-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.users.go
140 lines (123 loc) · 2.99 KB
/
handler.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// handlers.user.go
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
jwt "github.com/appleboy/gin-jwt"
"github.com/gin-gonic/gin"
)
type (
userField struct {
Field string `json:"field"`
Data string `json:"data"`
Id int `json:"id"`
}
userEmail struct {
Email string `json:"email" binding:"required"`
}
)
func cabinetHandler(c *gin.Context) {
claims := jwt.ExtractClaims(c)
user, _ := claims["id"].(string)
userData, err := readUser(user)
if err != nil {
c.JSON(200, gin.H{"data": err})
} else {
c.JSON(200, gin.H{"data": userData})
}
}
func registerHandler(c *gin.Context) {
x, _ := ioutil.ReadAll(c.Request.Body)
var user User
if err := json.Unmarshal(x, &user); err != nil {
c.AbortWithStatus(400)
return
}
if name, err := user.createUser(); err == nil {
c.JSON(200, gin.H{"title": "User registered", "id": name})
} else {
c.JSON(400, gin.H{"title": err.Error()})
c.AbortWithStatus(400)
}
}
func editUserField(c *gin.Context) {
x, _ := ioutil.ReadAll(c.Request.Body)
var field userField
if err := json.Unmarshal(x, &field); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
if err := updateUser(field.Field, field.Data, field.Id); err == nil {
c.JSON(200, gin.H{"title": "Field modifiyed", "data": "kek"})
} else {
c.AbortWithStatus(400)
}
}
func resetPassword(c *gin.Context) {
var pass map[string]string
email := c.Param("hash")
if err := c.BindJSON(&pass); err != nil {
c.JSON(400, gin.H{"title": "Невірний параметр"})
return
}
if err := passwordResetter(pass["password"], email); err == nil {
c.JSON(200, gin.H{"title": "Пароль змінено"})
} else {
c.JSON(400, gin.H{"title": "Невірне посилання"})
}
}
func activateAccount(c *gin.Context) {
hash := c.Param("hash")
var user User
if usr, err := user.readHash(hash); err == nil {
if err = user.deleteHash(hash); err != nil {
c.JSON(400, gin.H{"title": err})
return
}
if err = setActiveField(usr.Email); err != nil {
c.JSON(400, gin.H{"title": err})
return
}
c.JSON(200, gin.H{"title": "ok"})
} else {
if err := c.AbortWithError(404, errors.New("код застарілий")); err != nil {
log.Error(err)
}
}
}
func sendResetPasswordLink(c *gin.Context) {
var user userEmail
if err := c.BindJSON(&user); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
var u User
u.Email = user.Email
u.Name = user.Email
res, err := u.writeHash()
if err != nil {
if err := c.AbortWithError(404, errors.New("код застарілий")); err != nil {
log.Error(err)
}
return
}
if err = doSendEmail(u, *res, "email_password"); err != nil {
if err := c.AbortWithError(404, err); err != nil {
log.Error(err)
}
}
}
func checkPasswordLink(c *gin.Context) {
var u User
hash, err := u.readHash(c.Param("hash"))
if hash == nil {
if err := c.AbortWithError(404, err); err != nil {
log.Error(err)
}
} else {
c.Status(200)
}
}