Merge pull request #2952 from novaugust/ember-signin-transition

Transition user to original destination on login
This commit is contained in:
Hannah Wolfe 2014-06-14 18:12:31 +02:00
commit 0e81f3a20e
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;