From 49e44037305e5104c17b20f0fc8ae1f53c212db9 Mon Sep 17 00:00:00 2001 From: Matt Enlow Date: Fri, 13 Jun 2014 12:08:24 -0600 Subject: [PATCH] Transition user to original destination after signin Closes #2943 - `AuthenticatedRoute` now stores the user's original destination on the `ApplicationController`. - The `SignIn` route's `login` action checks if the `loginTransition` property is set on the `ApplicationController`, and if so, retries that transition after a successful login. --- core/client/routes/authenticated.js | 16 ++++++++----- core/client/routes/signin.js | 37 ++++++++++++++++------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/core/client/routes/authenticated.js b/core/client/routes/authenticated.js index fa1a0971ed..04c3311934 100644 --- a/core/client/routes/authenticated.js +++ b/core/client/routes/authenticated.js @@ -1,18 +1,22 @@ var AuthenticatedRoute = Ember.Route.extend({ - beforeModel: function () { + beforeModel: function (transition) { var user = this.container.lookup('user:current'); if (!user || !user.get('isSignedIn')) { - this.notifications.showError('Please sign in'); - - this.transitionTo('signin'); + this.redirectToSignin(transition); } }, - + redirectToSignin: function (transition) { + this.notifications.showError('Please sign in'); + if (transition) { + this.controllerFor('application').set('loginTransition', transition); + } + this.transitionTo('signin'); + }, actions: { error: function (error) { if (error.jqXHR && error.jqXHR.status === 401) { - this.transitionTo('signin'); + this.redirectToSignin(); } } } diff --git a/core/client/routes/signin.js b/core/client/routes/signin.js index cef04f5905..d9446a2cbf 100644 --- a/core/client/routes/signin.js +++ b/core/client/routes/signin.js @@ -10,30 +10,33 @@ var SigninRoute = Ember.Route.extend(styleBody, { login: function () { var self = this, controller = this.get('controller'), - data = controller.getProperties('email', 'password'); + data = controller.getProperties('email', 'password'), + //Data to check if user came in somewhere besides index + appController = this.controllerFor('application'), + loginTransition = appController.get('loginTransition'); if (!isEmpty(data.email) && !isEmpty(data.password)) { ajax({ url: this.get('ghostPaths').adminUrl('signin'), type: 'POST', - headers: { - 'X-CSRF-Token': this.get('csrf') - }, + headers: {'X-CSRF-Token': this.get('csrf')}, data: data - }).then( - function (response) { - self.store.pushPayload({ users: [response.userData]}); - self.store.find('user', response.userData.id).then(function (user) { - self.send('signedIn', user); - self.notifications.clear(); - self.transitionTo('posts'); - }); - }, function (resp) { - // This path is ridiculous, should be a helper in notifications; e.g. notifications.showAPIError - self.notifications.showAPIError(resp, 'There was a problem logging in, please try again.'); + }).then(function (response) { + self.store.pushPayload({users: [response.userData]}); + return self.store.find('user', response.userData.id); + }).then(function (user) { + self.send('signedIn', user); + self.notifications.clear(); + if (loginTransition) { + appController.set('loginTransition', null); + loginTransition.retry(); + } else { + self.transitionTo('posts'); } - ); + }).catch(function (resp) { + self.notifications.showAPIError(resp, 'There was a problem logging in, please try again.'); + }); } else { this.notifications.clear(); @@ -43,4 +46,4 @@ var SigninRoute = Ember.Route.extend(styleBody, { } }); -export default SigninRoute; +export default SigninRoute; \ No newline at end of file