Created
November 19, 2016 11:29
-
-
Save gauravtiwari/35646595459b446bba5bb9b1aff60bf2 to your computer and use it in GitHub Desktop.
Stackexchange client side oauth
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
/* 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