forked from makeless/makeless-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_reset.go
99 lines (85 loc) · 2.79 KB
/
password_reset.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
package makeless_go
import (
"database/sql"
"errors"
"github.com/gin-gonic/gin"
"github.com/makeless/makeless-go/http"
"github.com/makeless/makeless-go/model"
"github.com/makeless/makeless-go/struct"
"gorm.io/gorm"
h "net/http"
"sync"
)
func (makeless *Makeless) passwordReset(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().POST(
"/api/password-reset",
func(c *gin.Context) {
var err error
var bcrypted string
var tx = http.GetDatabase().GetConnection().WithContext(c).Begin(new(sql.TxOptions))
var token = c.Query("token")
var user = &makeless_go_model.User{
RWMutex: new(sync.RWMutex),
}
var passwordReset = &_struct.PasswordReset{
RWMutex: new(sync.RWMutex),
}
defer func() {
if r := recover(); r != nil {
tx.Rollback()
panic(r)
}
}()
if err = tx.Error; err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if err = c.ShouldBind(passwordReset); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
var passwordRequest = &makeless_go_model.PasswordRequest{
Token: &token,
RWMutex: new(sync.RWMutex),
}
if passwordRequest, err = http.GetDatabase().GetPasswordRequest(http.GetDatabase().GetConnection().WithContext(c), passwordRequest); err != nil {
switch errors.Is(err, gorm.ErrRecordNotFound) {
case true:
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
default:
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
}
return
}
if user, err = http.GetDatabase().GetUserByField(http.GetDatabase().GetConnection().WithContext(c), user, "email", *passwordRequest.GetEmail()); err != nil {
switch errors.Is(err, gorm.ErrRecordNotFound) {
case true:
c.AbortWithStatusJSON(h.StatusUnauthorized, http.Response(err, nil))
default:
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
}
return
}
if bcrypted, err = http.GetSecurity().EncryptPassword(*passwordReset.GetPassword()); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if _, err = http.GetDatabase().UpdatePassword(tx, user, bcrypted); err != nil {
tx.Rollback()
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if _, err = http.GetDatabase().UpdatePasswordRequest(tx, passwordRequest); err != nil {
tx.Rollback()
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if err := tx.Commit().Error; err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, nil))
},
)
return nil
}