Twitch login, TTS Login, and basic policies ui.

This commit is contained in:
Tom
2024-10-17 08:48:15 +00:00
parent f2133bbc0e
commit 89d944cd6e
48 changed files with 1443 additions and 504 deletions

View File

@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { ApiAuthenticationService } from '../services/api/api-authentication.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private auth: ApiAuthenticationService, private router: Router) {}
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
if (this.auth.isAuthenticated()) {
console.log('Valid OAuth');
return true;
}
console.log("Invalid OAuth");
return false;
}
}

View File

@ -0,0 +1,10 @@
export enum PolicyScope {
Global,
Local
}
export class Policy {
constructor(public path: string, public usage: number, public span: number, public editing: boolean = false, public isNew: boolean = false) {
}
}

View File

@ -0,0 +1,21 @@
import { Injectable } from "@angular/core";
import { Subject } from "rxjs"
@Injectable({
providedIn: 'root'
})
export default class EventService {
private subject = new Subject();
emit(eventName: string, payload: any) {
this.subject.next({ eventName, payload })
}
listen(eventName: string, callback: (event: any) => void) {
return this.subject.asObservable().subscribe((next: any) => {
if (eventName == next.eventName) {
callback(next.payload)
}
})
}
}

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ApiAuthenticationService } from './api-authentication.service';
describe('ApiAuthServiceService', () => {
let service: ApiAuthenticationService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ApiAuthenticationService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,42 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ApiAuthenticationService {
private authenticated: boolean;
private lastCheck: Date;
constructor(private http: HttpClient) {
this.authenticated = false;
this.lastCheck = new Date();
}
isAuthenticated() {
return this.authenticated;
}
update() {
const jwt = localStorage.getItem('jwt');
if (!jwt) {
this.updateAuthenticated(false);
return;
}
// /api/auth/jwt
this.http.get('/api/auth/jwt', {
headers: {
'Authorization': 'Bearer ' + jwt
}
}).subscribe((data: any) => {
console.log('jwt validation', data);
this.updateAuthenticated(data?.authenticated);
});
}
private updateAuthenticated(value: boolean) {
this.authenticated = value;
this.lastCheck = new Date();
}
}