diff --git a/auth/auth.go b/auth/auth.go index e6f9fa20..6df52067 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -30,6 +30,7 @@ type Authentication interface { RenewAccessToken(ctx echo.Context) error VerifyEmail(ctx echo.Context) error ResetPassword(ctx echo.Context) error + Invites(ctx echo.Context) error } // New is the constructor function returns an Authentication implementation diff --git a/auth/invites.go b/auth/invites.go new file mode 100644 index 00000000..f7fb01bc --- /dev/null +++ b/auth/invites.go @@ -0,0 +1,38 @@ +package auth + +import ( + "encoding/json" + "net/http" + "strings" + + "github.com/labstack/echo/v4" +) + +type List struct { + Emails string +} + +func (a *auth) Invites(ctx echo.Context) error { + var list List + err := json.NewDecoder(ctx.Request().Body).Decode(&list) + if err != nil { + a.logger.Log(ctx, err) + return ctx.JSON(http.StatusBadRequest, echo.Map{ + "error": err.Error(), + "msg": "error decode body, expecting and array of emails", + }) + } + err = a.emailClient.WelcomeEmail(strings.Split(list.Emails, ",")) + if err != nil { + a.logger.Log(ctx, err) + return ctx.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + "msg": "err sending invites", + }) + } + + a.logger.Log(ctx, err) + return ctx.JSON(http.StatusAccepted, echo.Map{ + "msg": "success", + }) +} diff --git a/auth/reset_password.go b/auth/reset_password.go index e5f6e5d0..8c6f2efd 100644 --- a/auth/reset_password.go +++ b/auth/reset_password.go @@ -62,7 +62,8 @@ func (a *auth) ResetPassword(ctx echo.Context) error { if err != nil { a.logger.Log(ctx, err) return ctx.JSON(http.StatusBadRequest, echo.Map{ - "error": "request body could not be decoded", + "error": err.Error(), + "msg": "request body could not be decoded", }) } diff --git a/config/config.go b/config/config.go index 290909cc..458ae642 100644 --- a/config/config.go +++ b/config/config.go @@ -5,7 +5,6 @@ import ( "log" "os" - "github.com/fatih/color" "github.com/spf13/viper" ) @@ -101,7 +100,6 @@ func NewStoreConfig() (*Store, error) { } func (sc *Store) Endpoint() string { - color.Green("stoer: %s", sc) return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?pool_max_conns=1000&sslmode=disable", sc.User, sc.Password, sc.Host, sc.Port, sc.Database) } diff --git a/router/helpers.go b/router/helpers.go index 64124b01..3b830f5a 100644 --- a/router/helpers.go +++ b/router/helpers.go @@ -12,7 +12,9 @@ import ( // RegisterAuthRoutes includes all the auth related endpoints func RegisterAuthRoutes(authRouter *echo.Group, authSvc auth.Authentication) { + //send-email/welcome authRouter.Add(http.MethodPost, "/signup", authSvc.SignUp) + authRouter.Add(http.MethodPost, "/send-email/welcome", authSvc.Invites) authRouter.Add(http.MethodGet, "/signup/verify", authSvc.VerifyEmail) authRouter.Add(http.MethodPost, "/signin", authSvc.SignIn) authRouter.Add(http.MethodPost, "/token", authSvc.SignIn) diff --git a/services/email/createEmail.go b/services/email/createEmail.go index 8d722334..9d09dd7f 100644 --- a/services/email/createEmail.go +++ b/services/email/createEmail.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/containerish/OpenRegistry/types" - "github.com/fatih/color" "github.com/sendgrid/sendgrid-go/helpers/mail" ) @@ -12,37 +11,32 @@ func (e *email) CreateEmail(u *types.User, kind EmailKind, token string) (*mail. mailReq := &Mail{} m := mail.NewV3Mail() - mailReq.To = u.Email + mailReq.To = append(mailReq.To, u.Email) mailReq.Data.Username = u.Username switch kind { case VerifyEmailKind: m.SetTemplateID(e.config.VerifyEmailTemplateId) - mailReq.Name = "Verify Email" - mailReq.Subject = "OpenRegistry - Verify Email" + mailReq.Name = "OpenRegistry" + mailReq.Subject = "Verify Email" mailReq.Data.Link = fmt.Sprintf("%s/auth/signup/verify?token=%s", e.backendEndpoint, token) case ResetPasswordEmailKind: m.SetTemplateID(e.config.ForgotPasswordTemplateId) - mailReq.Name = "Password Reset" - mailReq.Subject = "OpenRegistry - Forgot Password" + mailReq.Name = "OpenRegistry" + mailReq.Subject = "Forgot Password" mailReq.Data.Link = fmt.Sprintf("%s/auth/reset-password?token=%s", e.backendEndpoint, token) - case WelcomeEmailKind: - mailReq.Name = "Welcome to OpenRegistry" - mailReq.Subject = "Welcome to OpenRegistry" - mailReq.Data.Link = fmt.Sprintf("%s/send-email/welcome", e.backendEndpoint) - m.SetTemplateID(e.config.WelcomeEmailTemplateId) default: return nil, fmt.Errorf("incorrect email kind") } - color.Magenta("SendAs: %s", e.config.SendAs) email := mail.NewEmail(mailReq.Name, e.config.SendAs) m.SetFrom(email) p := mail.NewPersonalization() + tos := []*mail.Email{ - mail.NewEmail(mailReq.To, mailReq.To), + mail.NewEmail(mailReq.To[0], mailReq.To[0]), } p.AddTos(tos...) diff --git a/services/email/email.go b/services/email/email.go index 673c291a..8b71fca9 100644 --- a/services/email/email.go +++ b/services/email/email.go @@ -23,7 +23,7 @@ type MailData struct { type Mail struct { Data MailData Name string - To string + To []string Subject string Body string Mtype MailType @@ -32,6 +32,7 @@ type Mail struct { type MailService interface { CreateEmail(u *types.User, kind EmailKind, token string) (*mail.SGMailV3, error) SendEmail(u *types.User, token string, kind EmailKind) error + WelcomeEmail(list []string) error } func New(config *config.Email, backendEndpoint string) MailService { diff --git a/services/email/welcome_email.go b/services/email/welcome_email.go new file mode 100644 index 00000000..b6c68a4f --- /dev/null +++ b/services/email/welcome_email.go @@ -0,0 +1,36 @@ +package email + +import ( + "fmt" + "net/http" + + "github.com/sendgrid/sendgrid-go/helpers/mail" +) + +func (e *email) WelcomeEmail(list []string) error { + + mailReq := &Mail{} + m := mail.NewV3Mail() + + m.SetTemplateID(e.config.WelcomeEmailTemplateId) + mailReq.Name = "OpenRegistry" + mailReq.Subject = "Welcome to OpenRegistry" + mailReq.Data.Link = fmt.Sprintf("%s/send-email/welcome", e.backendEndpoint) + + email := mail.NewEmail(mailReq.Name, e.config.SendAs) + m.SetFrom(email) + p := mail.NewPersonalization() + + var tos []*mail.Email + for _, v := range list { + tos = append(tos, mail.NewEmail(v, v)) + } + p.AddTos(tos...) + m.AddPersonalizations(p) + + resp, err := e.client.Send(m) + if err != nil && resp.StatusCode != http.StatusAccepted { + return fmt.Errorf("ERR_SEND_EMAIL: %w", err) + } + return nil +}