diff options
| author | adnano <[email protected]> | 2020-09-28 02:05:37 -0400 |
|---|---|---|
| committer | adnano <[email protected]> | 2020-09-28 02:05:37 -0400 |
| commit | 70c5d8b9cea707b20a9f5aa1b0888776326d55be (patch) | |
| tree | 6636c23265f8762f438c55753a40d31f44c836fc /examples | |
| parent | Use WithCertificate helper in auth example (diff) | |
| download | go-gemini-70c5d8b9cea707b20a9f5aa1b0888776326d55be.tar.xz go-gemini-70c5d8b9cea707b20a9f5aa1b0888776326d55be.zip | |
Add WithInput helper functions
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/auth/auth.go | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/examples/auth/auth.go b/examples/auth/auth.go index c348648..c126de8 100644 --- a/examples/auth/auth.go +++ b/examples/auth/auth.go @@ -46,11 +46,11 @@ func main() { handler := &gmi.ServeMux{} handler.HandleFunc("/", welcome) - handler.HandleFunc("/login", login) - handler.HandleFunc("/login/password", loginPassword) - handler.HandleFunc("/profile", profile) - handler.HandleFunc("/admin", admin) - handler.HandleFunc("/logout", logout) + handler.HandleFunc("/login/", login) + handler.HandleFunc("/login/password/", loginPassword) + handler.HandleFunc("/profile/", profile) + handler.HandleFunc("/admin/", admin) + handler.HandleFunc("/logout/", logout) server := &gmi.Server{ Certificate: cert, @@ -74,15 +74,13 @@ func welcome(rw *gmi.ResponseWriter, req *gmi.Request) { func login(rw *gmi.ResponseWriter, req *gmi.Request) { gmi.WithCertificate(rw, req, func(cert *x509.Certificate) { - if username := req.URL.RawQuery; username == "" { - gmi.Input(rw, req, "Username") - } else { + gmi.WithInput(rw, req, "Username", func(username string) { fingerprint := gmi.Fingerprint(cert) sessions[fingerprint] = &session{ username: username, } gmi.Redirect(rw, req, "/login/password") - } + }) }) } @@ -94,9 +92,7 @@ func loginPassword(rw *gmi.ResponseWriter, req *gmi.Request) { return } - if password := req.URL.RawQuery; password == "" { - gmi.SensitiveInput(rw, req, "Password") - } else { + gmi.WithSensitiveInput(rw, req, "Password", func(password string) { expected := logins[session.username].password if password == expected { session.authorized = true @@ -104,7 +100,7 @@ func loginPassword(rw *gmi.ResponseWriter, req *gmi.Request) { } else { gmi.SensitiveInput(rw, req, "Wrong password. Try again") } - } + }) }) } |