Auth, Sessions and Middleware #28
Unanswered
bshepherdson
asked this question in
Q&A
Replies: 1 comment
|
Hi Braden! Im terribly sorry to have missed your post until now. Your ideas are very appreciated. Thanks! I have been struggling to design a complete user auth lifecycle and user scoped state that is available in view and actions, But with the latest release of via (large api and untime update) im getting closer and closer of solving this. Planning to have for next release: v.Group(g *via.Group), v.Middleware(ctx *via.Ctx), and a fully working example with user auth bound to a cookie and user scoped state. Cheers! |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Context
I'm porting an early stage Datastar app from Templ and vanilla
net/httpto Via - it's magical! Tons of fiddly wrangling of signals between JSON and Go structs has melted away, and my struggles to create reusable components that could support multiple instances on screen at once have been solved neatly!However, one thing I am struggling with is the lack of pluggable behaviour around the HTTP requests/responses. My original app uses Gorilla sessions to store a blob in a cookie, and use that to persist a bit of per-user state. (Long term, it would probably be storing a session ID and looking up the session in a database, but for now it's a protobuf in a cookie.)
Question
How would you implement authentication, authorization and sessions on top of Via?
My Thoughts
I've got two ideas, and I'm messing around with them locally. I just wanted to make sure I hadn't missed a best practice or example.
Idea 1:
c.MiddlewarehookI'm currently playing around with
func (c *Context) Middleware(mw func(w http.ResponseWriter, r *http.Request) error) { ... }but that feels clumsy. Like you get access to thehttp.Handlerbits but you have to be gentle withwto avoid breaking the rest of thePage.Idea 2: Inverting
PageI also wonder about inverting the way
V.Pageworks, where the user writes thehttp.Handlermore directly, allowing them to do HTTP-level stuff like redirects, 403s, cookies, custom headers, etc. and then calling into Via for the heart of rendering the response.Perhaps
v.Route("/some/path", func(w http.ResponseWriter, r *http.Request) { ... })) does any session-y things with the request, path or query parameters, etc. and then calls the core of what isPage()today as the final(?) step, probably likev.Page(w, func(c *via.Context) { ... }).This has the advantage of moving ad-hoc things like the
GetPathParam()handling out of Via and into user land. It has the serious disadvantage of being a top-level API change, of course.All reactions