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

129 lines
3.8 KiB
JavaScript
Raw Normal View History

import Controller from '@ember/controller';
import {alias} from '@ember/object/computed';
import {get} from '@ember/object';
import {isArray as isEmberArray} from '@ember/array';
import {
isVersionMismatchError
} from 'ghost-admin/services/ajax';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default Controller.extend({
ajax: service(),
config: service(),
ghostPaths: service(),
notifications: service(),
session: service(),
settings: service(),
flowErrors: '',
profileImage: null,
signupDetails: alias('model'),
actions: {
validate(property) {
return this.signupDetails.validate({property});
},
setImage(image) {
this.set('profileImage', image);
},
submit(event) {
event.preventDefault();
this.signup.perform();
}
},
signup: task(function* () {
let setupProperties = ['name', 'email', 'password', 'token'];
let notifications = this.notifications;
this.set('flowErrors', '');
this.get('signupDetails.hasValidated').addObjects(setupProperties);
try {
yield this.signupDetails.validate();
yield this._completeInvitation();
try {
yield this._authenticateWithPassword();
yield this._sendImage.perform();
} catch (error) {
notifications.showAPIError(error, {key: 'signup.complete'});
}
return true;
} catch (error) {
// ValidationEngine throws undefined
if (!error) {
this.set('flowErrors', 'Please fill out the form to complete your signup');
return false;
}
if (error && error.payload && error.payload.errors && isEmberArray(error.payload.errors)) {
if (isVersionMismatchError(error)) {
notifications.showAPIError(error);
}
this.set('flowErrors', error.payload.errors[0].message);
} else {
notifications.showAPIError(error, {key: 'signup.complete'});
}
return false;
}
}).drop(),
_completeInvitation() {
let authUrl = this.get('ghostPaths.url').api('authentication', 'invitation');
let signupDetails = this.signupDetails;
return this.ajax.post(authUrl, {
dataType: 'json',
data: {
invitation: [{
name: signupDetails.get('name'),
email: signupDetails.get('email'),
password: signupDetails.get('password'),
token: signupDetails.get('token')
}]
}
});
},
_authenticateWithPassword() {
let email = this.get('signupDetails.email');
let password = this.get('signupDetails.password');
return this.session
.authenticate('authenticator:cookie', email, password);
},
_sendImage: task(function* () {
let formData = new FormData();
let imageFile = this.profileImage;
let uploadUrl = this.get('ghostPaths.url').api('images', 'upload');
if (imageFile) {
formData.append('file', imageFile, imageFile.name);
formData.append('purpose', 'profile_image');
Made `session.user` a synchronous property rather than a promise no issue Having `session.user` return a promise made dealing with it in components difficult because you always had to remember it returned a promise rather than a model and had to handle the async behaviour. It also meant that you couldn't use any current user properties directly inside getters which made refactors to Glimmer/Octane idioms harder to reason about. `session.user` was a cached computed property so it really made no sense for it to be a promise - it was loaded on first access and then always returned instantly but with a fulfilled promise rather than the underlying model. Refactoring to a synchronous property that is loaded as part of the authentication flows (we load the current user to check that we're logged in - we may as well make use of that!) means one less thing to be aware of/remember and provides a nicer migration process to Glimmer components. As part of the refactor, the auth flows and pre-load of required data across other services was also simplified to make it easier to find and follow. - refactored app setup and `session.user` - added `session.populateUser()` that fetches a user model from the current user endpoint and sets it on `session.user` - removed knowledge of app setup from the `cookie` authenticator and moved it into = `session.postAuthPreparation()`, this means we have the same post-authentication setup no matter which authenticator is used so we have more consistent behaviour in tests which don't use the `cookie` authenticator - switched `session` service to native class syntax to get the expected `super()` behaviour - updated `handleAuthentication()` so it populate's `session.user` and performs post-auth setup before transitioning (handles sign-in after app load) - updated `application` route to remove duplicated knowledge of app preload behaviour that now lives in `session.postAuthPreparation()` (handles already-authed app load) - removed out-of-date attempt at pre-loading data from setup controller as that's now handled automatically via `session.handleAuthentication` - updated app code to not treat `session.user` as a promise - predominant usage was router `beforeModel` hooks that transitioned users without valid permissions, this sets us up for an easier removal of the `current-user-settings` mixin in the future
2021-07-08 16:37:31 +03:00
let user = this.session.user;
let response = yield this.ajax.post(uploadUrl, {
data: formData,
processData: false,
contentType: false,
dataType: 'text'
});
let [image] = get(JSON.parse(response), 'images');
let imageUrl = image.url;
user.set('profileImage', imageUrl);
return yield user.save();
}
})
});