diff --git a/main.go b/main.go index d5b9f9f..87d6222 100644 --- a/main.go +++ b/main.go @@ -30,8 +30,10 @@ func main() { d := gomail.NewDialer("localhost", 1025, "from@gmail.com", "email_password") d.TLSConfig = &tls.Config{InsecureSkipVerify: true} + userconfig := user.NewUserConfig() + sample.Register(router) - user.Register(router, tokenAuth, uc, d) + user.Register(router, tokenAuth, uc, d, userconfig) }, } diff --git a/pkg/user/templates/token.tmpl b/pkg/user/templates/token.tmpl index 86a3cb1..cbcf658 100644 --- a/pkg/user/templates/token.tmpl +++ b/pkg/user/templates/token.tmpl @@ -1,5 +1,5 @@ Hallo {{ .username }},

-um deine E-Mail addresse zu bestätigen klicke auf folgenden link: {{ .url }}

+um deine E-Mail addresse zu bestätigen klicke auf folgenden link: LINK

Kthxbye,
system \ No newline at end of file diff --git a/pkg/user/user.go b/pkg/user/user.go index 8a5ab1a..2a35ce8 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -25,8 +25,31 @@ var tokenAuth *jwtauth.JWTAuth var d *gomail.Dialer -func Register(router chi.Router, token *jwtauth.JWTAuth, userClient UserClient, dialer *gomail.Dialer) { +type UserConfig struct { + RegisterMail *gomail.Message + URLValidationToken string +} + +var userconfig UserConfig + +func NewUserConfig() UserConfig { + uc := UserConfig{} + + m := gomail.NewMessage() + m.SetHeader("From", "test@keks.cloud") + m.SetHeader("To", "to@example.com") + m.SetHeader("Subject", "Activate your Account") + uc.RegisterMail = m + + uc.URLValidationToken = "http://localhost/validate/%s" + + + return uc +} + +func Register(router chi.Router, token *jwtauth.JWTAuth, userClient UserClient, dialer *gomail.Dialer, config UserConfig) { uc = userClient + userconfig = config ren = render.New(render.Options{ //Layout: "layout", FileSystem: &render.EmbedFileSystem{ @@ -111,23 +134,21 @@ func register(w http.ResponseWriter, r *http.Request) { buf: &bytes.Buffer{}, } - ren.HTML(rw, http.StatusOK, "token", map[string]string{"username": r.FormValue("username"), "url": "http://www.google.de"}) + token, err := uc.getMailValidationToken(r.FormValue("email"), true) + if err != nil { + panic(err) + } + + ren.HTML(rw, http.StatusOK, "token", map[string]string{"username": r.FormValue("username"), "url": fmt.Sprintf(userconfig.URLValidationToken, token)}) content, err := io.ReadAll(rw.buf) if err != nil { panic(err) } - m := gomail.NewMessage() - m.SetHeader("From", "test@keks.cloud") - m.SetHeader("To", "to@example.com") - m.SetHeader("Subject", "Gomail test subject") - m.SetBody("text/plain", "This is Gomail test body") - m.SetBody("text/html", string(content)) - - - - if err := d.DialAndSend(m); err != nil { + mail := *userconfig.RegisterMail + mail.SetBody("text/html", string(content)) + if err := d.DialAndSend(&mail); err != nil { fmt.Println(err) panic(err) } diff --git a/pkg/user/userclient.go b/pkg/user/userclient.go index f73014b..bf3e127 100644 --- a/pkg/user/userclient.go +++ b/pkg/user/userclient.go @@ -1,6 +1,7 @@ package user import ( + "encoding/base64" "errors" "fmt" "math/rand" @@ -9,7 +10,7 @@ import ( type UserClient interface { register(username, password, email string) (bool, error) login(username, password string, requiredMailValidation bool) (bool, error) - getMailValidationToken(forceRecreate bool) (string, error) + getMailValidationToken(email string, forceRecreate bool,) (string, error) } type UserClientMemory struct { @@ -54,8 +55,9 @@ func (uc UserClientMemory) login(username, password string, requiredMailValidati return false, nil } -func (uc UserClientMemory) getMailValidationToken(forceRecreate bool) (string, error) { - token := randomString(5) +func (uc UserClientMemory) getMailValidationToken(email string, forceRecreate bool) (string, error) { + token := randomString(35) + token = fmt.Sprintf("%v/%v", base64.StdEncoding.EncodeToString([]byte(email)), token) return token, nil }