55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"threadr/models"
|
|
"github.com/gorilla/sessions"
|
|
)
|
|
|
|
func ProfileHandler(app *App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session := r.Context().Value("session").(*sessions.Session)
|
|
userID, ok := session.Values["user_id"].(int)
|
|
if !ok {
|
|
http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound)
|
|
return
|
|
}
|
|
user, err := models.GetUserByID(app.DB, userID)
|
|
if err != nil {
|
|
log.Printf("Error fetching user in ProfileHandler: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if user == nil {
|
|
http.Error(w, "User not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
displayName := user.DisplayName
|
|
if displayName == "" {
|
|
displayName = user.Username
|
|
}
|
|
data := struct {
|
|
PageData
|
|
User models.User
|
|
DisplayName string
|
|
}{
|
|
PageData: PageData{
|
|
Title: "ThreadR - Profile",
|
|
Navbar: "profile",
|
|
LoggedIn: true,
|
|
ShowCookieBanner: false,
|
|
BasePath: app.Config.ThreadrDir,
|
|
StaticPath: app.Config.ThreadrDir + "/static",
|
|
CurrentURL: r.URL.Path,
|
|
},
|
|
User: *user,
|
|
DisplayName: displayName,
|
|
}
|
|
if err := app.Tmpl.ExecuteTemplate(w, "profile", data); err != nil {
|
|
log.Printf("Error executing template in ProfileHandler: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
} |