/*
* Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
package middlewares
import (
"git.src.quest/~liljamo/emerwen-web/internal/auth"
"git.src.quest/~liljamo/emerwen-web/internal/consts"
"github.com/alexedwards/scs/v2"
"github.com/gin-gonic/gin"
)
// CheckForSession returns a gin middleware for checking if a session exists
// and setting context values if one exists.
func CheckForSession(sm *scs.SessionManager) gin.HandlerFunc {
return func(c *gin.Context) {
if claims, ok := sm.Get(c.Request.Context(), "claims").(auth.Claims); ok {
c.Set(consts.KeyLoggedIn, true)
if claims.Name != "" {
c.Set(consts.KeyUserName, claims.Name)
} else {
c.Set(consts.KeyUserName, claims.Email)
}
c.Next()
} else {
c.Set(consts.KeyLoggedIn, false)
c.Next()
}
}
}