Ghost/ghost/admin/app/controllers/signup.js

100 lines
3.6 KiB
JavaScript
Raw Normal View History

import Controller from 'ember-controller';
import RSVP from 'rsvp';
import injectService from 'ember-service/inject';
import {isEmberArray} from 'ember-array/utils';
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
const {Promise} = RSVP;
export default Controller.extend(ValidationEngine, {
// ValidationEngine settings
validationType: 'signup',
submitting: false,
flowErrors: '',
image: null,
ghostPaths: injectService(),
config: injectService(),
notifications: injectService(),
session: injectService(),
ajax: injectService(),
sendImage() {
let image = this.get('image');
this.get('session.user').then((user) => {
2016-01-19 16:03:27 +03:00
return new Promise((resolve, reject) => {
image.formData = {};
image.submit()
.success((response) => {
2016-01-18 18:37:14 +03:00
let usersUrl = this.get('ghostPaths.url').api('users', user.id.toString());
user.image = response;
2016-01-18 18:37:14 +03:00
this.get('ajax').put(usersUrl, {
data: {
users: [user]
}
}).then(resolve).catch(reject);
})
.error(reject);
});
});
},
actions: {
signup() {
let model = this.get('model');
let setupProperties = ['name', 'email', 'password', 'token'];
let data = model.getProperties(setupProperties);
let image = this.get('image');
let notifications = this.get('notifications');
this.set('flowErrors', '');
this.get('hasValidated').addObjects(setupProperties);
2016-09-24 18:48:06 +03:00
return this.validate().then(() => {
2016-01-18 18:37:14 +03:00
let authUrl = this.get('ghostPaths.url').api('authentication', 'invitation');
this.toggleProperty('submitting');
2016-09-24 18:48:06 +03:00
return this.get('ajax').post(authUrl, {
dataType: 'json',
data: {
invitation: [{
name: data.name,
email: data.email,
password: data.password,
token: data.token
}]
}
}).then(() => {
2016-09-24 18:48:06 +03:00
return this.get('session').authenticate('authenticator:oauth2', this.get('model.email'), this.get('model.password')).then(() => {
if (image) {
this.sendImage();
}
}).catch((resp) => {
notifications.showAPIError(resp, {key: 'signup.complete'});
});
}).catch((error) => {
this.toggleProperty('submitting');
2016-01-18 18:37:14 +03:00
if (error && error.errors && isEmberArray(error.errors)) {
if (isVersionMismatchError(error)) {
notifications.showAPIError(error);
}
this.set('flowErrors', error.errors[0].message);
} else {
notifications.showAPIError(error, {key: 'signup.complete'});
}
});
}).catch(() => {
this.set('flowErrors', 'Please fill out the form to complete your sign-up');
});
},
setImage(image) {
this.set('image', image);
}
}
});