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 { ScreenReceive } from './screens/screen-receive/screen-receive';
|
||||||
import { ScreenProfile } from './screens/screen-profile/screen-profile';
|
import { ScreenProfile } from './screens/screen-profile/screen-profile';
|
||||||
import { ScreenLogin } from './screens/screen-login/screen-login';
|
import { ScreenLogin } from './screens/screen-login/screen-login';
|
||||||
|
import { authGuard } from './services/auth-guard';
|
||||||
|
|
||||||
export const routes: Routes = [
|
export const routes: Routes = [
|
||||||
{
|
{
|
||||||
path: '',
|
path: '',
|
||||||
pathMatch:'full',
|
pathMatch:'full',
|
||||||
redirectTo: '/send'
|
redirectTo: '/send',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'login',
|
path: 'login',
|
||||||
@@ -17,13 +18,16 @@ export const routes: Routes = [
|
|||||||
{
|
{
|
||||||
path:'send',
|
path:'send',
|
||||||
component: ScreenSend,
|
component: ScreenSend,
|
||||||
|
canActivate: [authGuard],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path:'receive',
|
path:'receive',
|
||||||
component: ScreenReceive,
|
component: ScreenReceive,
|
||||||
|
canActivate: [authGuard],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path:'profile',
|
path:'profile',
|
||||||
component: ScreenProfile,
|
component: ScreenProfile,
|
||||||
|
canActivate: [authGuard],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { Validators, FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Form } from '@angular/forms';
|
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 { NgbModule } from '@ng-bootstrap/ng-bootstrap';
|
||||||
import { APIService } from '../../services/api';
|
import { APIService } from '../../services/api';
|
||||||
|
|
||||||
@@ -21,6 +21,7 @@ export class ScreenLogin {
|
|||||||
constructor(
|
constructor(
|
||||||
private api: APIService,
|
private api: APIService,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
|
private route: ActivatedRoute,
|
||||||
private fb: FormBuilder,
|
private fb: FormBuilder,
|
||||||
) {
|
) {
|
||||||
this.loginForm = this.fb.group({
|
this.loginForm = this.fb.group({
|
||||||
@@ -36,13 +37,15 @@ export class ScreenLogin {
|
|||||||
|
|
||||||
this.api.login(this.loginForm.value.username, this.loginForm.value.password).subscribe({
|
this.api.login(this.loginForm.value.username, this.loginForm.value.password).subscribe({
|
||||||
next: () => {
|
next: () => {
|
||||||
this.router.navigate(['']);
|
const returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
|
||||||
|
this.router.navigateByUrl(returnUrl);
|
||||||
},
|
},
|
||||||
error: (err) => {
|
error: (err) => {
|
||||||
this.error = err.error?.message || 'Login failed. Please try again.';
|
this.error = err.error?.message || 'Login failed. Please try again.';
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.api.checkAuthStatus().subscribe();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export class APIService {
|
|||||||
return this.http.post(this.apiUrl + '/auth/logout', {});
|
return this.http.post(this.apiUrl + '/auth/logout', {});
|
||||||
}
|
}
|
||||||
checkAuthStatus(): Observable<boolean> {
|
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),
|
map(() => true),
|
||||||
catchError(() => of(false)),
|
catchError(() => of(false)),
|
||||||
tap({
|
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 express, { Request } from 'express';
|
||||||
import { logger } from '../util/logging';
|
import { logger } from '../util/logging';
|
||||||
import User from '../model/user';
|
import User from '../model/user';
|
||||||
|
import { JWT, JWK } from 'ts-jose';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@@ -36,7 +37,6 @@ router.post('/logout', (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/status', (req, res) => {
|
router.get('/status', (req, res) => {
|
||||||
|
|
||||||
console.log(req.cookies);
|
console.log(req.cookies);
|
||||||
if (isAuthenticated(req)){
|
if (isAuthenticated(req)){
|
||||||
return res.status(200).json({authenticated: true});
|
return res.status(200).json({authenticated: true});
|
||||||
@@ -49,4 +49,8 @@ function isAuthenticated(req: Request){
|
|||||||
return req.cookies.jwt
|
return req.cookies.jwt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getJWT(user: User){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user