-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathconnect.js
More file actions
41 lines (37 loc) · 1.4 KB
/
connect.js
File metadata and controls
41 lines (37 loc) · 1.4 KB
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
const config = require('./config');
const Dialog = require('./dialog/dialog');
const Promise = require('es6-promise').Promise;
/**
* Sets the oauth_token to the value that was provided by the callback
* @param {Object} options The callback's parameters
* @return {Object} The callback's parameters
*/
const setOauthToken = (options) => {
config.set('oauth_token', options.oauth_token);
return options;
};
module.exports = function (options = {}) {
// resolve immediately when oauth_token is set
const oauth_token = options.oauth_token || config.get('oauth_token');
if (oauth_token) {
setOauthToken(options);
return new Promise((resolve) => { resolve({oauth_token}); });
}
// set up the options for the dialog
// make `client_id`, `redirect_uri` and `scope` overridable
const dialogOptions = {
client_id: options.client_id || config.get('client_id'),
redirect_uri: options.redirect_uri || config.get('redirect_uri'),
response_type: 'code_and_token',
scope: options.scope || 'non-expiring',
display: 'popup'
};
// `client_id` and `redirect_uri` have to be passed
if (!dialogOptions.client_id || !dialogOptions.redirect_uri) {
throw new Error('Options client_id and redirect_uri must be passed');
}
// set up and open the dialog
// set access token when user is done
let dialog = new Dialog(dialogOptions);
return dialog.open().then(setOauthToken);
};