package app import ( "net/http" "golang.org/x/text/language" "dev.tandem.ws/tandem/camper/pkg/auth" "dev.tandem.ws/tandem/camper/pkg/database" "dev.tandem.ws/tandem/camper/pkg/locale" ) func (h *App) getUser(r *http.Request, conn *database.Conn) (*auth.User, error) { cookie := auth.GetSessionCookie(r) if _, err := conn.Exec(r.Context(), "select set_cookie($1)", cookie); err != nil { return nil, err } user := &auth.User{ Email: "", LoggedIn: false, Role: "guest", } row := conn.QueryRow(r.Context(), "select coalesce(email, ''), email is not null, role, lang_tag, csrf_token from user_profile") var langTag string if err := row.Scan(&user.Email, &user.LoggedIn, &user.Role, &langTag, &user.CSRFToken); err != nil { return nil, err } if lang, err := language.Parse(langTag); err == nil { user.Language = lang } else { return nil, err } user.Locale = h.locales[user.Language] if user.Locale == nil { user.Locale = h.matchLocale(r) } return user, nil } func (h *App) matchLocale(r *http.Request) *locale.Locale { l := locale.Match(r.Header.Get("Accept-Language"), h.locales, h.languageMatcher) if l == nil { l = h.defaultLocale } return l } func profileHandler(user *auth.User, conn *database.Conn) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var head string head, r.URL.Path = shiftPath(r.URL.Path) switch head { case "session": switch r.Method { case http.MethodDelete: handleLogout(w, r, user, conn) default: methodNotAllowed(w, r, http.MethodDelete) } default: http.NotFound(w, r) } } }