Add session service and startup logic to check for authed user (#15)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
2020-10-12 20:31:20 -05:00
parent d3af6611c6
commit 3e9a0a03f8
9 changed files with 165 additions and 12 deletions

View File

@@ -0,0 +1,10 @@
export interface RedirectAction {
type: 'redirect';
args: {
destination: string
blank?: boolean
appUrl?: boolean
};
}
export type Action = RedirectAction;

View File

@@ -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;
}
}
}
}

View File

@@ -9,6 +9,7 @@ import ApiResponse from '../structures/ApiResponse';
})
export class ApiService {
protected baseEndpoint: string = environment.backendBase;
protected statUrl: string = environment.statUrl;
constructor(
protected http: HttpClient,
@@ -23,6 +24,14 @@ export class ApiService {
}
public request(endpoint, params = {}, method: 'get'|'post' = 'get'): Observable<ApiResponse> {
return this._request(this._build_url(endpoint), params, method);
}
public stat(): Observable<ApiResponse> {
return this._request(this.statUrl);
}
protected _request(endpoint, params = {}, method: 'get'|'post' = 'get'): Observable<ApiResponse> {
return new Observable<ApiResponse>(sub => {
let data: any = {}
if ( method === 'get' ) {
@@ -31,7 +40,7 @@ export class ApiService {
data = params;
}
this.http[method](this._build_url(endpoint), data).subscribe({
this.http[method](endpoint, data).subscribe({
next: (response: any) => {
sub.next(new ApiResponse(response));
},

View File

@@ -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('/')}`;
}
}