2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2016-07-06 22:47:30 +03:00
|
|
|
import RSVP from 'rsvp';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2017-04-13 15:05:29 +03:00
|
|
|
import {
|
|
|
|
VersionMismatchError,
|
|
|
|
isVersionMismatchError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
2018-01-11 01:57:43 +03:00
|
|
|
import {alias} from '@ember/object/computed';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
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
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Controller.extend(ValidationEngine, {
|
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,
|
|
|
|
|
2014-06-24 04:47:51 +04:00
|
|
|
// ValidationEngine settings
|
|
|
|
validationType: 'signup',
|
2018-01-11 20:43:23 +03:00
|
|
|
signupDetails: alias('model'),
|
2014-06-24 04:47:51 +04:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
|
|
|
signup() {
|
|
|
|
this.get('signup').perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
setImage(image) {
|
|
|
|
this.set('profileImage', image);
|
|
|
|
}
|
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2017-04-13 15:05:29 +03:00
|
|
|
authenticate: task(function* (authStrategy, authentication) {
|
|
|
|
try {
|
|
|
|
let authResult = yield this.get('session')
|
|
|
|
.authenticate(authStrategy, ...authentication);
|
2017-08-02 10:05:59 +03:00
|
|
|
let promises = [];
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2017-08-02 10:05:59 +03:00
|
|
|
promises.pushObject(this.get('settings').fetch());
|
|
|
|
promises.pushObject(this.get('config').fetchPrivate());
|
|
|
|
|
|
|
|
// fetch settings and private config for synchronous access
|
|
|
|
yield RSVP.all(promises);
|
2017-04-13 15:05:29 +03:00
|
|
|
|
|
|
|
return authResult;
|
|
|
|
} catch (error) {
|
2017-11-04 01:59:39 +03:00
|
|
|
if (error && error.payload && error.payload.errors) {
|
2017-04-13 15:05:29 +03:00
|
|
|
// we don't get back an ember-data/ember-ajax error object
|
|
|
|
// back so we need to pass in a null status in order to
|
|
|
|
// test against the payload
|
|
|
|
if (isVersionMismatchError(null, error)) {
|
|
|
|
let versionMismatchError = new VersionMismatchError(error);
|
|
|
|
return this.get('notifications').showAPIError(versionMismatchError);
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
error.payload.errors.forEach((err) => {
|
2017-04-13 15:05:29 +03:00
|
|
|
err.message = err.message.htmlSafe();
|
|
|
|
});
|
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
this.set('flowErrors', error.payload.errors[0].message.string);
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
if (error.payload.errors[0].message.string.match(/user with that email/)) {
|
2018-01-11 01:57:43 +03:00
|
|
|
this.get('signupDetails.errors').add('email', '');
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
if (error.payload.errors[0].message.string.match(/password is incorrect/)) {
|
2018-01-11 01:57:43 +03:00
|
|
|
this.get('signupDetails.errors').add('password', '');
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Connection errors don't return proper status message, only req.body
|
|
|
|
this.get('notifications').showAlert('There was a problem on the server.', {type: 'error', key: 'session.authenticate.failed'});
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
signup: task(function* () {
|
|
|
|
let setupProperties = ['name', 'email', 'password', 'token'];
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
|
|
|
|
this.set('flowErrors', '');
|
|
|
|
this.get('hasValidated').addObjects(setupProperties);
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield this.validate();
|
|
|
|
yield this._completeInvitation();
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield this._authenticateWithPassword();
|
2018-01-05 18:38:23 +03:00
|
|
|
yield this.get('_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');
|
|
|
|
}
|
|
|
|
|
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'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
_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')
|
|
|
|
.authenticate('authenticator:oauth2', email, password);
|
|
|
|
},
|
|
|
|
|
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');
|
|
|
|
let uploadUrl = this.get('ghostPaths.url').api('uploads');
|
|
|
|
|
|
|
|
if (imageFile) {
|
|
|
|
formData.append('uploadimage', imageFile, imageFile.name);
|
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'
|
|
|
|
});
|
|
|
|
|
|
|
|
let imageUrl = JSON.parse(response);
|
|
|
|
let usersUrl = this.get('ghostPaths.url').api('users', user.id.toString());
|
|
|
|
|
|
|
|
user.profile_image = imageUrl;
|
|
|
|
|
|
|
|
return yield this.get('ajax').put(usersUrl, {
|
|
|
|
data: {
|
|
|
|
users: [user]
|
|
|
|
}
|
2015-08-12 03:10:27 +03:00
|
|
|
});
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
})
|
2014-06-24 04:47:51 +04:00
|
|
|
});
|