From 752b125378749c2c869d3950c54586b04dc8ae56 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 15 Jun 2021 22:04:27 +0200 Subject: [PATCH] added user activation to createUser --- routers/api/v1/user/user.go | 53 +++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index a8e1e6f..d40b8cd 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -1,10 +1,14 @@ package user import ( + "bytes" "crypto/rand" "database/sql" "encoding/base64" + "encoding/json" + "io/ioutil" "math/big" + "net/http" "regexp" "strings" "time" @@ -21,7 +25,7 @@ import ( "gorm.io/gorm" ) -var cfg = &config.Cfg.Settings +var cfg = &config.Cfg type LoginInput struct { Username string `json:"username"` @@ -133,6 +137,45 @@ func NewUser(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusInternalServerError) } + // account activation via email + + 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": "https://roese.dev/activate/1234567890"}`) + + js := JsonMessage{Key: "mykey", Mail: "app@roese.dev", TemplateId: 0, LanguageId: 0, BodyData: &bodyData} + + reqBody, err := json.MarshalIndent(&js, "", "\t") + + if err != nil { + log.Infoln("error reqBody", err) + } + + 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) + sessionId, err := createUserSession(db, user.ID, c.IP(), string(c.Context().UserAgent())) if err != nil { @@ -223,7 +266,7 @@ func isUpper(s string) bool { } func isUsernameValid(u string) bool { - if len(u) < int(cfg.UsernameMinLen) || len(u) > int(cfg.UsernameMaxLen) { + if len(u) < int(cfg.Settings.UsernameMinLen) || len(u) > int(cfg.Settings.UsernameMaxLen) { return false } return true @@ -232,14 +275,14 @@ func isUsernameValid(u string) bool { var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") func isEmailValid(e string) bool { - if len(e) < int(cfg.EmailMinLen) || len(e) > int(cfg.EmailMaxLen) { + if len(e) < int(cfg.Settings.EmailMinLen) || len(e) > int(cfg.Settings.EmailMaxLen) { return false } return emailRegex.MatchString(e) } func isPasswordValid(p string) bool { - if len(p) < int(cfg.PasswordMinLen) || len(p) > int(cfg.PasswordMaxLen) { + if len(p) < int(cfg.Settings.PasswordMinLen) || len(p) > int(cfg.Settings.PasswordMaxLen) { return false } return true @@ -324,7 +367,7 @@ func createUserSession(db *gorm.DB, userId string, ip string, userAgent string) } func getExpiresTime() time.Time { - return time.Now().Add(time.Hour * time.Duration(cfg.ExpiredTime)) + return time.Now().Add(time.Hour * time.Duration(cfg.Settings.ExpiredTime)) } func Login(c *fiber.Ctx) error {