// Generated by CoffeeScript 1.4.0 (function() { var evented; evented = Em.Object.extend(Em.Evented); window.Auth = evented.create({ authToken: null, currentUserId: null, jqxhr: null, prevRoute: null, signIn: function(data) { var async, _this = this; if (data == null) { data = {}; } async = data.async != null ? data.async : true; if (data.async != null) { delete data['async']; } return this.ajax(this.resolveUrl(Auth.Config.get('tokenCreateUrl')), 'POST', { data: data, async: async, success: function(json, status, jqxhr) { _this.set('authToken', json[Auth.Config.get('tokenKey')]); _this.set('currentUserId', json[Auth.Config.get('idKey')]); _this.set('jqxhr', jqxhr); return _this.trigger('signInSuccess'); }, error: function(jqxhr) { _this.set('jqxhr', jqxhr); return _this.trigger('signInError'); }, complete: function(jqxhr) { _this.set('prevRoute', null); _this.set('jqxhr', jqxhr); return _this.trigger('signInComplete'); } }); }, signOut: function(data) { var async, _this = this; if (data == null) { data = {}; } data[Auth.Config.get('tokenKey')] = this.get('authToken'); async = data.async != null ? data.async : true; if (data.async != null) { delete data['async']; } return this.ajax(this.resolveUrl(Auth.Config.get('tokenDestroyUrl')), 'DELETE', { data: data, async: async, success: function(json, status, jqxhr) { _this.set('authToken', null); _this.set('currentUserId', null); _this.set('jqxhr', jqxhr); return _this.trigger('signOutSuccess'); }, error: function(jqxhr) { _this.set('jqxhr', jqxhr); return _this.trigger('signOutError'); }, complete: function(jqxhr) { _this.set('prevRoute', null); _this.set('jqxhr', jqxhr); return _this.trigger('signOutComplete'); } }); }, resolveUrl: function(path) { var base; base = Auth.Config.get('baseUrl'); if (base && base[base.length - 1] === '/') { base = base.substr(0, base.length - 1); } if ((path != null ? path[0] : void 0) === '/') { path = path.substr(1, path.length); } return [base, path].join('/'); }, 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, baseUrl: null, signInRoute: null, signOutRoute: null, authRedirect: false, smartSignInRedirect: false, smartSignOutRedirect: false, signInRedirectFallbackRoute: 'index', signOutRedirectFallbackRoute: 'index', rememberMe: false, rememberTokenKey: null, rememberPeriod: 14, rememberAutoRecall: true }); Auth.Route = Em.Route.extend(Em.Evented, { redirect: function() { if (!Auth.get('authToken')) { this.trigger('authAccess'); if (Auth.Config.get('authRedirect')) { 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); } }); Auth.Module = Em.Object.create(); Auth.Module.RememberMe = Em.Object.create({ init: function() { var _this = this; Auth.on('signInSuccess', function() { return _this.remember(); }); Auth.on('signInError', function() { return _this.forget(); }); return Auth.on('signOutSuccess', function() { return _this.forget(); }); }, recall: function(opts) { var data, token; if (opts == null) { opts = {}; } if (!Auth.Config.get('rememberMe')) { return; } if (!Auth.get('authToken') && (token = $.cookie('ember-auth-remember-me'))) { data = {}; if (opts.async != null) { data['async'] = opts.async; } data[Auth.Config.get('rememberTokenKey')] = token; return Auth.signIn(data); } }, remember: function() { var curToken, json, token; if (!Auth.Config.get('rememberMe')) { return; } json = JSON.parse((Auth.get('jqxhr')).responseText); token = json[Auth.Config.get('rememberTokenKey')]; curToken = $.cookie('ember-auth-remember-me'); if (token && token !== curToken) { return $.cookie('ember-auth-remember-me', token, { expires: Auth.Config.get('rememberPeriod') }); } }, forget: function() { if (!Auth.Config.get('rememberMe')) { return; } return $.removeCookie('ember-auth-remember-me'); } }); Auth.Route.reopen({ redirect: function() { if (Auth.Config.get('rememberMe') && Auth.Config.get('rememberAutoRecall')) { Auth.Module.RememberMe.recall({ async: false }); } return this._super(); } }); }).call(this);