From a28f8edd5408508e1d6032200b0ca7d63b81ebb7 Mon Sep 17 00:00:00 2001 From: Hayden Hargreaves Date: Sun, 27 Jul 2025 12:47:25 -0700 Subject: [PATCH] (FIX): Fixed the auth issue, at least on the profile page. --- internal/app/handlers/page_handler.go | 8 +++++++- internal/app/service/user_service.go | 8 ++++---- internal/domain/user/service.go | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/internal/app/handlers/page_handler.go b/internal/app/handlers/page_handler.go index 53d2ac5..4227f46 100755 --- a/internal/app/handlers/page_handler.go +++ b/internal/app/handlers/page_handler.go @@ -150,6 +150,12 @@ func ProfilePage(ctx *gin.Context) { // Else, get the user data deps := ctx.MustGet("deps").(*domainServer.InjectedDependencies) user := deps.UserService.GetAuthenicatedUser(ctx) + if user == nil { + // User is failing to be found, direct to the login page + ctx.Redirect(http.StatusSeeOther, domainServer.WEB_LOGIN) + return + } + recipes, err := deps.RecipeService.GetUserRecipes(user.Id) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{ @@ -179,7 +185,7 @@ func ProfilePage(ctx *gin.Context) { } title := "Potion - Profile" - page := pages.ProfilePage(user, recipes, favorites, engagements) + page := pages.ProfilePage(*user, recipes, favorites, engagements) ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page)) } diff --git a/internal/app/service/user_service.go b/internal/app/service/user_service.go index 96b257d..f49b93e 100644 --- a/internal/app/service/user_service.go +++ b/internal/app/service/user_service.go @@ -25,19 +25,19 @@ func NewUserService(userRepository domain.UserRepository) domain.UserService { // user is actually logged in, if not, a blank user will be returned. To ensure success, call the // `domain.IsLoggedIn()` function first to ensure the user is logged in. If that passes, this // function should yield a result. -func (s *UserService) GetAuthenicatedUser(ctx *gin.Context) domain.User { +func (s *UserService) GetAuthenicatedUser(ctx *gin.Context) *domain.User { val, ok := ctx.Get("userId") if !ok { - return domain.User{} + return nil } id := val.(int) user, err := s.userRepository.GetUser(id) if err != nil { - return domain.User{} + return nil } - return *user + return user } // GetUser will get a user from the database via its ID. This is not related to the Google ID in diff --git a/internal/domain/user/service.go b/internal/domain/user/service.go index 16a7629..ddf835c 100644 --- a/internal/domain/user/service.go +++ b/internal/domain/user/service.go @@ -3,6 +3,6 @@ package domain import "github.com/gin-gonic/gin" type UserService interface { - GetAuthenicatedUser(ctx *gin.Context) User + GetAuthenicatedUser(ctx *gin.Context) *User GetUser(id int) (*User, error) }