Skip to content

Commit

Permalink
fix: expose helper func to store/get user info in/from ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
shaj13 committed Aug 7, 2020
1 parent b0bb6af commit c31102b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions auth/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ type userKey struct{}

// RequestWithUser Save user information in request context.
func RequestWithUser(info Info, r *http.Request) *http.Request {
ctx := context.WithValue(r.Context(), userKey{}, info)
ctx := CtxWithUser(r.Context(), info)
return r.WithContext(ctx)
}

// User return user information from request context.
func User(r *http.Request) Info {
v := r.Context().Value(userKey{})
return UserFromCtx(r.Context())
}

// CtxWithUser Save user information in context.
func CtxWithUser(ctx context.Context, info Info) context.Context {
return context.WithValue(ctx, userKey{}, info)
}

// UserFromCtx return user information from context.
func UserFromCtx(ctx context.Context) Info {
v := ctx.Value(userKey{})
if info, ok := v.(Info); ok {
return info
}
Expand Down

0 comments on commit c31102b

Please sign in to comment.