diff --git a/oauth3.core.js b/oauth3.core.js index bcfd474..3b31951 100644 --- a/oauth3.core.js +++ b/oauth3.core.js @@ -1065,7 +1065,7 @@ // 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*/) { + OAUTH3.create = function (location, opts) { if (!location) { location = OAUTH3._browser.window.location; } @@ -1073,16 +1073,32 @@ var result = { _clientUri: OAUTH3.clientUri(location) , _providerUri: null - , init: function (location) { + , _init: function (location, opts) { + var me = this; + if (location) { + me._clientUri = OAUTH3.clientUri(location); + } + if (opts) { + if (opts.providerUri) { + me._providerUri = opts.providerUri; + } + if (opts.session) { + if (!me._providerUri) { + throw new Error("'providerUri' was not supplied"); + } + me.session(opts.session, opts.sessionId); + } + } + } + , init: function (location/*, opts*/) { var me = this; var p = OAUTH3.PromiseA.resolve(); - if (location) { - this._clientUri = OAUTH3.clientUri(location); - } - if (this._providerUri) { + me._init(location, opts); + + if (me._providerUri) { // returns directives - p = OAUTH3.discover(this._providerUri, { client_id: this._clientUri }); + p = OAUTH3.discover(me._providerUri, { client_id: this._clientUri }); } return p.then(function () { @@ -1122,8 +1138,11 @@ return session; }); } - , session: function () { - return JSON.parse(JSON.stringify(OAUTH3.hooks.session._getCached(this._providerUri) || null)); + , session: function (session, id) { + if (!session) { + return JSON.parse(JSON.stringify(OAUTH3.hooks.session._getCached(this._providerUri) || null)); + } + return OAUTH3.hooks.session.set(this._providerUri, session, id); } , request: function (preq, opts) { opts = opts || {}; @@ -1159,6 +1178,9 @@ result.authenticate = result.login; result.authorize = result.login; result.expire = result.logout; + + result._init(location, opts); + return result; };