refactor: use a hook for checking sign in status in the backend

This commit is contained in:
Stavros
2025-01-19 23:00:27 +02:00
parent b8a134ed12
commit d0c1aae1e7
6 changed files with 77 additions and 36 deletions

44
internal/hooks/hooks.go Normal file
View File

@@ -0,0 +1,44 @@
package hooks
import (
"tinyauth/internal/auth"
"tinyauth/internal/types"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
func UseUserContext(c *gin.Context, userList types.UserList) (types.UserContext) {
session := sessions.Default(c)
cookie := session.Get("tinyauth")
if cookie == nil {
return types.UserContext{
Username: "",
IsLoggedIn: false,
}
}
username, ok := cookie.(string)
if !ok {
return types.UserContext{
Username: "",
IsLoggedIn: false,
}
}
user := auth.FindUser(userList, username)
if user == nil {
return types.UserContext{
Username: "",
IsLoggedIn: false,
}
}
return types.UserContext{
Username: username,
IsLoggedIn: true,
}
}