2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2022-05-27 15:28:29 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2019-01-31 13:27:40 +03:00
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
2022-05-27 15:28: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';
|
2022-05-27 15:28:29 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2014-06-24 04:47:51 +04:00
|
|
|
|
2022-05-27 15:28:29 +03:00
|
|
|
export default class SignupController extends Controller {
|
|
|
|
@service ajax;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service notifications;
|
|
|
|
@service session;
|
|
|
|
@service settings;
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
|
|
|
|
2022-05-27 15:28:29 +03:00
|
|
|
@tracked flowErrors = '';
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-05-27 15:28:29 +03:00
|
|
|
get signupDetails() {
|
|
|
|
return this.model;
|
|
|
|
}
|
2014-06-24 04:47:51 +04:00
|
|
|
|
2022-05-27 15:28:29 +03:00
|
|
|
@action
|
|
|
|
validate(property) {
|
|
|
|
return this.signupDetails.validate({property});
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-05-27 15:28:29 +03:00
|
|
|
@action
|
|
|
|
setSignupProperty(property, event) {
|
|
|
|
const value = event.target.value;
|
|
|
|
this.signupDetails[property] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
trimSignupProperty(property, event) {
|
|
|
|
const value = event.target.value.trim();
|
|
|
|
this.signupDetails[property] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
submit(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.signupTask.perform();
|
|
|
|
}
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2022-05-27 15:28:29 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*signupTask() {
|
|
|
|
const setupProperties = ['name', 'email', 'password', 'token'];
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2022-05-27 15:28:29 +03:00
|
|
|
this.flowErrors = '';
|
|
|
|
this.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();
|
|
|
|
} catch (error) {
|
2022-05-27 15:28:29 +03:00
|
|
|
this.notifications.showAPIError(error, {key: 'signup.complete'});
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
2022-05-27 14:13:54 +03:00
|
|
|
|
|
|
|
return true;
|
2017-04-13 15:05:29 +03:00
|
|
|
} catch (error) {
|
|
|
|
// ValidationEngine throws undefined
|
|
|
|
if (!error) {
|
2022-05-27 15:28:29 +03:00
|
|
|
this.flowErrors = 'Please fill out the form to complete your signup';
|
2018-09-17 18:03:58 +03:00
|
|
|
return false;
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
|
|
|
|
2022-05-27 15:28:29 +03:00
|
|
|
if (isEmberArray(error?.payload?.errors)) {
|
2017-04-13 15:05:29 +03:00
|
|
|
if (isVersionMismatchError(error)) {
|
2022-05-27 15:28:29 +03:00
|
|
|
this.notifications.showAPIError(error);
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
2022-05-27 15:28:29 +03:00
|
|
|
this.flowErrors = error.payload.errors[0].message;
|
2017-04-13 15:05:29 +03:00
|
|
|
} else {
|
2022-05-27 15:28:29 +03:00
|
|
|
this.notifications.showAPIError(error, {key: 'signup.complete'});
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
2022-05-27 14:13:54 +03:00
|
|
|
|
|
|
|
return false;
|
2017-04-13 15:05:29 +03:00
|
|
|
}
|
2022-05-27 15:28:29 +03:00
|
|
|
}
|
2017-04-13 15:05:29 +03:00
|
|
|
|
|
|
|
_completeInvitation() {
|
2022-05-27 15:28:29 +03:00
|
|
|
const authUrl = this.ghostPaths.url.api('authentication', 'invitation');
|
|
|
|
const signupDetails = this.signupDetails;
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.ajax.post(authUrl, {
|
2017-04-13 15:05:29 +03:00
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
invitation: [{
|
2022-05-27 15:28:29 +03:00
|
|
|
name: signupDetails.name,
|
|
|
|
email: signupDetails.email,
|
|
|
|
password: signupDetails.password,
|
|
|
|
token: signupDetails.token
|
2017-04-13 15:05:29 +03:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
2022-05-27 15:28:29 +03:00
|
|
|
}
|
2017-04-13 15:05:29 +03:00
|
|
|
|
|
|
|
_authenticateWithPassword() {
|
2022-05-27 15:28:29 +03:00
|
|
|
const {email, password} = this.signupDetails;
|
2017-04-13 15:05:29 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.session
|
2018-10-05 21:46:33 +03:00
|
|
|
.authenticate('authenticator:cookie', email, password);
|
2022-05-27 15:04:27 +03:00
|
|
|
}
|
2022-05-27 15:28:29 +03:00
|
|
|
}
|