Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting user context #48

Closed
zikwall opened this issue Sep 24, 2019 · 1 comment
Closed

Setting user context #48

zikwall opened this issue Sep 24, 2019 · 1 comment

Comments

@zikwall
Copy link

zikwall commented Sep 24, 2019

You have jwt intermediate in the examples, but you set everything there in cookies. The question is, how will it be correctly installed in the context and then received from there?

Here, for example, looks like regular net/http:

var ExampleJWTNet = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		//...
		// find by JWT token

		user := &User{100, false} // for example

		ctx := context.WithValue(r.Context(), "user", user)
		r = r.WithContext(ctx)
		next.ServeHTTP(w, r)
	})
}

and further it was possible to get this way:

func(w http.ResponseWriter, r *http.Request) Example {
    user := r.Context().Value("user")
}

I understand correctly that you need to use UserValue?

func JWTAuthorization(ctx *atreugo.RequestCtx) (int, error) {
	logger.Info("JWT Authorization")

	if authenticate(ctx) {
		logger.Info("User successfully Logged In")
                ctx.SetUserValue("key", "VALUE USER")
	}

	return 200, nil
}
@savsgio
Copy link
Owner

savsgio commented Sep 25, 2019

Yes, you could get information about jwt in the middleware and save in the request with ctx.SetUserValue("key", "VALUE USER") and get it later with ctx.GetUserValue("key")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants