lib/rack/oauth2/admin/js/sammy.oauth2.js in rack-oauth2-server-2.0.0.beta vs lib/rack/oauth2/admin/js/sammy.oauth2.js in rack-oauth2-server-2.0.0.beta2

- old
+ new

@@ -2,10 +2,15 @@ Sammy = Sammy || {}; // Sammy.OAuth2 is a plugin for using OAuth 2.0 to authenticate users and // access your application's API. Requires Sammy.Session. // + // Triggers the following events: + // - oauth.connected -- Access token set and ready to use + // - oauth.disconnected -- Access token reset + // - oauth.denied -- Authorization attempt rejected + // // ### Example // this.use(Sammy.Storage); // this.use(Sammy.OAuth2); // this.oauthorize = "/oauth/authorize"; // @@ -31,12 +36,12 @@ // this.get("#/signout", function(context) { // context.loseAccessToken(); // context.redirect("#/"); // }); // - Sammy.OAuth2 = function(options) { - this.options = options || {}; + Sammy.OAuth2 = function(app) { + app.use(Sammy.JSON); this.authorize = "/oauth/authorize"; // Use this on request that require OAuth token. You can use this in a // filter: it will redirect and return false if the access token is missing. // You can use it in a route, it will redirect to get the access token, or @@ -69,24 +74,24 @@ return this.session("oauth.token"); } // Stores the access token in the session. this.setAccessToken = function(token) { this.session("oauth.token", token); + this.trigger("oauth.connected"); } // Lose access token: use this to sign out. this.loseAccessToken = function() { this.session("oauth.token", null); + this.trigger("oauth.disconnected"); } // Add OAuth 2.0 access token to all XHR requests. - $(document).ajaxSend(function(oauth) { - return function(evt, xhr) { - var token = oauth.getAccessToken(); - if (token) - xhr.setRequestHeader("Authorization", "OAuth " + token); - } - }(this)); + $(document).ajaxSend(function(evt, xhr) { + var token = app.getAccessToken(); + if (token) + xhr.setRequestHeader("Authorization", "OAuth " + token); + }); // Converts query string parameters in fragment identifier to object. function parseParams(hash) { var pairs = hash.substring(1).split("&"), params = {}; for (var i in pairs) { @@ -99,9 +104,11 @@ var start_url; // Capture the application's start URL, we'll need that later on for // redirection. this.bind("run", function(evt, params) { start_url = params.start_url || "#"; + if (this.app.getAccessToken()) + this.trigger("oauth.connected"); }); // Intercept OAuth authorization response with access token, stores it and // redirects to original URL, or application root. this.before(/^#(access_token=|[^\\].*\&access_token=)/, function(context) {