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.
This commit is contained in:
Matt Enlow 2014-06-13 12:08:24 -06:00
parent df8ba5f439
commit 49e4403730
2 changed files with 30 additions and 23 deletions

View File

@ -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();
}
}
}

View File

@ -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;