-
Notifications
You must be signed in to change notification settings - Fork 0
/
strava.go
52 lines (45 loc) · 1.96 KB
/
strava.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package handlers
import (
"fmt"
"net/http"
"strconv"
"github.com/jrzimmerman/bestrida-server-go/models"
log "github.com/sirupsen/logrus"
"github.com/strava/go.strava"
)
// AuthHandler Route to display Auth button
func AuthHandler(w http.ResponseWriter, r *http.Request) {
// you should make this a template in your real application
fmt.Fprintf(w, `<a href="%s">`, authenticator.AuthorizationURL("", strava.Permissions.Public, false))
fmt.Fprint(w, `<img src="/assets/img/btn_strava_connectwith_orange.png" />`)
fmt.Fprint(w, `</a>`)
}
func oAuthSuccess(auth *strava.AuthorizationResponse, w http.ResponseWriter, r *http.Request) {
userToken := string(auth.AccessToken)
userID := strconv.Itoa(int(auth.Athlete.Id))
log.WithField("USER TOKEN", userToken).Info("user token on oAuth success")
log.WithField("USER ID", userID).Info("user id on oAuth success")
url := "/login.html?oauth_token=" + userToken + "&userId=" + userID
http.Redirect(w, r, url, http.StatusFound)
_, err := models.RegisterUser(auth)
if err != nil {
log.Errorf("error registering user %d", auth.Athlete.Id)
}
go GetFriendsFromStrava(auth.Athlete.Id)
// page refers to the number of activities to collect for a user
var page = 3
go GetUserSegmentsFromStrava(auth.Athlete.Id, page)
}
func oAuthFailure(err error, w http.ResponseWriter, r *http.Request) {
if err == strava.OAuthAuthorizationDeniedErr {
log.WithError(err).Error("The user clicked the 'Do not Authorize' button on the previous page.")
} else if err == strava.OAuthInvalidCredentialsErr {
log.WithError(err).Error("You provided an incorrect client_id or client_secret.")
} else if err == strava.OAuthInvalidCodeErr {
log.WithError(err).Error("The temporary token was not recognized, this shouldn't happen normally")
} else if err == strava.OAuthServerErr {
log.WithError(err).Error("There was some sort of server error, try again to see if the problem continues")
} else {
log.WithError(err).Error("authorization failure")
}
}