mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 12:45:47 +00:00
refactor: use a hook for checking sign in status in the backend
This commit is contained in:
44
internal/hooks/hooks.go
Normal file
44
internal/hooks/hooks.go
Normal 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user