all repos — auth-boilerplate @ 5dad190cf2035dafa6b9735c198d7f2e1efdc130

A simple Go web-app boilerplate.

public landing page
Marco Andronaco andronacomarco@gmail.com
Sun, 13 Oct 2024 00:53:34 +0200
commit

5dad190cf2035dafa6b9735c198d7f2e1efdc130

parent

1e0eb8a8122edac2c020fcc78539437884b89358

4 files changed, 20 insertions(+), 6 deletions(-)

jump to
M src/app/handlers.gosrc/app/handlers.go

@@ -5,14 +5,18 @@ "net/http"

"time" ) -func examplePage(w http.ResponseWriter, r *http.Request) { +func getIndexHandler(w http.ResponseWriter, r *http.Request) { + xt.ExecuteTemplate(w, "index.tmpl", nil) +} + +func getProfileHandler(w http.ResponseWriter, r *http.Request) { user, ok := getLoggedUser(r) if !ok { http.Error(w, "Could not find user in context.", http.StatusInternalServerError) return } - xt.ExecuteTemplate(w, "example.tmpl", map[string]interface{}{"User": user}) + xt.ExecuteTemplate(w, "profile.tmpl", map[string]interface{}{"User": user}) } func getRegisterHandler(w http.ResponseWriter, r *http.Request) {

@@ -25,7 +29,7 @@ if err != nil {

xt.ExecuteTemplate(w, "auth-login.tmpl", nil) return } - http.Redirect(w, r, "/", http.StatusFound) + http.Redirect(w, r, "/profile", http.StatusFound) } func getResetPasswordHandler(w http.ResponseWriter, r *http.Request) {

@@ -92,7 +96,7 @@ return

} login(w, user.ID, remember == "on") - http.Redirect(w, r, "/", http.StatusFound) + http.Redirect(w, r, "/login", http.StatusFound) } func logoutHandler(w http.ResponseWriter, r *http.Request) {
M src/app/init.gosrc/app/init.go

@@ -94,7 +94,8 @@ log.Fatal(err)

} // Handle routes - http.HandleFunc("GET /", loginRequired(examplePage)) + http.HandleFunc("GET /", getIndexHandler) + http.HandleFunc("GET /profile", loginRequired(getProfileHandler)) http.HandleFunc("GET /register", getRegisterHandler) http.HandleFunc("GET /login", getLoginHandler) http.HandleFunc("GET /reset-password", getResetPasswordHandler)
M templates/example.tmpltemplates/profile.tmpl

@@ -1,6 +1,6 @@

{{ extends "base.tmpl" }} -{{define "title" -}}Protected page{{end}} +{{define "title" -}}User profile{{end}} {{define "content" -}} <h1>Welcome, <i>{{.User.Username}}</i>!</h1>
A templates/index.tmpl

@@ -0,0 +1,9 @@

+{{ extends "base.tmpl" }} + +{{define "title" -}}Auth boilerplate{{end}} + +{{define "content" -}} + <h1>Auth boilerplate</i>!</h1> + <p>This page is accessible to anyone.</p> + <a href="/profile" class="button">Start</a> +{{end}}