diff --git a/handler/auth.go b/handler/auth.go index 22630e1..f829962 100644 --- a/handler/auth.go +++ b/handler/auth.go @@ -73,7 +73,7 @@ func (handler HandlerAuthImpl) handleSignIn() http.HandlerFunc { http.Error(w, "Invalid email or password", http.StatusUnauthorized) } else { utils.LogError("Error signing in", err) - http.Error(w, "An error occurred", http.StatusInternalServerError) + http.Error(w, "InternalServerErr", http.StatusInternalServerError) } return } diff --git a/handler/auth_test.go b/handler/auth_test.go new file mode 100644 index 0000000..921902a --- /dev/null +++ b/handler/auth_test.go @@ -0,0 +1,11 @@ +package handler + +import ( + "testing" +) + +func TestHandleSignIn(t *testing.T) { + t.Parallel() + t.Run("should signIn and return session cookie", func(t *testing.T) { + }) +} diff --git a/handler/default.go b/handler/default.go index 3810adc..95c990d 100644 --- a/handler/default.go +++ b/handler/default.go @@ -14,7 +14,7 @@ func GetHandler(d *sql.DB) http.Handler { router.HandleFunc("/", service.HandleIndexAnd404(d)) - handlerAuth := NewHandlerAuth(d, service.NewServiceAuthImpl(db.NewDbAuthSqlite(d))) + handlerAuth := NewHandlerAuth(d, service.NewServiceAuthImpl(db.NewDbAuthSqlite(d), service.NewServiceMail())) // Serve static files (CSS, JS and images) router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) diff --git a/service/auth.go b/service/auth.go index e8b2eaa..54b2b76 100644 --- a/service/auth.go +++ b/service/auth.go @@ -49,11 +49,13 @@ type ServiceAuth interface { type ServiceAuthImpl struct { dbAuth db.DbAuth + mail ServiceMail } -func NewServiceAuthImpl(dbAuth db.DbAuth) *ServiceAuthImpl { +func NewServiceAuthImpl(dbAuth db.DbAuth, mail ServiceMail) *ServiceAuthImpl { return &ServiceAuthImpl{ dbAuth: dbAuth, + mail: mail, } } @@ -399,7 +401,8 @@ func HandleDeleteAccountComp(db *sql.DB) http.HandlerFunc { return } - go utils.SendMail(user.Email, "Account deleted", "Your account has been deleted") + //TODO + // go utils.SendMail(user.Email, "Account deleted", "Your account has been deleted") utils.DoRedirect(w, r, "/") } @@ -577,7 +580,8 @@ func HandleResetPasswordComp(db *sql.DB) http.HandlerFunc { utils.TriggerToast(w, r, "error", "Internal Server Error") return } - utils.SendMail(email, "Reset Password", mail.String()) + //TODO + // utils.SendMail(email, "Reset Password", mail.String()) } utils.TriggerToast(w, r, "info", "If the email exists, an email has been sent") @@ -612,7 +616,8 @@ func sendVerificationEmail(db *sql.DB, userId string, email string) { utils.LogError("Could not render welcome email", err) return } - utils.SendMail(email, "Welcome to ME-FIT", w.String()) + //TODO + // utils.SendMail(email, "Welcome to ME-FIT", w.String()) } func TryCreateSessionAndSetCookie(r *http.Request, w http.ResponseWriter, db *sql.DB, user_uuid uuid.UUID) error { diff --git a/service/mail.go b/service/mail.go new file mode 100644 index 0000000..ea89331 --- /dev/null +++ b/service/mail.go @@ -0,0 +1,93 @@ +package service + +import ( + "fmt" + "log" + "me-fit/utils" + "net/smtp" + "os" +) + +type ServiceMail interface { + SendMail(to string, subject string, message string) error +} + +func NewServiceMail() ServiceMail { + enabled := os.Getenv("SMTP_ENABLED") == "true" + host := os.Getenv("SMTP_HOST") + port := os.Getenv("SMTP_PORT") + user := os.Getenv("SMTP_USER") + pass := os.Getenv("SMTP_PASS") + fromMail := os.Getenv("SMTP_FROM_MAIL") + fromName := os.Getenv("SMTP_FROM_NAME") + + if enabled { + if host == "" { + log.Fatal("SMTP_HOST must be set") + } + if port == "" { + log.Fatal("SMTP_PORT must be set") + } + if user == "" { + log.Fatal("SMTP_USER must be set") + } + if pass == "" { + log.Fatal("SMTP_PASS must be set") + } + if fromMail == "" { + log.Fatal("SMTP_FROM_MAIL must be set") + } + if fromName == "" { + log.Fatal("SMTP_FROM_NAME must be set") + } + + return NewServiceMailSmtp(host, port, user, pass, fromMail, fromName) + } else { + if utils.Environment == "prod" { + log.Fatal("SMTP_ENABLED must be set to true in production") + } + return NewServiceMailStub() + } +} + +type ServiceMailSmtp struct { + host string + port string + auth smtp.Auth + fromMail string + fromName string +} + +func NewServiceMailSmtp( + host string, + port string, + user string, + pass string, + fromMail string, + fromName string) ServiceMailSmtp { + return ServiceMailSmtp{ + host: host, + port: port, + auth: smtp.PlainAuth("", user, pass, host), + fromMail: fromMail, + fromName: fromName, + } +} + +func (service ServiceMailSmtp) SendMail(to string, subject string, message string) error { + + msg := fmt.Sprintf("From: %v <%v>\nTo: %v\nSubject: %v\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n%v", service.fromName, service.fromMail, to, subject, message) + + return smtp.SendMail(service.host+":"+service.port, service.auth, service.fromMail, []string{to}, []byte(msg)) +} + +type ServiceMailStub struct { +} + +func NewServiceMailStub() ServiceMailStub { + return ServiceMailStub{} +} + +func (service ServiceMailStub) SendMail(to string, subject string, message string) error { + return nil +} diff --git a/utils/env.go b/utils/env.go index 0f55b0e..5ddb5bd 100644 --- a/utils/env.go +++ b/utils/env.go @@ -7,44 +7,14 @@ import ( ) var ( - SmtpHost string - SmtpPort string - SmtpUser string - SmtpPass string - SmtpFromMail string - SmtpFromName string - BaseUrl string - Environment string + BaseUrl string + Environment string ) func MustInitEnv() { - SmtpHost = os.Getenv("SMTP_HOST") - SmtpPort = os.Getenv("SMTP_PORT") - SmtpUser = os.Getenv("SMTP_USER") - SmtpPass = os.Getenv("SMTP_PASS") - SmtpFromMail = os.Getenv("SMTP_FROM_MAIL") - SmtpFromName = os.Getenv("SMTP_FROM_NAME") BaseUrl = os.Getenv("BASE_URL") Environment = os.Getenv("ENVIRONMENT") - if SmtpHost == "" { - log.Fatal("SMTP_HOST must be set") - } - if SmtpPort == "" { - log.Fatal("SMTP_PORT must be set") - } - if SmtpUser == "" { - log.Fatal("SMTP_USER must be set") - } - if SmtpPass == "" { - log.Fatal("SMTP_PASS must be set") - } - if SmtpFromMail == "" { - log.Fatal("SMTP_FROM_MAIL must be set") - } - if SmtpFromName == "" { - log.Fatal("SMTP_FROM_NAME must be set") - } if BaseUrl == "" { log.Fatal("BASE_URL must be set") } diff --git a/utils/mail.go b/utils/mail.go deleted file mode 100644 index 029d6c6..0000000 --- a/utils/mail.go +++ /dev/null @@ -1,15 +0,0 @@ -package utils - -import ( - "fmt" - "net/smtp" -) - -func SendMail(to string, subject string, message string) error { - - auth := smtp.PlainAuth("", SmtpUser, SmtpPass, SmtpHost) - - msg := fmt.Sprintf("From: %v <%v>\nTo: %v\nSubject: %v\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n%v", SmtpFromName, SmtpFromMail, to, subject, message) - - return smtp.SendMail(SmtpHost+":"+SmtpPort, auth, SmtpFromMail, []string{to}, []byte(msg)) -}