I have modified the JWT Interceptor and added refresh token functionality

This commit is contained in:
Rajnikant 2022-05-27 12:57:56 +05:30
parent 83fa40907e
commit 0185dbd8af
4 changed files with 70 additions and 17 deletions

View File

@ -74,6 +74,7 @@ export class LoginComponent implements OnInit {
server.authToken = response.access_token;
server.username = username;
server.password = password;
server.tokenExpired = false;
await this.serverService.update(server);
if (this.returnUrl.length <= 1) {

View File

@ -1,40 +1,51 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { LoginService } from '@services/login.service';
import { ProjectService } from '@services/project.service';
import { Server } from '../models/server';
import { ServerService } from '../services/server.service';
@Injectable()
export class LoginGuard implements CanActivate {
constructor(
private serverService: ServerService,
private loginService: LoginService,
private router: Router
) {}
private serverService: ServerService,
private loginService: LoginService,
private router: Router,
private projectService: ProjectService
) { }
async canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const server_id = next.paramMap.get('server_id');
this.loginService.server_id = server_id
let server = await this.serverService.get(parseInt(server_id, 10));
try {
await this.loginService.getLoggedUser(server).toPromise();
await this.loginService.getLoggedUser(server)
} catch (e) {
if (e.status === 401) {
server.tokenExpired = true;
await this.serverService.update(server)
// this.test()
// server.tokenExpired = true;
// await this.serverService.update(server)
try {
let response = await this.loginService.login(server, server.username, server.password).toPromise();
server.authToken = response.access_token;
server.tokenExpired = false;
await this.serverService.update(server)
// debugger
// alert('test')
// let response = await this.loginService.getLoggedUserRefToken(server)
// server.authToken = response.access_token;
// server.tokenExpired = false;
// await this.serverService.update(server)
} catch (e) {
throw e;
}
}
}
return this.serverService.get(parseInt(server_id, 10)).then((server: Server) => {
if (server.authToken) return true;
this.router.navigate(['/server', server.id, 'login'], { queryParams: { returnUrl: state.url }});
if (server.authToken && !server.tokenExpired) {
return true
}
this.router.navigate(['/server', server.id, 'login'], { queryParams: { returnUrl: state.url } });
});
}
}

View File

@ -1,10 +1,47 @@
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpEvent, HttpResponse, HttpRequest, HttpHandler } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ServerService } from '@services/server.service';
import { LoginService } from '@services/login.service';
@Injectable()
export class HttpRequestsInterceptor implements HttpInterceptor {
constructor(
private serverService: ServerService,
private loginService: LoginService) { }
intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(httpRequest);
return next.handle(httpRequest).pipe(catchError(err => {
if (err.status === 401) {
this.call()
} else {
return throwError(err)
}
}))
}
async call() {
const server_id = this.loginService.server_id
let server = await this.serverService.get(parseInt(server_id, 10));
server.tokenExpired = true;
await this.serverService.update(server)
try {
let response = await this.loginService.getLoggedUserRefToken(server)
server.authToken = response.access_token;
server.tokenExpired = false;
await this.serverService.update(server)
await this.loginService.getLoggedUser(server)
this.reloadCurrentRoute()
} catch (e) {
throw e;
}
}
reloadCurrentRoute() {
window.location.reload()
}
}

View File

@ -7,6 +7,7 @@ import { AuthResponse } from '../models/authResponse';
@Injectable()
export class LoginService {
server_id:string =''
constructor(private httpServer: HttpServer) {}
login(server: Server, username: string, password: string) {
@ -22,6 +23,9 @@ export class LoginService {
}
getLoggedUser(server: Server) {
return this.httpServer.get(server, "/users/me");
return this.httpServer.get(server, "/users/me").toPromise()
}
async getLoggedUserRefToken(server: Server):Promise<any> {
return await this.httpServer.post<AuthResponse>(server, "/users/authenticate", {"username":server.username,"password":server.password}).toPromise()
}
}