2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2019-01-31 13:27:40 +03:00
|
|
|
import {alias} from '@ember/object/computed';
|
2019-02-22 08:29:40 +03:00
|
|
|
import {get} from '@ember/object';
|
2019-01-31 13:27:40 +03:00
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
2017-04-13 15:05:29 +03:00
|
|
|
import {
|
|
|
|
isVersionMismatchError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2014-06-24 04:47:51 +04:00
|
|
|
|
2018-09-17 18:03:58 +03:00
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
ajax: service(),
|
|
|
|
config: service(),
|
|
|
|
ghostPaths: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
|
|
|
settings: service(),
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
flowErrors: '',
|
|
|
|
profileImage: null,
|
|
|
|
|
|
|
|
signupDetails: alias('model'),
|
2014-06-24 04:47:51 +04:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
2018-09-17 18:03:58 +03:00
|
|
|
validate(property) {
|
|
|
|
return this.signupDetails.validate({property});
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setImage(image) {
|
|
|
|
this.set('profileImage', image);
|
2019-01-31 13:27:40 +03:00
|
|
|
},
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2019-01-31 13:27:40 +03:00
|
|
|
submit(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.signup.perform();
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
2019-01-31 13:27:40 +03:00
|
|
|
},
|
2017-04-13 15:05:29 +03:00
|
|
|
|
|
|
|
signup: task(function* () {
|
|
|
|
let setupProperties = ['name', 'email', 'password', 'token'];
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
|
|
|
|
this.set('flowErrors', '');
|
2018-09-17 18:03:58 +03:00
|
|
|
this.get('signupDetails.hasValidated').addObjects(setupProperties);
|
2017-04-13 15:05:29 +03:00
|
|
|
|
|
|
|
try {
|
2018-09-17 18:03:58 +03:00
|
|
|
yield this.signupDetails.validate();
|
2017-04-13 15:05:29 +03:00
|
|
|
yield this._completeInvitation();
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield this._authenticateWithPassword();
|
2019-02-26 07:21:24 +03:00
|
|
|
yield this._sendImage.perform();
|
2017-04-13 15:05:29 +03:00
|
|
|
} catch (error) {
|
|
|
|
notifications.showAPIError(error, {key: 'signup.complete'});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// ValidationEngine throws undefined
|
|
|
|
if (!error) {
|
|
|
|
this.set('flowErrors', 'Please fill out the form to complete your sign-up');
|
2018-09-17 18:03:58 +03:00
|
|
|
return false;
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
if (error && error.payload && error.payload.errors && isEmberArray(error.payload.errors)) {
|
2017-04-13 15:05:29 +03:00
|
|
|
if (isVersionMismatchError(error)) {
|
|
|
|
notifications.showAPIError(error);
|
|
|
|
}
|
2017-11-04 01:59:39 +03:00
|
|
|
this.set('flowErrors', error.payload.errors[0].message);
|
2017-04-13 15:05:29 +03:00
|
|
|
} else {
|
|
|
|
notifications.showAPIError(error, {key: 'signup.complete'});
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 18:03:58 +03:00
|
|
|
}).drop(),
|
2017-04-13 15:05:29 +03:00
|
|
|
|
|
|
|
_completeInvitation() {
|
|
|
|
let authUrl = this.get('ghostPaths.url').api('authentication', 'invitation');
|
2018-01-11 01:57:43 +03:00
|
|
|
let signupDetails = this.get('signupDetails');
|
2017-04-13 15:05:29 +03:00
|
|
|
|
|
|
|
return this.get('ajax').post(authUrl, {
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
invitation: [{
|
2018-01-11 01:57:43 +03:00
|
|
|
name: signupDetails.get('name'),
|
|
|
|
email: signupDetails.get('email'),
|
|
|
|
password: signupDetails.get('password'),
|
|
|
|
token: signupDetails.get('token')
|
2017-04-13 15:05:29 +03:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_authenticateWithPassword() {
|
2018-01-11 01:57:43 +03:00
|
|
|
let email = this.get('signupDetails.email');
|
|
|
|
let password = this.get('signupDetails.password');
|
2017-04-13 15:05:29 +03:00
|
|
|
|
|
|
|
return this.get('session')
|
2018-10-05 21:46:33 +03:00
|
|
|
.authenticate('authenticator:cookie', email, password);
|
2017-04-13 15:05:29 +03:00
|
|
|
},
|
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
_sendImage: task(function* () {
|
2017-08-18 06:27:42 +03:00
|
|
|
let formData = new FormData();
|
|
|
|
let imageFile = this.get('profileImage');
|
2019-02-26 07:21:24 +03:00
|
|
|
let uploadUrl = this.get('ghostPaths.url').api('images', 'upload');
|
2017-08-18 06:27:42 +03:00
|
|
|
|
|
|
|
if (imageFile) {
|
2019-02-26 07:21:24 +03:00
|
|
|
formData.append('file', imageFile, imageFile.name);
|
|
|
|
formData.append('purpose', 'profile_image');
|
2015-08-12 03:10:27 +03:00
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
let user = yield this.get('session.user');
|
|
|
|
let response = yield this.get('ajax').post(uploadUrl, {
|
|
|
|
data: formData,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
dataType: 'text'
|
|
|
|
});
|
|
|
|
|
2019-02-26 07:21:24 +03:00
|
|
|
let [image] = get(JSON.parse(response), 'images');
|
|
|
|
let imageUrl = image.url;
|
2018-01-05 18:38:23 +03:00
|
|
|
|
2019-02-26 07:21:24 +03:00
|
|
|
user.set('profileImage', imageUrl);
|
2018-01-05 18:38:23 +03:00
|
|
|
|
2019-02-26 07:21:24 +03:00
|
|
|
return yield user.save();
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
})
|
2014-06-24 04:47:51 +04:00
|
|
|
});
|