2015-02-12 21:22:32 -07:00
|
|
|
import Ember from 'ember';
|
2015-05-26 19:41:12 -05:00
|
|
|
import {request as ajax} from 'ic-ajax';
|
2015-05-13 00:27:59 -05:00
|
|
|
import Configuration from 'simple-auth/configuration';
|
2014-03-09 23:44:08 -04:00
|
|
|
import styleBody from 'ghost/mixins/style-body';
|
|
|
|
|
2015-05-26 19:41:12 -05:00
|
|
|
export default Ember.Route.extend(styleBody, {
|
2014-07-03 17:06:07 +02:00
|
|
|
classNames: ['ghost-signup'],
|
2015-05-13 00:27:59 -05:00
|
|
|
|
2015-05-25 21:10:50 -05:00
|
|
|
ghostPaths: Ember.inject.service('ghost-paths'),
|
|
|
|
notifications: Ember.inject.service(),
|
|
|
|
|
2014-07-03 17:06:07 +02:00
|
|
|
beforeModel: function () {
|
|
|
|
if (this.get('session').isAuthenticated) {
|
2015-05-25 21:10:50 -05:00
|
|
|
this.get('notifications').showWarn('You need to sign out to register as a new user.', {delayed: true});
|
2015-05-13 00:27:59 -05:00
|
|
|
this.transitionTo(Configuration.routeAfterAuthentication);
|
2014-07-03 17:06:07 +02:00
|
|
|
}
|
|
|
|
},
|
2014-09-19 04:50:15 +00:00
|
|
|
|
|
|
|
model: function (params) {
|
2014-08-24 19:34:26 -07:00
|
|
|
var self = this,
|
|
|
|
tokenText,
|
2014-08-06 08:08:02 -07:00
|
|
|
email,
|
2015-01-12 03:15:13 +00:00
|
|
|
model = Ember.Object.create(),
|
2014-12-08 09:34:40 +01:00
|
|
|
re = /^(?:[A-Za-z0-9_\-]{4})*(?:[A-Za-z0-9_\-]{2}|[A-Za-z0-9_\-]{3})?$/;
|
2014-09-19 04:50:15 +00:00
|
|
|
|
|
|
|
return new Ember.RSVP.Promise(function (resolve) {
|
|
|
|
if (!re.test(params.token)) {
|
2015-05-25 21:10:50 -05:00
|
|
|
self.get('notifications').showError('Invalid token.', {delayed: true});
|
2014-09-19 04:50:15 +00:00
|
|
|
|
|
|
|
return resolve(self.transitionTo('signin'));
|
2014-08-06 08:08:02 -07:00
|
|
|
}
|
2014-08-24 19:34:26 -07:00
|
|
|
|
2014-09-19 04:50:15 +00:00
|
|
|
tokenText = atob(params.token);
|
|
|
|
email = tokenText.split('|')[1];
|
|
|
|
|
2015-01-12 03:15:13 +00:00
|
|
|
model.set('email', email);
|
|
|
|
model.set('token', params.token);
|
2014-09-19 04:50:15 +00:00
|
|
|
|
2015-05-26 19:41:12 -05:00
|
|
|
return ajax({
|
2014-09-19 04:50:15 +00:00
|
|
|
url: self.get('ghostPaths.url').api('authentication', 'invitation'),
|
2014-08-24 19:34:26 -07:00
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
email: email
|
|
|
|
}
|
|
|
|
}).then(function (response) {
|
|
|
|
if (response && response.invitation && response.invitation[0].valid === false) {
|
2015-05-25 21:10:50 -05:00
|
|
|
self.get('notifications').showError('The invitation does not exist or is no longer valid.', {delayed: true});
|
2014-09-19 04:50:15 +00:00
|
|
|
|
|
|
|
return resolve(self.transitionTo('signin'));
|
2014-08-24 19:34:26 -07:00
|
|
|
}
|
2014-09-19 04:50:15 +00:00
|
|
|
|
|
|
|
resolve(model);
|
|
|
|
}).catch(function () {
|
|
|
|
resolve(model);
|
2014-08-24 19:34:26 -07:00
|
|
|
});
|
2014-09-19 04:50:15 +00:00
|
|
|
});
|
|
|
|
},
|
2014-08-24 19:34:26 -07:00
|
|
|
|
2014-09-19 04:50:15 +00:00
|
|
|
deactivate: function () {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
// clear the properties that hold the sensitive data from the controller
|
2014-10-24 21:09:50 +00:00
|
|
|
this.controllerFor('signup').setProperties({email: '', password: '', token: ''});
|
2014-07-03 17:06:07 +02:00
|
|
|
}
|
2014-03-09 23:44:08 -04:00
|
|
|
});
|