Twitch login, TTS Login, and basic policies ui.
This commit is contained in:
21
src/app/shared/auth/auth.guard.ts
Normal file
21
src/app/shared/auth/auth.guard.ts
Normal 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;
|
||||
}
|
||||
}
|
10
src/app/shared/models/policy.ts
Normal file
10
src/app/shared/models/policy.ts
Normal 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) {
|
||||
|
||||
}
|
||||
}
|
21
src/app/shared/services/EventService.ts
Normal file
21
src/app/shared/services/EventService.ts
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -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();
|
||||
});
|
||||
});
|
42
src/app/shared/services/api/api-authentication.service.ts
Normal file
42
src/app/shared/services/api/api-authentication.service.ts
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user