commit ada4d78806d8b087f9ede11efb012fd126ababae
parent 70f56d6413723d4ffc56367bb9145492f9ef64c3
Author: MTRNord <mtrnord1@gmail.com>
Date: Mon, 2 Apr 2018 19:51:25 +0200
Add endpoint to check if user is Authorized (used for the settings page. Other endpoints will check on server side)
Diffstat:
2 files changed, 17 insertions(+), 0 deletions(-)
diff --git a/api/login/twitter.go b/api/login/twitter.go
@@ -169,6 +169,22 @@ func LogoutHandler(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/", http.StatusFound)
}
+// IsAuthenticated returns true if the user has a signed session cookie.
+func IsAuthenticated(req *http.Request) bool {
+ if _, err := sessionStore.Get(req, sessionName); err == nil {
+ return true
+ }
+ return false
+}
+
+// IsAuthenticatedHandleFunc returns 200 if the user has a signed session cookie.
+func IsAuthenticatedHandleFunc(w http.ResponseWriter, req *http.Request) {
+ if IsAuthenticated(req) {
+ w.WriteHeader(http.StatusOK)
+ }
+ w.WriteHeader(http.StatusUnauthorized)
+}
+
// 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
@@ -43,6 +43,7 @@ func main() {
li := r.PathPrefix("/login").Subrouter()
li.Handle("/twitter", login.LoginHandler(login.TConfig, nil))
li.Handle("/twitter/callback", tLogin.CallbackHandler(login.TConfig, login.IssueSession(), nil))
+ li.HandleFunc("/twitter/callback", login.IsAuthenticatedHandleFunc)
// cors.Default() setup the middleware with default options being
// all origins accepted with simple methods (GET, POST). See