Skip to content

Commit

Permalink
Merge pull request #112 from Apes2getherStrong/WTG-4-expired-token-ha…
Browse files Browse the repository at this point in the history
…ndling

Wtg 4 expired token handling
  • Loading branch information
Drillllll authored Sep 13, 2024
2 parents abf0f0e + 9ec7757 commit 17b60cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {RouteListComponent} from "./route/route-list/route-list.component";
import {RegisterComponent} from "./user/register/register.component";
import {MapLocationEditComponent} from "./map-location/map-location-edit/map-location-edit.component";
import {AuthGuardService} from "./auth/auth-guard.service";
import {CanEditService} from "./auth/can-edit.service";



Expand Down Expand Up @@ -98,7 +99,7 @@ const routes: Routes = [
},
{
path: 'yourRoutes/:id/edit',
canActivate: [AuthGuardService],
canActivate: [AuthGuardService,CanEditService],
component: RouteEditComponent
},
{
Expand Down
37 changes: 37 additions & 0 deletions src/app/auth/can-edit.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from "@angular/router";
import {AuthService} from "./auth.service";
import {Observable} from "rxjs";
import {UserService} from "../user/user.service";
import {RouteService} from "../route/route.service";
import {Route} from "../route/route.model";

@Injectable({
providedIn: 'root'
})
export class CanEditService implements CanActivate {

constructor(private authService: AuthService, private router: Router, private routeService: RouteService) {
}

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
const tokenUserId = this.authService.getUserId();

let routeId: string | null = null;
if (route.paramMap.has('id')) {
routeId = route.paramMap.get('id');
this.routeService.getRouteById(routeId).subscribe(
(route) => {
if (tokenUserId === route.user.id) {
} else {
this.router.navigate(['/']);
}
},
)
}
return true
}
}
4 changes: 3 additions & 1 deletion src/app/interceptors/jwt.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { HttpInterceptorFn } from '@angular/common/http';
import {inject} from "@angular/core";
import {AuthService} from "../auth/auth.service";
import {Router} from "@angular/router";
import {catchError, throwError} from "rxjs";

export const jwtInterceptor: HttpInterceptorFn = (req, next) => {
const authService = inject(AuthService);
const token = authService.getToken();

const token = authService.getToken();

if (token) {
req = req.clone({
Expand Down

0 comments on commit 17b60cd

Please sign in to comment.