backend
This commit is contained in:
@@ -2,9 +2,10 @@ import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
|
||||
import { provideRouter } from '@angular/router';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
||||
import { provideHotToastConfig } from '@ngxpert/hot-toast';
|
||||
import { authInterceptor } from './core/interceptor/auth.interceptor';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideHttpClient(), provideHotToastConfig(),]
|
||||
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideHttpClient(withInterceptors([authInterceptor])), provideHotToastConfig(),]
|
||||
};
|
||||
|
||||
@@ -7,6 +7,6 @@ import { SessionKeyGuard } from './core/guards/session-key.guard';
|
||||
export const routes: Routes = [
|
||||
{ path: 'login', component: LoginComponent, canActivate: [SessionKeyGuard] },
|
||||
{ path: 'register', component: RegisterComponent },
|
||||
{ path: 'dashboard', component: DashboardComponent },
|
||||
{ path: 'dashboard', component: DashboardComponent, canActivate: [SessionKeyGuard] },
|
||||
{ path: '', component: LoginComponent, canActivate: [SessionKeyGuard] },
|
||||
];
|
||||
|
||||
@@ -19,6 +19,8 @@ export class SessionKeyGuard {
|
||||
private client_id;
|
||||
async canActivate(route: ActivatedRouteSnapshot):
|
||||
Promise<boolean> {
|
||||
|
||||
if (this.userService.user) { return true; }
|
||||
this.isLoading = true;
|
||||
|
||||
const success = await this.loginWithSessionId(route);
|
||||
@@ -68,7 +70,7 @@ export class SessionKeyGuard {
|
||||
}
|
||||
} else if (data["id"] != null) {
|
||||
this.userService.user = data as User;
|
||||
resolve(false);
|
||||
resolve(true);
|
||||
this.navigateToDashboard();
|
||||
}
|
||||
}
|
||||
|
||||
20
idp_client/src/app/core/interceptor/auth.interceptor.ts
Normal file
20
idp_client/src/app/core/interceptor/auth.interceptor.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { HttpInterceptorFn, HttpRequest, HttpHandlerFn, HttpEvent } from "@angular/common/http";
|
||||
import { inject } from "@angular/core";
|
||||
import { Observable } from "rxjs";
|
||||
import { UserService } from "../../auth/user.service";
|
||||
|
||||
export const authInterceptor: HttpInterceptorFn = (
|
||||
req: HttpRequest<any>,
|
||||
next: HttpHandlerFn
|
||||
): Observable<HttpEvent<any>> => {
|
||||
const userService = inject(UserService);
|
||||
const token = userService.user?.accessToken;
|
||||
if (token) {
|
||||
const cloned = req.clone({
|
||||
headers: req.headers.set('Authorization', `Bearer ${token}`),
|
||||
});
|
||||
return next(cloned);
|
||||
} else {
|
||||
return next(req);
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Component, inject, OnInit } from '@angular/core';
|
||||
import { UserService } from '../auth/user.service';
|
||||
import { Router } from '@angular/router';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
|
||||
@Component({
|
||||
selector: 'app-dashboard',
|
||||
@@ -13,12 +14,24 @@ export class DashboardComponent implements OnInit {
|
||||
|
||||
private userService: UserService = inject(UserService);
|
||||
private router: Router = inject(Router);
|
||||
private http: HttpClient = inject(HttpClient);
|
||||
|
||||
|
||||
ngOnInit(): void {
|
||||
console.log("ONINIT")
|
||||
if (!this.userService.user) {
|
||||
console.log("REDIRECT")
|
||||
this.router.navigateByUrl("/login");
|
||||
return;
|
||||
}
|
||||
|
||||
this.load();
|
||||
}
|
||||
|
||||
load() {
|
||||
this.http.get('api/app/user/clients').subscribe(res => {
|
||||
console.log(res)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user