2021-07-08 16:37:31 +03:00
|
|
|
import ESASessionService from 'ember-simple-auth/services/session';
|
|
|
|
import RSVP from 'rsvp';
|
|
|
|
import {configureScope} from '@sentry/browser';
|
2021-04-12 15:21:57 +03:00
|
|
|
import {getOwner} from '@ember/application';
|
|
|
|
import {run} from '@ember/runloop';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2021-07-08 16:37:31 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2015-10-18 21:17:02 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
export default class SessionService extends ESASessionService {
|
|
|
|
@service config;
|
|
|
|
@service('store') dataStore;
|
|
|
|
@service feature;
|
|
|
|
@service notifications;
|
|
|
|
@service router;
|
2021-10-28 15:59:41 +03:00
|
|
|
@service frontend;
|
2021-07-08 16:37:31 +03:00
|
|
|
@service settings;
|
2021-08-31 16:21:00 +03:00
|
|
|
@service ui;
|
2021-07-08 16:37:31 +03:00
|
|
|
@service upgradeStatus;
|
|
|
|
@service whatsNew;
|
2015-10-18 21:17:02 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
@tracked user = null;
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
skipAuthSuccessHandler = false;
|
2019-03-25 15:58:14 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
async populateUser(options = {}) {
|
|
|
|
if (this.user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = options.id || 'me';
|
|
|
|
const user = await this.dataStore.queryRecord('user', {id});
|
|
|
|
this.user = user;
|
|
|
|
}
|
|
|
|
|
|
|
|
async postAuthPreparation() {
|
|
|
|
await RSVP.all([
|
|
|
|
this.config.fetchAuthenticated(),
|
|
|
|
this.feature.fetch(),
|
|
|
|
this.settings.fetch()
|
|
|
|
]);
|
2021-11-09 18:38:48 +03:00
|
|
|
|
2021-10-28 15:59:41 +03:00
|
|
|
await this.frontend.loginIfNeeded();
|
2021-11-09 18:38:48 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
// update Sentry with the full Ghost version which we only get after authentication
|
|
|
|
if (this.config.get('sentry_dsn')) {
|
|
|
|
configureScope((scope) => {
|
|
|
|
scope.addEventProcessor((event) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
resolve({
|
|
|
|
...event,
|
|
|
|
release: `ghost@${this.config.get('version')}`
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadServerNotifications();
|
|
|
|
this.whatsNew.fetchLatest.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleAuthentication() {
|
2021-07-14 15:15:59 +03:00
|
|
|
if (!this.user) {
|
|
|
|
try {
|
|
|
|
await this.populateUser();
|
|
|
|
} catch (err) {
|
|
|
|
await this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.postAuthPreparation();
|
|
|
|
}
|
|
|
|
|
2021-07-14 14:27:51 +03:00
|
|
|
if (this.skipAuthSuccessHandler) {
|
|
|
|
this.skipAuthSuccessHandler = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
super.handleAuthentication('home');
|
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
|
|
|
|
handleInvalidation() {
|
|
|
|
let transition = this.appLoadTransition;
|
|
|
|
|
|
|
|
if (transition) {
|
|
|
|
transition.send('authorizationFailed');
|
|
|
|
} else {
|
|
|
|
run.scheduleOnce('routerTransitions', this, 'triggerAuthorizationFailed');
|
|
|
|
}
|
2021-07-08 16:37:31 +03:00
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
|
|
|
|
// TODO: this feels hacky, find a better way than using .send
|
|
|
|
triggerAuthorizationFailed() {
|
|
|
|
getOwner(this).lookup(`route:${this.router.currentRouteName}`).send('authorizationFailed');
|
2021-07-08 16:37:31 +03:00
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
|
|
|
|
loadServerNotifications() {
|
|
|
|
if (this.isAuthenticated) {
|
2021-07-08 16:37:31 +03:00
|
|
|
if (!this.user.isAuthorOrContributor) {
|
|
|
|
this.dataStore.findAll('notification', {reload: true}).then((serverNotifications) => {
|
|
|
|
serverNotifications.forEach((notification) => {
|
|
|
|
if (notification.top || notification.custom) {
|
|
|
|
this.notifications.handleNotification(notification);
|
|
|
|
} else {
|
|
|
|
this.upgradeStatus.handleUpgradeNotification(notification);
|
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
});
|
2021-07-08 16:37:31 +03:00
|
|
|
});
|
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
}
|
2016-05-05 17:03:09 +03:00
|
|
|
}
|
2021-07-08 16:37:31 +03:00
|
|
|
}
|