Last active
November 25, 2015 16:54
-
-
Save hhff/00cb88fe27795c83e414 to your computer and use it in GitHub Desktop.
Patching Torii’s Facebook Connect for Chrome iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const { | |
get, | |
Route | |
} = Ember; | |
export default Route.extend({ | |
beforeModel() { | |
return get(this, 'torii').open('facebook-connect', { recieveManualAuthorization: true }) | |
.then(authorization => { | |
if (authorization) { | |
/* Authorization was recieved, continue! */ | |
return this._doSomethingWith(authorization); | |
} | |
}).catch(response => { | |
/* Most likely the Auth Token is Expired. */ | |
this._handleBadOAuthResponse(response); | |
}); | |
}, | |
actions: { | |
startFacebookConnect() { | |
/* | |
* If FacebookConnectProvider#useManualLogin returns true, | |
* this will initiate a manual login, otherwise it will | |
* fallback to the Facebook JS SDK behaviour. | |
*/ | |
get(this, 'torii').open('facebook-connect').then(authorization => { | |
return this._doSomethingWith(authorization); | |
}); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* app/torii-providers/facebook-connect.js | |
*/ | |
import Ember from 'ember'; | |
import FacebookConnect from 'torii/providers/facebook-connect'; | |
import ajax from 'ic-ajax'; | |
let camelize = Ember.String.camelize; | |
function triggerManualFbLogin (settings, scope, redirectUri) { | |
window.location = `https://www.facebook.com/dialog/oauth? | |
client_id=${settings.appId}& | |
scope=${scope}& | |
redirect_uri=${redirectUri}& | |
response_type=token`; | |
// Never Resolving Promise to Pause App Execution until Redirect | |
return new Ember.RSVP.Promise((resolve) => { if (false) { resolve(); } }); | |
} | |
function recieveManualFbLogin() { | |
return new Ember.RSVP.Promise((resolve, reject) => { | |
let hash = window.location.hash; | |
/* | |
* If your application has multiple hash URLs, you'll | |
* need a deeper check here. | |
*/ | |
if (!hash) { resolve(); } | |
let authorization = {}; | |
let splat = hash.split('#')[1].split('&'); | |
splat.forEach((param) => { | |
let split = param.split('='); | |
authorization[camelize(split[0])] = split[1]; | |
}); | |
/* Reject this if we have an error in the Hash */ | |
if (authorization.hasOwnPropery('error')) { reject(authorization); }; | |
/* We didn't have an error, so fetch the User ID */ | |
ajax({ | |
url: `https://graph.facebook.com/me?fields=id&access_token=${authorization.accessToken}`, | |
type: 'get' | |
}).then(response => { | |
authorization.userId = response.id; | |
resolve(authorization); | |
}).catch(reject); | |
}); | |
} | |
export default FacebookConnect.extend({ | |
buildRedirectUri() { | |
/* | |
* Build the redirect URI. | |
* | |
* Important: The output here needs to be | |
* entered in your Facebook Developer App | |
* as Valid OAuth redirect URIs under | |
* Settings -> Advanced. | |
*/ | |
return window.location.href; | |
}, | |
useManualLogin() { | |
/* | |
* Use this function to test if Manual Login is necessary | |
*/ | |
return navigator.userAgent.match('CriOS'); | |
}, | |
open(options={}) { | |
if (options.recieveManualAuthorization) { | |
/* | |
* Recieve Manual Authentication from redirect URI beforeModel | |
*/ | |
return recieveManualFbLogin(); | |
} else if (this.useManualLogin()) { | |
/* | |
* We're not receiveing, so use the manualFbLogin if necessary | |
*/ | |
return triggerManualFbLogin(this.settings(), this.get('scope'), this.buildRedirectUri()); | |
} else { | |
/* | |
* No need for manual logins, fallback to default behaviour | |
*/ | |
return this._super.apply(this, arguments); | |
} | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment