feat(router): show landing page if user is not logged in

This commit is contained in:
Guz
2025-06-09 19:18:29 -03:00
parent 8403459cc8
commit f4a971bdae
2 changed files with 22 additions and 1 deletions

View File

@@ -94,7 +94,22 @@ func (router *router) setup() http.Handler {
r.Handle("/assets/", http.StripPrefix("/assets/", http.FileServerFS(router.assets)))
r.HandleFunc("/dashboard/", router.dashboard)
r.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
// TODO: Add a way to the user to bypass this check and see the landing page.
// Probably a query parameter to bypass like "?landing=true"
if userController.isLogged(r) {
err := router.templates.ExecuteTemplate(w, "dashboard", nil)
if err != nil {
exception.InternalServerError(err).ServeHTTP(w, r)
}
return
}
err := router.templates.ExecuteTemplate(w, "landing", nil)
if err != nil {
exception.InternalServerError(err).ServeHTTP(w, r)
}
})
r.HandleFunc("/login/{$}", userController.login)
r.HandleFunc("/register/{$}", userController.register)

View File

@@ -128,3 +128,9 @@ func (c userController) register(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (ctrl userController) isLogged(r *http.Request) bool {
// TODO: Check if token in valid (depends on token service being implemented)
cs := r.CookiesNamed("token")
return len(cs) > 0
}