2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-05-27 03:41:12 +03:00
|
|
|
import {request as ajax} from 'ic-ajax';
|
2014-06-24 04:47:51 +04:00
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
|
|
|
|
2015-05-27 03:41:12 +03:00
|
|
|
export default Ember.Controller.extend(ValidationEngine, {
|
2014-06-24 04:47:51 +04:00
|
|
|
// ValidationEngine settings
|
|
|
|
validationType: 'signup',
|
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
submitting: false,
|
2015-08-10 20:45:50 +03:00
|
|
|
flowErrors: '',
|
2015-05-26 05:10:50 +03:00
|
|
|
|
|
|
|
ghostPaths: Ember.inject.service('ghost-paths'),
|
|
|
|
notifications: Ember.inject.service(),
|
|
|
|
|
2014-06-24 04:47:51 +04:00
|
|
|
actions: {
|
|
|
|
signup: function () {
|
2014-07-03 19:06:07 +04:00
|
|
|
var self = this,
|
2015-01-12 06:15:13 +03:00
|
|
|
model = this.get('model'),
|
2015-05-26 05:10:50 +03:00
|
|
|
data = model.getProperties('name', 'email', 'password', 'token'),
|
|
|
|
notifications = this.get('notifications');
|
2014-06-24 04:47:51 +04:00
|
|
|
|
2015-08-10 20:45:50 +03:00
|
|
|
this.set('flowErrors', '');
|
2015-06-19 00:56:18 +03:00
|
|
|
notifications.closeNotifications();
|
2014-06-25 20:56:09 +04:00
|
|
|
|
2015-07-07 20:14:23 +03:00
|
|
|
this.validate().then(function () {
|
2015-08-08 18:04:22 +03:00
|
|
|
self.toggleProperty('submitting');
|
2014-06-24 04:47:51 +04:00
|
|
|
ajax({
|
2014-07-13 08:01:26 +04:00
|
|
|
url: self.get('ghostPaths.url').api('authentication', 'invitation'),
|
2014-06-24 04:47:51 +04:00
|
|
|
type: 'POST',
|
2014-07-03 19:06:07 +04:00
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
invitation: [{
|
|
|
|
name: data.name,
|
|
|
|
email: data.email,
|
|
|
|
password: data.password,
|
|
|
|
token: data.token
|
|
|
|
}]
|
|
|
|
}
|
2014-06-30 17:34:36 +04:00
|
|
|
}).then(function () {
|
2014-07-25 17:38:13 +04:00
|
|
|
self.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', {
|
2014-12-30 05:11:24 +03:00
|
|
|
identification: self.get('model.email'),
|
|
|
|
password: self.get('model.password')
|
2014-06-30 17:34:36 +04:00
|
|
|
});
|
2015-05-27 23:10:47 +03:00
|
|
|
}).catch(function (resp) {
|
2014-06-24 04:47:51 +04:00
|
|
|
self.toggleProperty('submitting');
|
2015-08-10 20:45:50 +03:00
|
|
|
if (resp && resp.jqXHR && resp.jqXHR.responseJSON && resp.jqXHR.responseJSON.errors) {
|
2015-08-11 16:21:30 +03:00
|
|
|
self.set('flowErrors', resp.jqXHR.responseJSON.errors[0].message);
|
2015-08-10 20:45:50 +03:00
|
|
|
} else {
|
|
|
|
notifications.showAPIError(resp);
|
|
|
|
}
|
2014-06-24 04:47:51 +04:00
|
|
|
});
|
2015-08-10 20:45:50 +03:00
|
|
|
}).catch(function () {
|
|
|
|
self.set('flowErrors', 'Please fill out the form to complete your sign-up');
|
2014-06-24 04:47:51 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|