feat: SVGs hinzugefügt und Authentifizierungs-Check vereinfacht

This commit is contained in:
Developer 02
2024-07-22 17:31:30 +02:00
parent fb0e719616
commit 2671a2ff59
19 changed files with 271 additions and 63 deletions

View File

@@ -1,6 +1,4 @@
<body>
<app-nav-menu></app-nav-menu>
<main class="container-fluid">
<router-outlet></router-outlet>
</main>
</body>
<app-nav-menu></app-nav-menu>
<main class="container-fluid">
<router-outlet></router-outlet>
</main>

View File

@@ -15,25 +15,24 @@ export class AuthGuard implements CanActivate {
private router: Router
) {}
canActivate(
async canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
return new Observable(observer => {
this.authService.isAuthenticated().subscribe({
next: (res) => {
if(!res)
this.openLogin();
observer.next(res)
},
error: (error) => {
observer.next(false)
},
complete: () => observer.complete()
});
});
): Promise<boolean> {
try {
const isAuthenticated = await this.authService.isAuthenticated();
if (!isAuthenticated) {
this.openLogin();
}
return isAuthenticated;
} catch (error) {
// Hata durumunda false döndür
return false;
}
}
openLogin(): MatDialogRef<LoginComponent, any> {
const dialogRef = this.dialog.open(LoginComponent, {

View File

@@ -22,7 +22,7 @@ export class NavMenuComponent {
isExpanded = false;
constructor(public dialog: MatDialog, private authService: AuthenticationService) {
this.authService.isAuthenticated().subscribe();
this.authService.isAuthenticated().then(console.log).catch(console.log)
}
get isDarkTheme(): boolean {
@@ -48,24 +48,19 @@ export class NavMenuComponent {
});
}
auth() {
this.authService.isAuthenticated().subscribe({
next: (res) => {
if (res)
this.authService.logout().subscribe();
else {
const dialogRef = this.dialog.open(LoginComponent, {
width: "35vw",
data: {
afterLogin: () => {
dialogRef.close();
}
}
});
async auth() {
const isLoggedIn = await this.authService.isAuthenticated();
if (isLoggedIn)
this.authService.logout().subscribe();
else {
const dialogRef = this.dialog.open(LoginComponent, {
width: "35vw",
data: {
afterLogin: () => {
dialogRef.close();
}
}
},
error: (err) => {
}
})
});
}
}
}

View File

@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Observable, firstValueFrom } from 'rxjs';
import { Router } from '@angular/router';
import Swal from 'sweetalert2';
import { UrlService } from './url.service';
@@ -24,21 +24,15 @@ export class AuthenticationService {
this.checkUrl = urlService.apiRoute.loginCheck;
}
isAuthenticated(): Observable<boolean> {
return new Observable(observer => {
this.http.get<boolean>(this.checkUrl, { withCredentials: true })
.subscribe({
next: (response) => {
_isLogedIn = response;
observer.next(response)
},
error: (error) => {
this.showErrorAlert()
observer.error(error)
},
complete: () => observer.complete()
});
});
async isAuthenticated(): Promise<boolean> {
try {
const response = await firstValueFrom(this.http.get<boolean>(this.checkUrl, { withCredentials: true }));
_isLogedIn = response;
return response;
} catch (error) {
this.showErrorAlert();
return false;
}
}
login(username: string, password: string): Observable<any> {