2023-12-18 23:39:16 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/smtp"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/jordan-wright/email"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
*server.Server
|
|
|
|
|
|
|
|
emailPool *email.Pool
|
|
|
|
emailAddress string
|
2024-02-13 16:57:03 +01:00
|
|
|
baseURL string
|
2023-12-18 23:39:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Mount(srv *server.Server, r chi.Router) {
|
|
|
|
s := &Server{
|
|
|
|
Server: srv,
|
|
|
|
emailAddress: os.Getenv("EMAIL_ADDRESS"), // The address used for sending email
|
2024-02-13 16:57:03 +01:00
|
|
|
baseURL: os.Getenv("BASE_URL"), // The frontend base URL
|
2023-12-18 23:39:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only mount email routes if email is set up
|
|
|
|
if os.Getenv("EMAIL_SMTP_ADDRESS") != "" {
|
|
|
|
// This function will never return an error
|
|
|
|
s.emailPool, _ = email.NewPool(
|
|
|
|
os.Getenv("EMAIL_SMTP_ADDRESS"), // This should be the SMTP server host, like "example.com:587"
|
|
|
|
4, smtp.PlainAuth("",
|
|
|
|
os.Getenv("EMAIL_USERNAME"), // This should be the account name, like "noreply@pronouns.cc"
|
|
|
|
os.Getenv("EMAIL_PASSWORD"), // This should be the account pass, like "6GzW3dY6"
|
|
|
|
os.Getenv("EMAIL_HOST"), // This should be the email host (seems to be SMTP server without the port?)
|
|
|
|
))
|
|
|
|
|
|
|
|
r.Route("/auth/email", func(r chi.Router) {
|
|
|
|
r.With(server.MustAuth).Get("/", server.WrapHandler(s.getEmails)) // List existing email addresses for account
|
|
|
|
r.With(server.MustAuth).Post("/", nil) // Add/update email to existing account, { email }
|
|
|
|
r.With(server.MustAuth).Delete("/{id}", nil) // Remove existing email from account, <no body>
|
|
|
|
|
2024-02-13 16:57:03 +01:00
|
|
|
r.Post("/login", nil) // Log in to account, { username, password }
|
|
|
|
r.Post("/signup", server.WrapHandler(s.postEmailSignup)) // Create account, { email }
|
|
|
|
r.Post("/signup/confirm", server.WrapHandler(s.postEmailSignupConfirm)) // Create account, { ticket, username, password }
|
|
|
|
r.Post("/confirm", nil) // Confirm email address, { ticket }
|
2023-12-18 23:39:16 +01:00
|
|
|
|
|
|
|
r.Patch("/password", nil) // Update password
|
|
|
|
r.Post("/password/forgot", nil) // Forgot/reset password, { email }
|
|
|
|
r.Post("/password/confirm", nil) // Confirm resetting password, { ticket, new_password }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|