Added resend, fixed CSRF
This commit is contained in:
@@ -5,10 +5,9 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"gopkg.in/gomail.v2"
|
||||
"github.com/resend/resend-go/v2"
|
||||
)
|
||||
|
||||
type ContactForm struct {
|
||||
@@ -31,29 +30,29 @@ func HandleContact(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Received form", form)
|
||||
fmt.Println("ENV:", os.Environ())
|
||||
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("From", os.Getenv("SMTP_EMAIL"))
|
||||
// m.SetHeader("Reply-To", form.EMail)
|
||||
m.SetHeader("To", os.Getenv("SMTP_EMAIL"))
|
||||
m.SetHeader("Subject", form.Subject)
|
||||
m.SetBody("text/plain", fmt.Sprintf("%s %s vous a envoyé un email:\n\n%s", form.Firstname, form.Lastname, form.Message))
|
||||
port, err := strconv.Atoi(os.Getenv("SMTP_PORT"))
|
||||
if err != nil {
|
||||
port = 587
|
||||
apiKey := os.Getenv("RESEND_APIKEY")
|
||||
sendTo := os.Getenv("SMTP_EMAIL")
|
||||
client := resend.NewClient(apiKey)
|
||||
params := &resend.SendEmailRequest{
|
||||
From: "onboarding@resend.dev",
|
||||
To: []string{sendTo},
|
||||
Subject: form.Subject,
|
||||
Html: fmt.Sprintf("<h1><strong>%s %s</strong> (%s) vous a envoyé un mail.</h1></br></br>%s",
|
||||
form.Firstname, form.Lastname, form.EMail, form.Message),
|
||||
ReplyTo: form.EMail,
|
||||
}
|
||||
d := gomail.NewDialer(os.Getenv("SMTP_DOMAIN"), port, os.Getenv("SMTP_EMAIL"), os.Getenv("SMTP_APP_PASSWORD"))
|
||||
|
||||
if err = d.DialAndSend(); err != nil {
|
||||
sent, err := client.Emails.Send(params)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(*sent)
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Email sent.",
|
||||
|
||||
@@ -13,6 +13,13 @@ import (
|
||||
"fr.latosa-escrima/utils"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/csrf"
|
||||
)
|
||||
|
||||
var CSRFMiddleware core.Middleware = csrf.Protect(
|
||||
core.CSRF_KEY,
|
||||
csrf.Secure(os.Getenv("ENVIRONMENT") != "DEV"),
|
||||
csrf.HttpOnly(true),
|
||||
)
|
||||
|
||||
func CORS(next http.Handler) http.Handler {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
var UserRoutes = map[string]core.Handler{
|
||||
"/users/login": {
|
||||
Handler: users.HandleLogin,
|
||||
Middlewares: []core.Middleware{Methods("POST")}},
|
||||
Middlewares: []core.Middleware{Methods("POST"), CSRFMiddleware}},
|
||||
// Could add users:own:get permission there, but don't think it's
|
||||
// necessary
|
||||
"/users/me": {
|
||||
|
||||
Reference in New Issue
Block a user