-
|
Hi, I'm really liking this library, and it's working quite nicely for me. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
I think your reading is correct: The relevant flow is:
So the transformer does not mutate the stored session/user, and its extra fields will not appear later from The cleanest workaround is to move the permission-loading logic into a shared helper, then call that helper from both places: func LoadUserPermissions(ctx context.Context, db *sql.DB, userID any) ([]Permission, error) {
// your existing query here
}Then use it in the session transformer for the func PermissionsMiddleware(auth *limen.Limen, db *sql.DB) limen.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := auth.GetSession(r)
if err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
perms, err := LoadUserPermissions(r.Context(), db, session.User.ID)
if err != nil {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
ctx := context.WithValue(r.Context(), permissionsKey{}, perms)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}If the route is already protected by Limen's So short version: use the transformer for shaping the auth response; use a separate enrichment middleware/helper for authorization data needed during request handling. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @krotname this is exactly right, really appreciate the clear breakdown 🙏
|
Beta Was this translation helpful? Give feedback.
I think your reading is correct:
WithHTTPSessionTransformer()is only on the HTTP response path, not on the session validation path.The relevant flow is:
WithHTTPSessionTransformer()storessessionTransformerin the HTTP config.Responder.SessionResponse()calls it before writing the auth/session JSON response.auth.GetSession(r)just callsSessionManager.ValidateSession(...)and returns*ValidatedSession.MiddlewareRequireSession()also callsauthInstance.GetSession(r)and then stores onlyUserandSessionin request context.So the transformer does not mutate the stored session/user, and its extra fields will not appear later from
auth.GetSession()orGetCurrentSessionFromCtx().The …