-
Notifications
You must be signed in to change notification settings - Fork 0
/
signUp.go
105 lines (99 loc) · 3.09 KB
/
signUp.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
package api
import (
"errors"
"github.com/amallick86/sanyukta/db"
"github.com/amallick86/sanyukta/models"
"github.com/amallick86/sanyukta/util"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"net/http"
"strconv"
"strings"
"time"
)
type createAccountRequest struct {
FirstName string `json:"FirstName" binding:"required,alphanum"`
LastName string `json:"LastName" binding:"required,alphanum"`
Email string `json:"Email" binding:"required,email"`
UserName string `json:"UserName" binding:"required,alphanum"`
Password string `json:"password" binding:"required,min=6"`
}
// CreateUser handles request for user creation
// @Summary create a new user
// @Tags AUTH
// @ID CreateUser
// @Accept json
// @Produce json
// @Param data body createAccountRequest true "create user"
// @Success 201 {object} sendOTPResponse
// @Failure 400 {object} Err
// @Failure 500 {object} Err
// @Router /auth/signup [post]
func (server *Server) createUser(ctx *gin.Context) {
var req createAccountRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}
hashedPassword, err := util.HashPassword(req.Password)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
}
user, err := server.store.CreateUser(ctx, db.CreateUserParams{
FirstName: req.FirstName,
LastName: req.LastName,
Email: req.Email,
UserName: req.UserName,
Password: hashedPassword,
})
if err != nil {
if strings.Contains(err.Error(), "Duplicate entry") {
logrus.Error(err)
ctx.JSON(http.StatusConflict, errorResponse(err))
return
}
logrus.Error(err)
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
otp, expTime, err := util.CheckOtpValidation(models.EmailOTP{
ID: util.CreateNullInt64(false, 0),
UID: user.UID,
FullName: req.FirstName + " " + req.LastName,
OTP: util.CreateNullInt64(false, 0),
CountOfDay: util.CreateNullInt16(false, 0),
CreatedAt: util.CreateNUllTime(false, time.Time{}),
ExpiredAt: util.CreateNUllTime(false, time.Time{}),
})
if err != nil {
logrus.Error(err)
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
param := util.SendEmailVerificationCodeParam{
SenderEmail: server.config.SenderEmail,
SendInBlueEmail: server.config.SendInBlueEmail,
SendInBluePassword: server.config.SendInBluePassword,
SendInBlueAddress: server.config.ServerAddress,
SMTPHost: server.config.SMTPHost,
UserName: req.FirstName + " " + req.LastName,
UserEmail: req.Email,
OTP: strconv.FormatInt(otp, 10),
}
err = util.SendEmailVerificationCode(param)
if err != nil {
logrus.Error(err)
ctx.JSON(http.StatusInternalServerError, errorResponse(errors.New("unable to send email")))
}
_, err = server.store.AddOtpOfUser(ctx, db.AddOtpOfUserParam{
UID: user.UID,
OTP: otp,
CountOfDay: 1,
ExpiredAt: expTime,
})
ctx.JSON(http.StatusCreated, sendOTPResponse{
ExpiredAt: expTime,
Message: "congratulation your account has been successfully created",
})
return
}