diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2024-10-03 20:52:36 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2024-10-03 20:52:36 +0200 |
commit | 3234c5ec777117d429bdc04dcf10c30094079e57 (patch) | |
tree | 32d1477947bea185151b4a60cbfcc88bc8262492 /auth.go | |
parent | bf3fa3171a33d417d261e6543da704bbc0a9dee3 (diff) | |
download | gosten-3234c5ec777117d429bdc04dcf10c30094079e57.tar.gz gosten-3234c5ec777117d429bdc04dcf10c30094079e57.tar.bz2 gosten-3234c5ec777117d429bdc04dcf10c30094079e57.zip |
Use unique type instead of string key
Diffstat (limited to '')
-rw-r--r-- | auth.go | 7 |
1 files changed, 4 insertions, 3 deletions
@@ -11,8 +11,9 @@ import ( "golang.org/x/crypto/bcrypt" ) +type userContextKey struct{} + const ( - userContextKey = "_user" sessionDuration = 86400 * 7 // 7 days loginQueryMarker = "next" ) @@ -25,7 +26,7 @@ func RequireAuth(next http.Handler) http.Handler { u, err := Q.GetUserById(r.Context(), s.UserID) if err == nil { // authenticated --> done - ctx := context.WithValue(r.Context(), userContextKey, u.ID) + ctx := context.WithValue(r.Context(), userContextKey{}, u.ID) next.ServeHTTP(w, r.WithContext(ctx)) return } @@ -125,5 +126,5 @@ func loginPage() http.HandlerFunc { } func userId(r *http.Request) int32 { - return r.Context().Value(userContextKey).(int32) + return r.Context().Value(userContextKey{}).(int32) } |