Add session service and startup logic to check for authed user (#15)
parent
d3af6611c6
commit
3e9a0a03f8
@ -0,0 +1,10 @@
|
|||||||
|
export interface RedirectAction {
|
||||||
|
type: 'redirect';
|
||||||
|
args: {
|
||||||
|
destination: string
|
||||||
|
blank?: boolean
|
||||||
|
appUrl?: boolean
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Action = RedirectAction;
|
@ -0,0 +1,27 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Action } from './action.types';
|
||||||
|
import {SessionService} from './session.service';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class ActionsService {
|
||||||
|
constructor(
|
||||||
|
protected readonly session: SessionService,
|
||||||
|
) { }
|
||||||
|
|
||||||
|
async perform(action: Action) {
|
||||||
|
if ( action.type === 'redirect' ) {
|
||||||
|
let destination = action.args.destination;
|
||||||
|
if ( action.args.appUrl ) {
|
||||||
|
destination = this.session.buildAppUrl(destination);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( action.args.blank ) {
|
||||||
|
window.open(destination, '_blank');
|
||||||
|
} else {
|
||||||
|
window.location.href = destination;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import {ApiService} from './api.service';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class SessionService {
|
||||||
|
public appName!: string;
|
||||||
|
public systemBase!: string;
|
||||||
|
protected data: any = {};
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
protected readonly api: ApiService,
|
||||||
|
) { }
|
||||||
|
|
||||||
|
async stat() {
|
||||||
|
return new Promise((res, rej) => {
|
||||||
|
this.api.stat().subscribe(response => {
|
||||||
|
res(response.data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async initialize() {
|
||||||
|
return new Promise((res, rej) => {
|
||||||
|
this.api.get('/session').subscribe(response => {
|
||||||
|
this.data = response.data;
|
||||||
|
res();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
buildAppUrl(...parts: string[]): string {
|
||||||
|
parts = parts.map(x => {
|
||||||
|
if ( x.startsWith('/') ) {
|
||||||
|
x = x.slice(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( x.endsWith('/') ) {
|
||||||
|
x = x.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
});
|
||||||
|
|
||||||
|
return `${this.systemBase}${this.systemBase.endsWith('/') ? '' : '/'}${parts.join('/')}`;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
export const environment = {
|
export const environment = {
|
||||||
production: true,
|
production: true,
|
||||||
backendBase: '/api/v1',
|
backendBase: '/api/v1',
|
||||||
|
statUrl: '/stat',
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in new issue