blob: e96f1f93002f09f657fc57538dafb13d790f5207 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package processors
import (
"imageboard/database"
"imageboard/models"
"imageboard/session"
"imageboard/utils/auth"
"github.com/gofiber/fiber/v2"
)
func UserContextProcessor(ctx *fiber.Ctx) error {
var user *models.User
sess, err := session.Store.Get(ctx)
if err == nil {
var userID uint
if id := sess.Get("user_id"); id != nil {
switch v := id.(type) {
case uint:
userID = v
case int:
userID = uint(v)
case int64:
userID = uint(v)
case float64:
userID = uint(v)
}
}
if userID != 0 {
dbUser, err := database.GetUserByID(userID)
if err == nil && dbUser != nil {
dbUser.UpdateLastUserActivity(database.DB)
user = dbUser
}
}
}
ctx.Locals("User", user)
ctx.Locals("IsAuthenticated", user != nil)
ctx.Locals("LogoutURL", auth.GetLogoutURLWithRedirect(ctx))
return ctx.Next()
}
|