Added CSRF & YouTube and dark mode

This commit is contained in:
cdricms
2025-01-22 17:39:03 +01:00
parent 48e761667f
commit 5a5846d853
29 changed files with 1186 additions and 280 deletions

61
backend/api/contact.go Normal file
View File

@@ -0,0 +1,61 @@
package api
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"fr.latosa-escrima/api/core"
"gopkg.in/gomail.v2"
)
type ContactForm struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
EMail string `json:"email"`
Subject string `json:"subject"`
Message string `json:"message"`
}
func HandleContact(w http.ResponseWriter, r *http.Request) {
// TODO: Warning email not being sent ?
var form ContactForm
err := json.NewDecoder(r.Body).Decode(&form)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusBadRequest)
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
}
d := gomail.NewDialer(os.Getenv("SMTP_DOMAIN"), port, os.Getenv("SMTP_EMAIL"), os.Getenv("SMTP_APP_PASSWORD"))
if err = d.DialAndSend(); err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Email sent.",
}.Respond(w, http.StatusAccepted)
}

3
backend/api/core/csrf.go Normal file
View File

@@ -0,0 +1,3 @@
package core
var CSRF_KEY = []byte("32-byte-long-auth-key")

19
backend/api/get_csrf.go Normal file
View File

@@ -0,0 +1,19 @@
package api
import (
"fmt"
"net/http"
"fr.latosa-escrima/api/core"
"github.com/gorilla/csrf"
)
func HandleCSRF(w http.ResponseWriter, r *http.Request) {
token := csrf.Token(r)
fmt.Println(token)
core.JSONSuccess{
Status: core.Success,
Message: "CSRF generated.",
Data: map[string]string{"csrf": token},
}.Respond(w, http.StatusOK)
}