Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fagianijunior/ff970c4eeadfca4cd1764f9fd2852b08 to your computer and use it in GitHub Desktop.
Save fagianijunior/ff970c4eeadfca4cd1764f9fd2852b08 to your computer and use it in GitHub Desktop.
Register a API in https://www.strava.com/settings/api
// Install plugin inAppBrowser
$ ionic cordova plugin add cordova-plugin-inappbrowser
$ npm install --save @ionic-native/in-app-browser
// I create a provider, but in this example I will do inside a "page".
// Import inAppBrowser in /pages/login/login.ts
import { InAppBrowser } from '@ionic-native/in-app-browser';
// and the contructor
constructor(
public iab: InAppBrowser
) {
this.appKey = '<CLIENT_ID>';
this.redirectURI = 'http://localhost';
this.url = 'https://www.strava.com/oauth/authorize?client_id=' + this.appKey + '&redirect_uri=' + this.redirectURI + '&response_type=code&approval_prompt=auto&scope=read_all';
}
// In HTML need a button
<button ion-button large block (click)="login()" class="login">Connect to STRAVA</button>
// The button function
login() {
doLogin().then((code) => {
console.log(code);
});
}
// The login function
doLogin() {
return new Promise((resolve, reject) => {
let browser = this.iab.create(this.url, '_blank', 'location=no');
let listener = browser.on('loadstart').subscribe((event: any) => {
// Avoid transition pages
if((event.url.indexOf('oauth/authorize') > -1) || (event.url.indexOf('oauth/accept_application') > -1)){
return;
}
// On unauthorize
if(event.url.indexOf('?state=&error=access_denied') > -1){
browser.close();
alert("You must authorize access to Strava in order to manage your equipment.");
return;
}
// On authorization success
if(event.url.indexOf(this.redirectURI) > -1 ){
let token = event.url.split('&')[1].split('=')[1];
listener.unsubscribe();
browser.close();
resolve(token);
} else {
reject("Could not authenticate");
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment