auth guard
This commit is contained in:
@@ -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],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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({
|
||||
|
||||
17
client/src/app/services/auth-guard.spec.ts
Normal file
17
client/src/app/services/auth-guard.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
20
client/src/app/services/auth-guard.ts
Normal file
20
client/src/app/services/auth-guard.ts
Normal 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;
|
||||
}
|
||||
})
|
||||
);
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
import express, { Request } from 'express';
|
||||
import { logger } from '../util/logging';
|
||||
import User from '../model/user';
|
||||
import { JWT, JWK } from 'ts-jose';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -36,7 +37,6 @@ router.post('/logout', (req, res) => {
|
||||
});
|
||||
|
||||
router.get('/status', (req, res) => {
|
||||
|
||||
console.log(req.cookies);
|
||||
if (isAuthenticated(req)){
|
||||
return res.status(200).json({authenticated: true});
|
||||
@@ -49,4 +49,8 @@ function isAuthenticated(req: Request){
|
||||
return req.cookies.jwt
|
||||
}
|
||||
|
||||
function getJWT(user: User){
|
||||
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user