Connect handlers: don't call {fitbit,gfit}.NewClient().
authorFlorian Forster <ff@octo.it>
Wed, 31 Jan 2018 06:59:24 +0000 (07:59 +0100)
committerFlorian Forster <ff@octo.it>
Wed, 31 Jan 2018 06:59:24 +0000 (07:59 +0100)
Those constructors try to load the OAuth token from datastore, which
doesn't exist yet when these handlers are called.

fitbit/fitbit.go
gfit/gfit.go
kraftakt.go

index e960438..6f15b7c 100644 (file)
@@ -33,6 +33,10 @@ func oauthConfig() *oauth2.Config {
        }
 }
 
+func AuthURL(ctx context.Context, u *app.User) string {
+       return oauthConfig().AuthCodeURL(u.Sign("Fitbit"), oauth2.AccessTypeOffline)
+}
+
 func ParseToken(ctx context.Context, r *http.Request, u *app.User) error {
        if state := r.FormValue("state"); state != u.Sign("Fitbit") {
                return fmt.Errorf("invalid state parameter: %q", state)
@@ -150,10 +154,6 @@ func NewClient(ctx context.Context, fitbitUserID string, u *app.User) (*Client,
        }, nil
 }
 
-func (c *Client) AuthURL(ctx context.Context) string {
-       return oauthConfig().AuthCodeURL(c.appUser.Sign("Fitbit"), oauth2.AccessTypeOffline)
-}
-
 func (c *Client) ActivitySummary(ctx context.Context, date string) (*ActivitySummary, error) {
        url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/activities/date/%s.json",
                c.fitbitUserID, date)
index 858f94d..ec88b19 100644 (file)
@@ -42,6 +42,10 @@ func oauthConfig() *oauth2.Config {
        }
 }
 
+func AuthURL(ctx context.Context, u *app.User) string {
+       return oauthConfig().AuthCodeURL(u.Sign("Google"), oauth2.AccessTypeOffline)
+}
+
 func Application(ctx context.Context) *fitness.Application {
        return &fitness.Application{
                Name:       "Kraftakt",
@@ -85,10 +89,6 @@ func NewClient(ctx context.Context, u *app.User) (*Client, error) {
        }, nil
 }
 
-func (c *Client) AuthURL(ctx context.Context) string {
-       return oauthConfig().AuthCodeURL(c.appUser.Sign("Google"), oauth2.AccessTypeOffline)
-}
-
 func (c *Client) DeleteToken(ctx context.Context) error {
        return c.appUser.DeleteToken(ctx, "Google")
 }
index 092cad3..8d56299 100644 (file)
@@ -137,12 +137,7 @@ func loginHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ *
 }
 
 func fitbitConnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
-       c, err := fitbit.NewClient(ctx, "", u)
-       if err != nil {
-               return err
-       }
-
-       http.Redirect(w, r, c.AuthURL(ctx), http.StatusTemporaryRedirect)
+       http.Redirect(w, r, fitbit.AuthURL(ctx, u), http.StatusTemporaryRedirect)
        return nil
 }
 
@@ -202,12 +197,7 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http
 }
 
 func googleConnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
-       c, err := gfit.NewClient(ctx, u)
-       if err != nil {
-               return err
-       }
-
-       http.Redirect(w, r, c.AuthURL(ctx), http.StatusTemporaryRedirect)
+       http.Redirect(w, r, gfit.AuthURL(ctx, u), http.StatusTemporaryRedirect)
        return nil
 }