From 7d066a8f112c391cb4b820448c4be6296595452a Mon Sep 17 00:00:00 2001 From: MrBounty Date: Thu, 8 Aug 2024 17:53:54 +0200 Subject: [PATCH] Added a SendNoreplyEmail function --- Authentification.go | 45 +++++++++++++++++++++++++++++++++------------ views/error.html | 1 + 2 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 views/error.html diff --git a/Authentification.go b/Authentification.go index 0849bd2..5eb3a2a 100644 --- a/Authentification.go +++ b/Authentification.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "net/http" + "net/smtp" "os" "github.com/edgedb/edgedb-go" @@ -156,10 +157,14 @@ func handleUiSignIn(c *fiber.Ctx) error { Secure: true, }) - return c.Redirect(fmt.Sprintf("%s/ui/signup?challenge=%s", os.Getenv("EDGEDB_AUTH_BASE_URL"), challenge), fiber.StatusTemporaryRedirect) + return c.Redirect(fmt.Sprintf("%s/ui/signin?challenge=%s", os.Getenv("EDGEDB_AUTH_BASE_URL"), challenge), fiber.StatusTemporaryRedirect) } func handleCallbackSignup(c *fiber.Ctx) error { + if c.Query("verification_email_sent_at") != "" { + return c.Redirect("/") + } + code := c.Query("code") if code == "" { err := c.Query("error") @@ -169,7 +174,7 @@ func handleCallbackSignup(c *fiber.Ctx) error { verifier := c.Cookies("jade-edgedb-pkce-verifier", "") if verifier == "" { - panic("Could not find 'verifier' in the cookie store. Is this the same user agent/browser that started the authorization flow?") + return c.SendString("Could not find 'verifier' in the cookie store. Is this the same user agent/browser that started the authorization flow?") } codeExchangeURL := fmt.Sprintf("%s/token?code=%s&verifier=%s", os.Getenv("EDGEDB_AUTH_BASE_URL"), code, verifier) @@ -269,35 +274,30 @@ func handleCallbackSignup(c *fiber.Ctx) error { func handleCallback(c *fiber.Ctx) error { code := c.Query("code") if code == "" { - err := c.Query("error") fmt.Println("OAuth callback is missing 'code'. OAuth provider responded with error") - panic(err) + return c.Render("error", fiber.Map{"Text": "Error: OAuth provider responded with an error. Please contact the support or try later."}, "layouts/main") } verifier := c.Cookies("jade-edgedb-pkce-verifier", "") if verifier == "" { - panic("Could not find 'verifier' in the cookie store. Is this the same user agent/browser that started the authorization flow?") + return c.Render("error", fiber.Map{"Text": "Error: No verifier cookie found. Please make sure to use the same devide and browser when login for the first time."}, "layouts/main") } codeExchangeURL := fmt.Sprintf("%s/token?code=%s&verifier=%s", os.Getenv("EDGEDB_AUTH_BASE_URL"), code, verifier) resp, err := http.Get(codeExchangeURL) if err != nil { - fmt.Println("Error exchanging code for access token") - panic(err) + return c.Render("error", fiber.Map{"Text": "Internal JADE error code "}, "layouts/main") } defer resp.Body.Close() if resp.StatusCode != fiber.StatusOK { - body, _ := io.ReadAll(resp.Body) - fmt.Println("Error exchanging code for access token") - panic(string(body)) + return c.Render("error", fiber.Map{"Text": "Hello"}, "layouts/main") } var tokenResponse TokenResponse err = json.NewDecoder(resp.Body).Decode(&tokenResponse) if err != nil { - fmt.Println("Error decoding auth server response") - panic(err) + return c.Render("error", fiber.Map{"Text": "Hello"}, "layouts/main") } c.Cookie(&fiber.Cookie{ @@ -316,3 +316,24 @@ func handleSignOut(c *fiber.Ctx) error { c.ClearCookie("jade-edgedb-auth-token") return c.Redirect("/", fiber.StatusTemporaryRedirect) } + +func handleEmailVerification(c *fiber.Ctx) error { + return c.Render("error", fiber.Map{"Text": "Hello"}, "layouts/main") +} + +func SendNoreplyEmail(to string, subject string, content string) { + auth := smtp.PlainAuth("", "noreply@bouvai.com", os.Getenv("NOREPLY_APP_PASSWORD"), "smtp.gmail.com") + + msg := []byte("To: " + to + "\r\n" + + + "Subject: " + subject + "\r\n" + + + "\r\n" + + + content + "\r\n") + + err := smtp.SendMail("smtp.gmail.com:587", auth, "noreply@bouvai.com", []string{to}, msg) + if err != nil { + panic(err) + } +} diff --git a/views/error.html b/views/error.html new file mode 100644 index 0000000..fc2f32c --- /dev/null +++ b/views/error.html @@ -0,0 +1 @@ +{{ Text }}