Files
app/ts_web/states/idp.state.ts
T

95 lines
2.9 KiB
TypeScript
Raw Normal View History

import * as plugins from '../plugins.js';
import { domtools } from '@design.estate/dees-element'
2024-09-29 13:56:38 +02:00
export class IdpState {
// STATIC
private static idpStateDeferred = plugins.smartpromise.defer<IdpState>();
public static async getSingletonInstance() {
if (!this.idpStateDeferred.claimed) {
this.idpStateDeferred.claim();
const newIdpState = new IdpState();
await newIdpState.init();
this.idpStateDeferred.resolve(newIdpState);
2024-09-29 13:56:38 +02:00
}
return this.idpStateDeferred.promise;
2024-09-29 13:56:38 +02:00
}
// INSTANCE
public receptionUrl = window.location.origin;
2024-09-29 13:56:38 +02:00
public idpClient = new plugins.idpClient.IdpClient(this.receptionUrl);
public domtools: domtools.DomTools;
public mainStatePart: plugins.deesDomtools.plugins.smartstate.StatePart<'main', {
view: 'welcome' | 'login' | 'register' | 'finishregistration' | 'dash' | 'logout';
}>
public async init() {
2025-12-04 17:45:40 +00:00
await this.idpClient.enableTypedSocket();
const domtoolsInstance = await domtools.DomTools.setupDomTools();
this.domtools = domtoolsInstance;
const state = new plugins.deesDomtools.plugins.smartstate.Smartstate<'main'>();
this.mainStatePart = await state.getStatePart('main', {
view: 'welcome',
}, 'soft');
this.domtools.router.on('/', async () => {
await this.mainStatePart.setState({
...this.mainStatePart.getState(),
view: 'welcome',
})
});
this.domtools.router.on('/login', async () => {
const isOauthLogin = new URL(window.location.href).searchParams.get('oauth') === 'true';
if (!isOauthLogin && await this.idpClient.determineLoginStatus(false)) {
this.domtools.router.pushUrl('/dash/overview');
return;
}
await this.mainStatePart.setState({
...this.mainStatePart.getState(),
view: 'login',
})
});
this.domtools.router.on('/logout', async () => {
await this.idpClient.logout();
await this.mainStatePart.setState({
...this.mainStatePart.getState(),
view: 'logout',
})
});
this.domtools.router.on('/register', async () => {
if (await this.idpClient.determineLoginStatus(false)) {
this.domtools.router.pushUrl('/dash/overview');
return;
}
await this.mainStatePart.setState({
...this.mainStatePart.getState(),
view: 'register',
})
});
this.domtools.router.on('/finishregistration', async () => {
await this.mainStatePart.setState({
...this.mainStatePart.getState(),
view: 'finishregistration',
})
});
this.domtools.router.on('/dash{/*path}', async () => {
if (!await this.idpClient.determineLoginStatus(false)) {
this.domtools.router.pushUrl('/login');
return;
}
await this.mainStatePart.setState({
...this.mainStatePart.getState(),
view: 'dash',
})
});
this.domtools.router._handleRouteState();
}
}