aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/session/context.go
blob: 60f88b4f46075e728bcd388d7c76cf7229d649fd (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
package session

import (
	"context"

	"albatross-2026-backend/db"
)

type sessionIDContextKey struct{}
type userContextKey struct{}
type clientIPContextKey struct{}

func GetSessionIDFromContext(ctx context.Context) (string, bool) {
	sessionID, ok := ctx.Value(sessionIDContextKey{}).(string)
	return sessionID, ok
}

func SetSessionIDInContext(ctx context.Context, sessionID string) context.Context {
	return context.WithValue(ctx, sessionIDContextKey{}, sessionID)
}

func GetUserFromContext(ctx context.Context) (*db.User, bool) {
	user, ok := ctx.Value(userContextKey{}).(*db.User)
	return user, ok
}

// SetUserInContext sets a user in the context. Intended for testing.
func SetUserInContext(ctx context.Context, user *db.User) context.Context {
	return context.WithValue(ctx, userContextKey{}, user)
}

func GetClientIPFromContext(ctx context.Context) string {
	ip, _ := ctx.Value(clientIPContextKey{}).(string)
	return ip
}

func SetClientIPInContext(ctx context.Context, ip string) context.Context {
	return context.WithValue(ctx, clientIPContextKey{}, ip)
}