Skip to content

Instantly share code, notes, and snippets.

@gauravtiwari
Created November 19, 2016 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravtiwari/35646595459b446bba5bb9b1aff60bf2 to your computer and use it in GitHub Desktop.
Save gauravtiwari/35646595459b446bba5bb9b1aff60bf2 to your computer and use it in GitHub Desktop.
Stackexchange client side oauth
/* global window SE */
import $ from 'jquery';
export default class StackExchange {
constructor() {
$.getScript(window.STACKOVERFLOW_JS_SDK_URL, (data, textStatus) => {
if (textStatus === 'success') {
SE.init({
clientId: `${window.STACKOVERFLOW_CLIENT_ID}`,
key: `${window.STACKOVERFLOW_CLIENT_KEY}`,
channelUrl: `${window.location.protocol}//${window.location.hostname}/blank`,
complete: () => {
this.SE = SE;
},
});
}
});
}
authenticate() {
return new Promise((resolve, reject) => {
this.SE.authenticate({
success: (data) => {
const uid = data.networkUsers[0].account_id;
resolve({
access_token: data.accessToken,
uid,
expires_at: data.expirationDate,
});
},
error: () => reject('Can not login. Please try again!'),
scope: ['private_info'],
networkUsers: true,
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment