add helpers

This commit is contained in:
AJ ONeal 2017-02-21 14:18:41 -07:00
parent ae97b30b6f
commit b1ddf92e76
1 changed files with 75 additions and 0 deletions

View File

@ -930,4 +930,79 @@
OAUTH3.PromiseA = Promise; OAUTH3.PromiseA = Promise;
} }
// this is not necessary, but it's relatively small
// and gives people the 3-line examples they love so much
OAUTH3.create = function (location/*, opts*/) {
if (!location) {
location = OAUTH3._browser.window.location;
}
return {
_clientUri: OAUTH3.clientUri(location)
, _providerUri: null
, init: function (location) {
var me = this;
var p = OAUTH3.PromiseA.resolve();
if (location) {
this._clientUri = OAUTH3.clientUri(location);
}
if (this._providerUri) {
p = OAUTH3.discover(this._providerUri, { client_id: this._clientUri }).then(function (/*directives*/) {
$('.js-signin').removeAttr('disabled');
});
}
return OAUTH3.discover(this._clientUri, { client_id: this._clientUri }).then(function (clientDirectives) {
me._clientDirectives = clientDirectives;
return p.then(function () {
return clientDirectives;
});
});
}
, setProvider: function (providerUri) {
var me = this;
me._providerUri = providerUri;
return me.init().then(function () {
// this should be synchronous the second time around
return OAUTH3.discover(me._providerUri, { client_id: me._clientUri }).then(function (directives) {
console.log("setProvider", directives);
me._providerDirectives = directives;
return directives;
});
});
}
, login: function (opts) {
var me = this;
opts = opts || {};
opts.client_uri = me._clientUri;
console.log('login', me._providerDirectives);
return OAUTH3.implicitGrant(me._providerDirectives, opts).then(function (session) {
me._session = true;
return session;
});
}
, session: function () {
return JSON.parse(JSON.stringify(OAUTH3.hooks.session._getCached(this._providerUri)));
}
, request: function (preq) {
preq.client_uri = this._clientUri;
preq.client_id = this._clientUri;
if (this._session) {
preq.session = preq.session || OAUTH3.hooks.session._getCached(this._providerUri);
}
return OAUTH3.request(preq);
}
, logout: function (opts) {
opts = opts || {};
opts.client_uri = this._clientUri;
opts.client_id = this._clientUri;
opts.session = OAUTH3.hooks.session._getCached(this._providerUri);
return OAUTH3.logout(this._providerUri, opts);
}
};
};
}('undefined' !== typeof exports ? exports : window)); }('undefined' !== typeof exports ? exports : window));