import { HttpClient } from '@angular/common/http';
import { CSP_NONCE, Component, OnDestroy, OnInit, inject } from '@angular/core';
import {
ActivatedRoute,
NavigationEnd,
NavigationStart,
Router,
RouterOutlet,
} from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
template: @switch (pageSwitch) { @case (1) { <h1>Welcome to {{ title }}!</h1> <button type="button" (click)="login()">login</button> } @case (2) { <h1>{{ this.Email }}</h1> } @case (3) { <p>test</p> }}
,
styles: [],
})
export class AppComponent implements OnInit {
pageSwitch!: number;
authorizationEndpoint!: string;
title = 'project';
http = inject(HttpClient);
ActivatedRoute = inject(ActivatedRoute);
currentSegment!: string;
Email!: string;
constructor(private router: Router) {}
ngOnInit(): void {
this.pageSwitch = 1;
this.router.events.subscribe((event) => {
this.ActivatedRoute.fragment.subscribe((fragment) => {
if (fragment) {
window.history.pushState(null, '', this.router.url);
const t = fragment?.split('&').map((parm) => {
const [key, value] = parm.split('=');
return { key, value };
});
// console.log(t);
const a = t?.find((v) => v.key === 'id_token')?.value;
// if ('id_token') {
this.Email = JSON.parse(
atob(a!.split('.')?.[1].replace('(.*?)', ''))
)['email'];
// }
// if (event instanceof NavigationEnd) {
const aaa = t?.find((v) => v.key === 'newHash')?.value;
if (!aaa) {
console.log(aaa);
window.location.hash += '&' + 'newHash=test';
this.pageSwitch = 2;
}else{
this.pageSwitch = 3;
}
// if (aaa && this.pageSwitch === 3) {
// }
} else {
this.pageSwitch = 1;
}
});
});
}
generateNonce() {
return Array.from(window.crypto.getRandomValues(new Uint8Array(16)))
.map((b) => b.toString(16))
.join('');
}
}