-
Notifications
You must be signed in to change notification settings - Fork 10
/
userHandlers.go
220 lines (202 loc) · 6.53 KB
/
userHandlers.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package handlers
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/tamiat/backend/pkg/emailVerification"
"golang.org/x/crypto/bcrypt"
"net/http"
"net/mail"
"strconv"
"github.com/tamiat/backend/pkg/domain/user"
"github.com/tamiat/backend/pkg/errs"
"github.com/tamiat/backend/pkg/middleware"
"github.com/tamiat/backend/pkg/service"
)
type UserHandlers struct {
Service service.UserService
}
type JWT struct {
Token string `json:"token"`
}
type Login struct {
Email string `json:"email" form:"email" binding:"required,email"`
Password string `json:"password" form:"password" binding:"required"`
}
type Signup struct {
Email string `json:"email" form:"email" binding:"required,email"`
Password string `json:"password" form:"password" binding:"required"`
Role string `json:"role" form:"role" binding:"required"`
}
//
// @Summary Signup endpoint
// @Description Provide email and password to login, response is JWT
// @Consume application/x-www-form-urlencoded
// @Produce application/json
// @Param email formData string true "Email"
// @Param password formData string true "Password"
// @Param role formData string true "Role"
// @Success 200 {object} user.User
// @Failure 400 {object} errs.ErrResponse "Bad Request"
// @Failure 500 {object} errs.ErrResponse "Internal server error"
// @Router /signup [post]
func (receiver UserHandlers) Signup(ctx *gin.Context) {
var userObj user.User
var signupRequestData Signup
//decoding request body
if err := ctx.ShouldBind(&signupRequestData); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
userObj.Email = signupRequestData.Email
userObj.Password = signupRequestData.Password
userObj.Role = signupRequestData.Role
//encrypting password
hash, err := bcrypt.GenerateFromPassword([]byte(userObj.Password), 10)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
userObj.Password = string(hash)
//database connection
userObj.ID, err = receiver.Service.Signup(userObj)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
code := emailVerification.CodeGenerator()
hashOTP, err := bcrypt.GenerateFromPassword([]byte(code), 10)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
userObj.Otp = string(hashOTP)
err = receiver.Service.InsertOTP(&userObj)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
err = emailVerification.SendEmail(userObj.Email, code)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
//w.WriteHeader(http.StatusOK)
userObj.Password = ""
userObj.Otp = ""
//json.NewEncoder(w).Encode(userObj)
ctx.JSON(http.StatusOK, userObj)
}
//
// @Summary Login endpoint
// @Description Provide email and password to login, response is JWT
// @Consume application/x-www-form-urlencoded
// @Produce application/json
// @Param email formData string true "Email"
// @Param password formData string true "Password"
// @Success 200 {object} handlers.JWT
// @Failure 400 {object} errs.ErrResponse "Bad Request"
// @Failure 404 {object} errs.ErrResponse "User not found"
// @Failure 401 {object} errs.ErrResponse "Unauthorizes"
// @Failure 500 {object} errs.ErrResponse "Internal server error"
// @Router /login [post]
func (receiver UserHandlers) Login(ctx *gin.Context) {
var userObj user.User
var loginRequestData Login
//decoding request body
if err := ctx.ShouldBind(&loginRequestData); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
userObj.Email = loginRequestData.Email
userObj.Password = loginRequestData.Password
// retrieving hashed password from database
hashedPassword, err := receiver.Service.Login(userObj)
if err != nil {
if err == errs.ErrRecordNotFound {
ctx.JSON(http.StatusNotFound, gin.H{"error": errs.ErrRecordNotFound.Error()})
return
}
ctx.JSON(http.StatusInternalServerError, gin.H{"error": errs.ErrDb.Error()})
return
}
// authentication process
password := loginRequestData.Password
if err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)); err != nil {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": errs.ErrInvalidPassword.Error()})
return
}
//generating token
token, err := middleware.GenerateToken(userObj)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": errs.ErrTokenErr.Error()})
return
}
jwtObj := JWT{Token: token}
ctx.JSON(http.StatusOK, jwtObj)
}
type otp struct {
Otp string `json:"otp" form:"otp" binding:"required"`
}
// @Summary verify user email
// @Description provide user id and otp sent to his email, it consists of 6 characters
// @Consume application/x-www-form-urlencoded
// @Produce application/json
// @Param id path int true "User ID"
// @Param otp formData string true "OTP"
// @Success 200 {object} response.Response
// @Failure 401 {pbject} errs.ErrResponse
// @Failure 500 {object} errs.ErrResponse "Internal server error"
// @Failure 400 {object} errs.ErrResponse "Bad request"
// @Router /confirmEmail/{id} [post]
func (receiver UserHandlers) VerifyEmail(ctx *gin.Context) {
var userObj user.User
var otpObj otp
if err := ctx.ShouldBind(&otpObj); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if len(otpObj.Otp) != 6 {
fmt.Println(otpObj.Otp)
fmt.Println("error 1")
ctx.JSON(http.StatusUnauthorized, gin.H{"error": errs.ErrInvalidVerificationCode.Error()})
return
}
id, err := strconv.Atoi(ctx.Param("id"))
if err != nil {
ctx.JSON(http.StatusBadRequest, errs.ErrParsingID)
return
}
userObj.ID = id
hashedOTP, err := receiver.Service.ReadOTP(&userObj)
err = bcrypt.CompareHashAndPassword([]byte(hashedOTP), []byte(otpObj.Otp))
if err != nil {
fmt.Println("error 2")
ctx.JSON(http.StatusUnauthorized, gin.H{"error": errs.ErrInvalidVerificationCode.Error()})
return
}
err = receiver.Service.VerifyEmail(&userObj)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": errs.ErrDb.Error()})
return
}
ctx.JSON(http.StatusOK, otpObj)
}
// valid used to check email validation
func valid(email string) bool {
_, err := mail.ParseAddress(email)
return err == nil
}
// validateEmailAndPassword validates email and pass
func validateEmailAndPassword(userObj user.User) error {
//err error()
if userObj.Email == "" {
return errs.ErrEmailMissing
}
if !valid(userObj.Email) {
return errs.ErrInvalidEmail
}
if userObj.Password == "" {
return errs.ErrInvalidPassword
}
return nil
}