auth guard

This commit is contained in:
eneller
2026-03-08 19:27:18 +01:00
parent cd568f0a63
commit c9a2cd8d66
6 changed files with 53 additions and 5 deletions

View File

@@ -3,12 +3,13 @@ import { ScreenSend } from './screens/screen-send/screen-send';
import { ScreenReceive } from './screens/screen-receive/screen-receive';
import { ScreenProfile } from './screens/screen-profile/screen-profile';
import { ScreenLogin } from './screens/screen-login/screen-login';
import { authGuard } from './services/auth-guard';
export const routes: Routes = [
{
path: '',
pathMatch:'full',
redirectTo: '/send'
redirectTo: '/send',
},
{
path: 'login',
@@ -17,13 +18,16 @@ export const routes: Routes = [
{
path:'send',
component: ScreenSend,
canActivate: [authGuard],
},
{
path:'receive',
component: ScreenReceive,
canActivate: [authGuard],
},
{
path:'profile',
component: ScreenProfile,
canActivate: [authGuard],
},
];

View File

@@ -1,7 +1,7 @@
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { Validators, FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Form } from '@angular/forms';
import { Router } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { APIService } from '../../services/api';
@@ -21,6 +21,7 @@ export class ScreenLogin {
constructor(
private api: APIService,
private router: Router,
private route: ActivatedRoute,
private fb: FormBuilder,
) {
this.loginForm = this.fb.group({
@@ -36,13 +37,15 @@ export class ScreenLogin {
this.api.login(this.loginForm.value.username, this.loginForm.value.password).subscribe({
next: () => {
this.router.navigate(['']);
const returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
this.router.navigateByUrl(returnUrl);
},
error: (err) => {
this.error = err.error?.message || 'Login failed. Please try again.';
this.loading = false;
}
});
this.api.checkAuthStatus().subscribe();
}
}

View File

@@ -23,7 +23,7 @@ export class APIService {
return this.http.post(this.apiUrl + '/auth/logout', {});
}
checkAuthStatus(): Observable<boolean> {
return this.http.get(`${this.apiUrl}/auth/status`, { withCredentials: true }).pipe(
return this.http.get(`${this.apiUrl}/auth/status`, { withCredentials: true}).pipe(
map(() => true),
catchError(() => of(false)),
tap({

View File

@@ -0,0 +1,17 @@
import { TestBed } from '@angular/core/testing';
import { CanActivateFn } from '@angular/router';
import { authGuard } from './auth-guard';
describe('authGuard', () => {
const executeGuard: CanActivateFn = (...guardParameters) =>
TestBed.runInInjectionContext(() => authGuard(...guardParameters));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(executeGuard).toBeTruthy();
});
});

View File

@@ -0,0 +1,20 @@
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { APIService } from './api';
import { map } from 'rxjs/operators';
export const authGuard: CanActivateFn = (route, state) => {
const api = inject(APIService);
const router = inject(Router);
return api.isAuthenticated$.pipe(
map((isAuthenticated) => {
if (isAuthenticated) {
return true;
} else {
router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
}
})
);
};