Improve user visible status.
[kraftakt.git] / gfitsync.go
index 21f764b..56cb353 100644 (file)
@@ -115,7 +115,18 @@ func indexHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u
                haveToken = true
        }
 
-       fmt.Fprintf(w, "u = %v, tok = %v, haveToken = %v\n", u, tok, haveToken)
+       // fmt.Fprintf(w, "u = %v, tok = %v, haveToken = %v\n", u, tok, haveToken)
+       fmt.Fprintln(w, "<html><body><h1>Fitbit to Google Fit sync</h1>")
+
+       fmt.Fprintf(w, "<p>Hello %s</p>\n", u.Email)
+       fmt.Fprint(w, "<p>Fitbit: ")
+       if haveToken {
+               fmt.Fprint(w, `<strong style="color: DarkGreen;">Authorized</strong>`)
+       } else {
+               fmt.Fprint(w, `<strong style="color: DarkRed;">Not authorized</strong> (<a href="/setup">Authorize</a>)`)
+       }
+       fmt.Fprintln(w, "</p>")
+       fmt.Fprintln(w, "</body></html>")
 
        // TODO(octo): print summary to user
        return nil
@@ -152,8 +163,16 @@ func fitbitGrantHandler(ctx context.Context, w http.ResponseWriter, r *http.Requ
        }
        defer res.Body.Close()
 
+       if res.StatusCode >= 400 {
+               data, _ := ioutil.ReadAll(r.Body)
+               log.Errorf(ctx, "creating subscription failed: status %d %q", res.StatusCode, data)
+               return fmt.Errorf("creating subscription failed")
+       }
+
        redirectURL := r.URL
        redirectURL.Path = "/"
+       redirectURL.RawQuery = ""
+       redirectURL.Fragment = ""
        http.Redirect(w, r, redirectURL.String(), http.StatusTemporaryRedirect)
        return nil
 }