Closed namatoj closed 7 months ago
I think we should start flat and add more folders and re-group as we go and learn more about what we are building. We could set it up like this
creates the pocketbase app and responsible for grouping and registering handlers register handlers, e.g
func App() {
app := pocketbase.New()
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.Use(auth.LoadAuthContextFromCookie(app))
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public"), false))
e.Router.POST("/login/", auth.LoginHandler)
e.Router.GET("/logout/", auth.LogoutHandler)
e.Router.GET("/", start.viewHandler)
})
return app
}
have handler funcs for login, logout, cookie management e.g
func LogoutHandler(c echo.Context) error {
c.SetCookie(&http.Cookie{
Name: "token",
Value: "",
Path: "/",
MaxAge: -1,
Secure: true,
HttpOnly: true,
})
return c.HTML(http.StatusOK, "logged out")
}
imports the app and starts it. Might do some additional flag / config parsing in the future.
This video gives some great tips on code structure https://www.youtube.com/watch?v=oL6JBUk6tj0
Also by breaking out the handlers we can setup auth_test.go and write some simple "given x is in the context the handler should do y" tests.
This video gives some great tips on code structure https://www.youtube.com/watch?v=oL6JBUk6tj0
That was inspiring! Got me excited about the hexagonal architecture.
Also by breaking out the handlers we can setup auth_test.go and write some simple "given x is in the context the handler should do y" tests.
Agree! I'll break out the handlers to somewhere in pkg
. To be continued!
Something a bit like this maybe?
I had some troubles with some of the details in your suggestion. auth.LoginHandler
suggest that auth
is in another package (right?). But the directory structure that auth.go
should be in the same directory as app.go
which didn't vibe with the package
keyword on the top row.
This lead me to take the easy way of putting auth.go
and app.go
in the web
package. Let me know if I misunderstood anything or if you have more feedback on how to improve this.
Added functionality to use the JWT in a session cookie as mentioned here
Added the following routes:
If sending a POST request to
/login/
containing the form-data valuesemail
andpassword
while it is served using https corresponding to a user in the user collection the cookietoken
will be set.@simondmansson I'm a bit unsure about how we want to structure the routes and the middleware function. It quite fast became rather messy. :-) I'm open for suggestions. Othewise I will leave this PR for a bit and come back when I have more inspiration for structure.