Added a SendNoreplyEmail function

This commit is contained in:
Adrien Bouvais 2024-08-08 17:53:54 +02:00
parent c6608fbea6
commit 7d066a8f11
2 changed files with 34 additions and 12 deletions

View File

@ -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)
}
}

1
views/error.html Normal file
View File

@ -0,0 +1 @@
{{ Text }}