(FIX): More auth issues solved.

If a user is logged into an account that cannot be found, they will be
silently logged out, so they can log back into their account. In the
event that the data becomes stale.
This commit is contained in:
Hayden Hargreaves 2025-07-27 13:00:03 -07:00
parent a28f8edd54
commit dbf8470195
2 changed files with 17 additions and 15 deletions

View File

@ -36,18 +36,7 @@ func GoogleCallback(ctx *gin.Context) {
if jwt, dbUser, googleUserInfo, err := deps.AuthService.GoogleAuthSuccess(state, code); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
// TODO: Update these values when using a real domain. Maybe an ENV?
domain.SetCookie(ctx, "jwt_token", jwt, time.Hour*24*7)
// ctx.SetCookie(
// "jwt_token",
// jwt,
// int(time.Now().Add(7*24*time.Hour).Sub(time.Now()).Seconds()),
// "/",
// "", // TODO: Real live domain
// false, // TODO: True in prod
// true,
// )
// ctx.JSON(http.StatusOK, gin.H{"jwt": jwt, "googleUserInfo": googleUserInfo, "dbUser": dbUser})
_ = dbUser
_ = googleUserInfo
@ -60,11 +49,7 @@ func GoogleCallback(ctx *gin.Context) {
// require authentication will require the user to sign back in before accessing them again.
// This route will direct the user back to the home page.
func Logout(ctx *gin.Context) {
// TODO: Use same values as the GoogleCallback function
domain.SetCookie(ctx, "jwt_token", "", -1)
domain.SetCookie(ctx, "search-filters", "", -1)
// ctx.SetCookie("jwt_token", "", -1, "/", "", false, true) // TODO: Update settings
// ctx.SetCookie("search-filters", "", -1, "/", "", false, true)
ctx.Redirect(http.StatusSeeOther, domain.WEB_HOME)
}

View File

@ -28,6 +28,14 @@ func HomePage(ctx *gin.Context) {
loggedIn := domain.IsLoggedIn(ctx)
// Ensure user is logged in with a valid account
if user := deps.UserService.GetAuthenicatedUser(ctx); user == nil {
// Log (stale) user out
domain.SetCookie(ctx, "jwt_token", "", -1)
domain.SetCookie(ctx, "search-filters", "", -1)
loggedIn = false
}
var page templ.Component
if loggedIn {
userId := ctx.MustGet("userId").(int)
@ -214,6 +222,15 @@ func RecipePage(ctx *gin.Context) {
// Get signed in user, if they exist
var userId *int = nil
var loggedIn = domainServer.IsLoggedIn(ctx)
// Ensure user is logged in with a valid account
if user := deps.UserService.GetAuthenicatedUser(ctx); user == nil {
// Log (stale) user out
domain.SetCookie(ctx, "jwt_token", "", -1)
domain.SetCookie(ctx, "search-filters", "", -1)
loggedIn = false
}
if loggedIn {
storeId := ctx.MustGet("userId").(int)
userId = &storeId