From: Florian Forster Date: Wed, 31 Jan 2018 06:59:24 +0000 (+0100) Subject: Connect handlers: don't call {fitbit,gfit}.NewClient(). X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=5b245cc42860c980c321c1c1829cdc373d53ac53;p=kraftakt.git Connect handlers: don't call {fitbit,gfit}.NewClient(). Those constructors try to load the OAuth token from datastore, which doesn't exist yet when these handlers are called. --- diff --git a/fitbit/fitbit.go b/fitbit/fitbit.go index e960438..6f15b7c 100644 --- a/fitbit/fitbit.go +++ b/fitbit/fitbit.go @@ -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) diff --git a/gfit/gfit.go b/gfit/gfit.go index 858f94d..ec88b19 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -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") } diff --git a/kraftakt.go b/kraftakt.go index 092cad3..8d56299 100644 --- a/kraftakt.go +++ b/kraftakt.go @@ -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 }