fixed some login and booking bugs

This commit is contained in:
tumillanino
2025-10-28 13:36:05 +11:00
parent dad3ec655f
commit 5d7f0a8eb3
11 changed files with 402 additions and 141 deletions

View File

@@ -1,77 +1,99 @@
package handlers
import (
"database/sql"
"html/template"
"log"
"net/http"
"os"
"strconv"
"decor-by-hannahs/internal/auth"
ory "github.com/ory/client-go"
"decor-by-hannahs/internal/db"
)
type BookingWithService struct {
ID int64
ServiceName string
ServiceDescription string
ServicePriceCents int32
EventDate string
Address string
Notes string
Status string
CreatedAt string
ID int64
ServiceName string
ServiceDescription string
ServicePriceCents int32
ServiceOption string
EventType string
EventDate string
Address string
Notes string
Status string
CreatedAt string
}
type ProfileData struct {
Authenticated bool
Email string
DisplayName string
ID string
ActivePage string
Bookings []BookingWithService
OrySettingsURL string
Authenticated bool
Email string
DisplayName string
ID string
ActivePage string
Bookings []BookingWithService
OrySettingsURL string
OryLoginURL string
}
func ProfileHandler(q *db.Queries, tmpl *template.Template) http.HandlerFunc {
func ProfileHandler(q *db.Queries, tmpl *template.Template, oryClient *ory.APIClient) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
pd := ProfileData{ActivePage: "profile"}
oryAPIURL := getOryAPIURL()
pd := ProfileData{
ActivePage: "profile",
OryLoginURL: "/login",
OrySettingsURL: oryAPIURL + "/ui/settings",
}
user, err := auth.GetUser(r.Context())
if err == nil {
pd.Authenticated = true
pd.Email = user.Email
pd.ID = strconv.FormatInt(user.ID, 10)
pd.DisplayName = user.Email
bookings, err := q.ListBookingsWithServiceByUser(r.Context(), user.ID)
if err != nil {
log.Printf("Error fetching bookings: %v", err)
} else {
for _, b := range bookings {
pd.Bookings = append(pd.Bookings, BookingWithService{
ID: b.ID,
ServiceName: b.ServiceName,
ServiceDescription: nullStringToString(b.ServiceDescription),
ServicePriceCents: b.ServicePriceCents,
EventDate: b.EventDate.Format("January 2, 2006"),
Address: nullStringToString(b.Address),
Notes: nullStringToString(b.Notes),
Status: b.Status,
CreatedAt: b.CreatedAt.Time.Format("Jan 2, 2006"),
cookies := r.Header.Get("Cookie")
session, _, err := oryClient.FrontendAPI.ToSession(r.Context()).Cookie(cookies).Execute()
if err == nil && session != nil && session.Active != nil && *session.Active {
email := getEmailFromSession(session)
if email != "" {
oryID := sql.NullString{String: session.Identity.Id, Valid: true}
user, err := q.GetUserByOryID(r.Context(), oryID)
if err != nil {
user, err = q.CreateUser(r.Context(), db.CreateUserParams{
Email: email,
OryIdentityID: oryID,
})
if err != nil {
log.Printf("Failed to create user: %v", err)
} else {
log.Printf("Created new user: %s (ID: %d)", email, user.ID)
}
}
}
oryAPIURL := os.Getenv("ORY_API_URL")
if oryAPIURL == "" {
tunnelPort := os.Getenv("TUNNEL_PORT")
if tunnelPort == "" {
tunnelPort = "4000"
if err == nil {
pd.Authenticated = true
pd.Email = user.Email
pd.ID = strconv.FormatInt(user.ID, 10)
pd.DisplayName = user.Email
bookings, err := q.ListBookingsWithServiceByUser(r.Context(), sql.NullInt64{Int64: user.ID, Valid: true})
if err != nil {
log.Printf("Error fetching bookings: %v", err)
} else {
for _, b := range bookings {
pd.Bookings = append(pd.Bookings, BookingWithService{
ID: b.ID,
ServiceName: b.ServiceName,
ServiceDescription: nullStringToString(b.ServiceDescription),
ServicePriceCents: b.ServicePriceCents,
ServiceOption: nullStringToString(b.ServiceOption),
EventType: nullStringToString(b.EventType),
EventDate: b.EventDate.Format("January 2, 2006"),
Address: nullStringToString(b.Address),
Notes: nullStringToString(b.Notes),
Status: nullStringToString(b.Status),
CreatedAt: b.CreatedAt.Time.Format("Jan 2, 2006"),
})
}
}
}
oryAPIURL = "http://localhost:" + tunnelPort
}
pd.OrySettingsURL = oryAPIURL + "/ui/settings"
}
if err := tmpl.ExecuteTemplate(w, "profile.tmpl", pd); err != nil {
@@ -80,3 +102,18 @@ func ProfileHandler(q *db.Queries, tmpl *template.Template) http.HandlerFunc {
}
}
}
func getEmailFromSession(session *ory.Session) string {
if session.Identity.Traits == nil {
return ""
}
traits, ok := session.Identity.Traits.(map[string]interface{})
if !ok {
return ""
}
email, ok := traits["email"].(string)
if !ok {
return ""
}
return email
}