// Generated by CoffeeScript 1.4.0 (function() { window.Auth = Em.Object.create({ authToken: null, currentUserId: null, error: null, prevRoute: null, signIn: function(data) { var _this = this; if (data == null) { data = {}; } return this.ajax(Auth.Config.get('tokenCreateUrl'), 'POST', { data: data, success: function(json) { _this.set('authToken', json[Auth.Config.get('tokenKey')]); return _this.set('currentUserId', json[Auth.Config.get('idKey')]); }, error: function(json) { return _this.set('error', json); }, complete: function() { return _this.set('prevRoute', null); } }); }, signOut: function(data) { var _this = this; if (data == null) { data = {}; } data[Auth.Config.get('tokenKey')] = this.get('authToken'); return this.ajax(Auth.Config.get('tokenDestroyUrl'), 'DELETE', { data: data, success: function(json) { _this.set('authToken', null); return _this.set('currentUserId', null); }, error: function(json) { return _this.set('error', json); }, complete: function() { return _this.set('prevRoute', null); } }); }, resolveRedirectRoute: function(type) { var fallback, isSmart, sameRoute, typeClassCase; if (type !== 'signIn' && type !== 'signOut') { return null; } typeClassCase = "" + (type[0].toUpperCase()) + (type.slice(1)); isSmart = Auth.Config.get("smart" + typeClassCase + "Redirect"); fallback = Auth.Config.get("" + type + "RedirectFallbackRoute"); sameRoute = Auth.Config.get("" + type + "Route"); if (!isSmart) { return fallback; } if (!(this.prevRoute != null) || this.prevRoute === sameRoute) { return fallback; } else { return this.prevRoute; } }, ajax: function(url, type, hash) { hash.url = url; hash.type = type; hash.dataType = 'json'; hash.contentType = 'application/json; charset=utf-8'; if (hash.data && type !== 'GET') { hash.data = JSON.stringify(hash.data); } return jQuery.ajax(hash); } }); Auth.Config = Em.Object.create({ tokenCreateUrl: null, tokenDestroyUrl: null, tokenKey: null, idKey: null, signInRoute: null, signOutRoute: null, authRedirect: false, smartSignInRedirect: false, smartSignOutRedirect: false, signInRedirectFallbackRoute: 'index', signOutRedirectFallbackRoute: 'index' }); Auth.Route = Em.Route.extend({ redirect: function() { if (Auth.Config.get('authRedirect') && !Auth.get('authToken')) { Auth.set('prevRoute', this.routeName); return this.transitionTo(Auth.Config.get('signInRoute')); } } }); Auth.SignInController = Em.ObjectController.extend({ registerRedirect: function() { return Auth.addObserver('authToken', this, 'smartSignInRedirect'); }, smartSignInRedirect: function() { if (Auth.get('authToken')) { this.get('target.router').transitionTo(Auth.resolveRedirectRoute('signIn')); return Auth.removeObserver('authToken', this, 'smartSignInRedirect'); } } }); Auth.SignOutController = Em.ObjectController.extend({ registerRedirect: function() { return Auth.addObserver('authToken', this, 'smartSignOutRedirect'); }, smartSignOutRedirect: function() { if (!Auth.get('authToken')) { this.get('target.router').transitionTo(Auth.resolveRedirectRoute('signOut')); return Auth.removeObserver('authToken', this, 'smartSignOutRedirect'); } } }); Auth.RESTAdapter = DS.RESTAdapter.extend({ ajax: function(url, type, hash) { var token; if (token = Auth.get('authToken')) { hash.data || (hash.data = {}); hash.data[Auth.Config.get('tokenKey')] = Auth.get('authToken'); } hash.context = this; return Auth.ajax(url, type, hash); } }); }).call(this);