commit e034837e000e38d04d23811a1924a458f549d67d
parent 1e0127d3e9ccab1bdc06feac1590231b0a2040fb
Author: MTRNord <mtrnord1@gmail.com>
Date: Fri, 30 Mar 2018 22:13:55 +0200
Implement Logout Handler
Diffstat:
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/api/login/twitter.go b/api/login/twitter.go
@@ -40,6 +40,14 @@ func IssueSession() http.Handler {
return http.HandlerFunc(fn)
}
+// LogoutHandler destroys the session on POSTs and redirects to home.
+func LogoutHandler(w http.ResponseWriter, req *http.Request) {
+ if req.Method == "POST" {
+ sessionStore.Destroy(w, sessionName)
+ }
+ http.Redirect(w, req, "/", http.StatusFound)
+}
+
// LoginHandler handles Twitter login requests by obtaining a request token and
// redirecting to the authorization URL.
func LoginHandler(config *oauth1.Config, failure http.Handler) http.Handler {
diff --git a/main.go b/main.go
@@ -38,9 +38,12 @@ func main() {
p := r.PathPrefix("/paper/{uuid}").Subrouter()
p.HandleFunc("/yesterday", web_api.Yesterday).Methods("GET")
- l := r.PathPrefix("/login").Subrouter()
- l.Handle("/twitter", login.LoginHandler(login.TConfig, nil))
- l.Handle("/twitter/callback", tLogin.CallbackHandler(login.TConfig, login.IssueSession(), nil))
+ lo := r.PathPrefix("/logout").Subrouter()
+ lo.HandleFunc("/twitter", login.LogoutHandler)
+
+ li := r.PathPrefix("/login").Subrouter()
+ li.Handle("/twitter", login.LoginHandler(login.TConfig, nil))
+ li.Handle("/twitter/callback", tLogin.CallbackHandler(login.TConfig, login.IssueSession(), nil))
// cors.Default() setup the middleware with default options being
// all origins accepted with simple methods (GET, POST). See