very rough, templatized html email verification (#50, #51)

This commit is contained in:
Michael Quigley 2022-09-12 15:28:59 -04:00
parent 712bdf734b
commit aa19870d41
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 18 additions and 2 deletions

View File

@ -63,6 +63,19 @@ func (self *createAccountHandler) handleDirectCreate(params identity.CreateAccou
}
func (self *createAccountHandler) handleVerifiedCreate(params identity.CreateAccountParams) middleware.Responder {
if params.Body == nil || params.Body.Email == "" {
logrus.Errorf("missing email")
return identity.NewCreateAccountBadRequest().WithPayload("missing email")
}
token, err := generateApiToken()
if err != nil {
logrus.Errorf("error generating api token: %v", err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
if err := sendVerificationEmail(params.Body.Email, token, self.cfg); err != nil {
logrus.Error(err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
return identity.NewCreateAccountCreated()
}

View File

@ -27,7 +27,7 @@ type ProxyConfig struct {
type EmailConfig struct {
Host string
Port uint16
Port int
Username string
Password string
}

View File

@ -29,9 +29,12 @@ func sendVerificationEmail(emailAddress, token string, cfg *Config) error {
return errors.Wrap(err, "error executing email verification template")
}
subject := "Subject: Welcome to zrok!\n"
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
msg := []byte(subject + mime + buf.String())
auth := smtp.PlainAuth("", cfg.Email.Username, cfg.Email.Password, cfg.Email.Host)
to := []string{emailAddress}
err = smtp.SendMail(fmt.Sprintf("%v:%d", cfg.Email.Host, cfg.Email.Port), auth, cfg.Registration.EmailFrom, to, buf.Bytes())
err = smtp.SendMail(fmt.Sprintf("%v:%d", cfg.Email.Host, cfg.Email.Port), auth, cfg.Registration.EmailFrom, to, msg)
if err != nil {
return errors.Wrap(err, "error sending email verification")
}