feat(auth): #154 send verification mails
This commit is contained in:
12
utils/env.go
12
utils/env.go
@@ -14,6 +14,7 @@ var (
|
||||
SmtpFromMail string
|
||||
SmtpFromName string
|
||||
BaseUrl string
|
||||
Environment string
|
||||
)
|
||||
|
||||
func MustInitEnv() {
|
||||
@@ -24,6 +25,7 @@ func MustInitEnv() {
|
||||
SmtpFromMail = os.Getenv("SMTP_FROM_MAIL")
|
||||
SmtpFromName = os.Getenv("SMTP_FROM_NAME")
|
||||
BaseUrl = os.Getenv("BASE_URL")
|
||||
Environment = os.Getenv("ENVIRONMENT")
|
||||
|
||||
if SmtpHost == "" {
|
||||
log.Fatal("SMTP_HOST must be set")
|
||||
@@ -46,12 +48,10 @@ func MustInitEnv() {
|
||||
if BaseUrl == "" {
|
||||
log.Fatal("BASE_URL must be set")
|
||||
}
|
||||
if Environment == "" {
|
||||
log.Fatal("ENVIRONMENT must be set")
|
||||
}
|
||||
|
||||
slog.Info("BASE_URL is " + BaseUrl)
|
||||
slog.Info("SMTP_HOST is " + SmtpHost)
|
||||
slog.Info("SMTP_PORT is " + SmtpPort)
|
||||
slog.Info("SMTP_USER is " + SmtpUser)
|
||||
slog.Info("SMTP_PASS is " + SmtpPass)
|
||||
slog.Info("SMTP_FROM_MAIL is " + SmtpFromMail)
|
||||
slog.Info("SMTP_FROM_NAME is " + SmtpFromName)
|
||||
slog.Info("ENVIRONMENT is " + Environment)
|
||||
}
|
||||
|
||||
72
utils/http.go
Normal file
72
utils/http.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log/slog"
|
||||
"me-fit/types"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ContextKey string
|
||||
|
||||
const (
|
||||
ContextKeyUser ContextKey = "user_id"
|
||||
)
|
||||
|
||||
func DoRedirect(w http.ResponseWriter, r *http.Request, url string) {
|
||||
isHtmx := r.Header.Get("HX-Request") == "true"
|
||||
if isHtmx {
|
||||
w.Header().Add("HX-Redirect", url)
|
||||
} else {
|
||||
http.Redirect(w, r, url, http.StatusSeeOther)
|
||||
}
|
||||
}
|
||||
|
||||
func GetUser(r *http.Request) *types.User {
|
||||
user := r.Context().Value(ContextKeyUser)
|
||||
if user != nil {
|
||||
return user.(*types.User)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserFromSession(db *sql.DB, r *http.Request) *types.User {
|
||||
sessionId := getSessionID(r)
|
||||
if sessionId == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var user types.User
|
||||
var createdAt time.Time
|
||||
|
||||
user.SessionId = sessionId
|
||||
|
||||
err := db.QueryRow(`
|
||||
SELECT u.user_uuid, u.email, u.email_verified, s.created_at
|
||||
FROM session s
|
||||
INNER JOIN user u ON s.user_uuid = u.user_uuid
|
||||
WHERE session_id = ?`, sessionId).Scan(&user.Id, &user.Email, &user.EmailVerified, &createdAt)
|
||||
if err != nil {
|
||||
slog.Warn("Could not verify session: " + err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
if createdAt.Add(time.Duration(8 * time.Hour)).Before(time.Now()) {
|
||||
user.SessionValid = false
|
||||
} else {
|
||||
user.SessionValid = true
|
||||
}
|
||||
|
||||
return &user
|
||||
}
|
||||
|
||||
func getSessionID(r *http.Request) string {
|
||||
for _, c := range r.Cookies() {
|
||||
if c.Name == "id" {
|
||||
return c.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -1,19 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
func SendWelcomeMail(to string) {
|
||||
func SendMail(to string, subject string, message string) error {
|
||||
|
||||
auth := smtp.PlainAuth("", SmtpUser, SmtpPass, SmtpHost)
|
||||
|
||||
msg := "From: " + SmtpFromName + " <" + SmtpFromMail + ">\nTo: " + to + "\nSubject: Welcome to me-fit\n\nWelcome to me-fit!"
|
||||
|
||||
err := smtp.SendMail(SmtpHost+":"+SmtpPort, auth, SmtpFromMail, []string{to}, []byte(msg))
|
||||
if err != nil {
|
||||
slog.Error("Could not send mail: " + err.Error())
|
||||
}
|
||||
msg := fmt.Sprintf("From: %v <%v>\nTo: %v\nSubject: %v\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n%v", SmtpFromName, SmtpFromMail, to, subject, message)
|
||||
|
||||
return smtp.SendMail(SmtpHost+":"+SmtpPort, auth, SmtpFromMail, []string{to}, []byte(msg))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user