Add session service and startup logic to check for authed user (#15)
This commit is contained in:
10
src/app/service/action.types.ts
Normal file
10
src/app/service/action.types.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface RedirectAction {
|
||||
type: 'redirect';
|
||||
args: {
|
||||
destination: string
|
||||
blank?: boolean
|
||||
appUrl?: boolean
|
||||
};
|
||||
}
|
||||
|
||||
export type Action = RedirectAction;
|
||||
27
src/app/service/actions.service.ts
Normal file
27
src/app/service/actions.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
},
|
||||
|
||||
48
src/app/service/session.service.ts
Normal file
48
src/app/service/session.service.ts
Normal 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('/')}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user