Added mail verification for user creation

master
Alex 2021-07-12 20:31:23 +02:00
parent 2a63114ffb
commit ae65dcfc98
1 changed files with 45 additions and 43 deletions

View File

@ -24,7 +24,7 @@ type RegisterInput struct {
}
func NewUser(c *fiber.Ctx) error {
// swagger:operation POST /users User usersNewUser
// swagger:operation POST /users User user
// ---
// summary: Create new user
// produces:
@ -59,7 +59,6 @@ func NewUser(c *fiber.Ctx) error {
// type: string
// responses:
// '201':
// description: user created
// "$ref": "#/definitions/User"
// '400':
// description: format is not correct
@ -128,52 +127,15 @@ func NewUser(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusInternalServerError)
}
// account activation via email
userActivationId, err := createUserActivation(user.Id)
userActionId, err := createUserAction(user.Id)
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}
type JsonMessage struct {
Key string `json:"k"`
Mail string `json:"m"`
TemplateId int `json:"t"`
LanguageId int `json:"l"`
BodyData *json.RawMessage `json:"d"`
}
bodyData := json.RawMessage(`{"name": "` + user.Name + `",
"email": "` + user.Email + `",
"url": "http://localhost:3000/api/v1/user/activate/` + userActivationId + `"}`)
js := JsonMessage{Key: "mykey", Mail: "app@roese.dev", TemplateId: 0, LanguageId: user.LanguageId, BodyData: &bodyData}
reqBody, err := json.MarshalIndent(&js, "", "\t")
if err != nil {
log.Infoln("error reqBody", err)
}
rabbitmq.Publish(string(reqBody))
/*resp, err := http.Post(cfg.Mail.Host+"/send", "application/json", bytes.NewBuffer(reqBody))
if err != nil {
log.Infoln("err http post", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Infoln("err reading body", err)
}
log.Infoln("body", body)
log.Infoln("StatusCode", resp.StatusCode)*/
rabbitmq.PublishMail(user.Email, 0, user.LanguageId, json.RawMessage(`{"name": "`+user.Name+`",
"email": "`+user.Email+`",
"url": "http://localhost:3000/api/v1/user/action/0/`+userActionId+`"}`))
sessionId, err := createUserSession(db, user.Id, c.IP(), string(c.Context().UserAgent()))
@ -191,3 +153,43 @@ func NewUser(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusCreated)
}
func ActivateUser(c *fiber.Ctx) error {
// swagger:operation POST /user/action/{actionType}/{actionId} User activation
// ---
// summary: Activate user
// parameters:
// - name: id
// in: query
// description: action id
// type: string
// required: true
// responses:
// '200':
// description: User was activated
// '401':
// description: action Id is incorrect or expired
db := database.DB
deleteExpiredUserActions(db)
userAction := structs.UserAction{Id: c.Params("actionId")}
log.Infoln("activate user", c.Params("actionType"), c.Params("actionId"))
db.Select("user_id").Where("id = ?", c.Params("actionId")).Find(&userAction)
log.Infoln("usA", userAction)
if userAction.UserId == "" {
log.Info("StatusUnauthorized")
return c.SendStatus(fiber.StatusUnauthorized)
}
db.Delete(userAction)
db.Model(structs.User{Id: userAction.UserId}).Update("state", 0)
return c.SendStatus(fiber.StatusOK)
}