Token Refresh problem resolved
This commit is contained in:
parent
905e0a9a16
commit
5185ba664c
@ -1 +1 @@
|
||||
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
|
||||
{"version":3,"sources":["../../@angular/common/http (ignored)"],"names":[],"mappings":";;;;;AAAA,e","file":"0.js","sourcesContent":["/* (ignored) */\n\n\n//////////////////\n// WEBPACK FOOTER\n// @angular/common/http (ignored)\n// module id = 721\n// module chunks = 0"],"sourceRoot":""}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,5 +1,5 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<widget id="com.sineedge.pdgenie" version="0.0.19" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
|
||||
<widget id="com.sineedge.pdgenie" version="0.0.20" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
|
||||
<name>PD Genie Officer </name>
|
||||
<description>info@pdgenie.com</description>
|
||||
<author email="info@pdgenie.com" href="https://pdgenie.com">PD Genie</author>
|
||||
|
||||
@ -143,6 +143,7 @@ import { OfficerNotesModalPage } from '../pages/officer-notes-modal/officer-note
|
||||
import { QueriesDocsPage } from '../pages/queries-docs/queries-docs';
|
||||
import {Market} from '@ionic-native/market'
|
||||
import { PdGeneralImagesModule } from '../pages/pd-general-images/Pd-general-images.module';
|
||||
import { ReqTokenInterceptor } from '../providers/_guard/req-token.interceptor';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
@ -311,12 +312,12 @@ import { PdGeneralImagesModule } from '../pages/pd-general-images/Pd-general-ima
|
||||
|
||||
],
|
||||
providers: [
|
||||
StatusBar,AwsServiceProvider,AuthGuard,TokenInterceptor,ToastProvider,
|
||||
StatusBar,AwsServiceProvider,AuthGuard,TokenInterceptor,ToastProvider,ReqTokenInterceptor,
|
||||
ConstantsProvider,CognitoServiceProvider,SplashScreen,PdtriggerProvider,FingerprintAIO,SQLite,AndroidPermissions,BackgroundGeolocation,
|
||||
Push,Events,AppVersion,
|
||||
{
|
||||
provide:HTTP_INTERCEPTORS,
|
||||
useClass: TokenInterceptor,
|
||||
useClass: ReqTokenInterceptor,
|
||||
multi: true
|
||||
},
|
||||
{
|
||||
|
||||
@ -522,7 +522,7 @@ export class PdQuestionGeneralInfoPage {
|
||||
// console.log('full data',this.fulldata[control.length]);
|
||||
|
||||
let formData:FormGroup=this.fb.group({
|
||||
pd_co_applicant_id:[''],
|
||||
pd_co_applicant_id:[this.fulldata[control.length].pd_co_applicant_id],
|
||||
applicant_name:[this.fulldata[control.length].applicant_name],
|
||||
relationship:[''],
|
||||
relation_to:[''],
|
||||
|
||||
160
src/providers/_guard/req-token.interceptor.ts
Normal file
160
src/providers/_guard/req-token.interceptor.ts
Normal file
@ -0,0 +1,160 @@
|
||||
import { Injectable, Injector } from '@angular/core';
|
||||
import {
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpInterceptor,
|
||||
HttpSentEvent,
|
||||
HttpHeaderResponse,
|
||||
HttpProgressEvent,
|
||||
HttpResponse,
|
||||
HttpUserEvent
|
||||
} from '@angular/common/http';
|
||||
// import { AuthGuard } from './auth.guard';
|
||||
import { Observable, of, BehaviorSubject} from 'rxjs';
|
||||
|
||||
// import { Router } from '@angular/router';
|
||||
|
||||
import { catchError, switchMap } from 'rxjs/operators';
|
||||
import { HttpErrorResponse } from "@angular/common/http";
|
||||
|
||||
import { CognitoServiceProvider } from '../aws-service/cognito.service';
|
||||
import { ConstantsProvider } from '../constants/constants';
|
||||
// import { Observable, EMPTY, throwError } from 'rxjs';
|
||||
// import { catchError } from 'rxjs/operators';
|
||||
import 'rxjs/add/operator/catch';
|
||||
import { AwsServiceProvider } from '../aws-service/aws-service';
|
||||
|
||||
// const cognitoService=this.injector.get(CognitoServiceProvider)
|
||||
// const constantProvider=this.injector.get(ConstantsProvider)
|
||||
|
||||
@Injectable()
|
||||
export class ReqTokenInterceptor implements HttpInterceptor{
|
||||
|
||||
constructor(public injector:Injector ) {
|
||||
|
||||
}
|
||||
isRefreshingToken: boolean = false;
|
||||
tokenSubject: BehaviorSubject<string> = new BehaviorSubject<string>(null);
|
||||
|
||||
addToken(req: HttpRequest<any>, token: string): HttpRequest<any> {
|
||||
|
||||
const constantProvider = this.injector.get(ConstantsProvider)
|
||||
return req.clone({
|
||||
setHeaders: {
|
||||
'Authorization': constantProvider.environment().authorization,
|
||||
'Token': token,
|
||||
'From': '2',
|
||||
}
|
||||
})
|
||||
}
|
||||
// addToken1(req: HttpRequest<any>, token: string): HttpRequest<any> {
|
||||
|
||||
// const constantProvider=this.injector.get(ConstantsProvider)
|
||||
// console.log("Token 1",token,req);
|
||||
// return req.clone({ setHeaders: {'Authorization': constantProvider.environment().authorization,
|
||||
// 'Token':token,
|
||||
// 'From':'2',
|
||||
// }})
|
||||
// }
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
|
||||
const cognitoService=this.injector.get(CognitoServiceProvider)
|
||||
|
||||
let token=cognitoService.getToken() || ''
|
||||
console.log("Token From Interceptor >>",cognitoService.getToken())
|
||||
if(token!=''){
|
||||
token=token.getIdToken().getJwtToken()
|
||||
}
|
||||
return <any>next.handle(this.addToken(req, token)).pipe(
|
||||
catchError(error => {
|
||||
if (error instanceof HttpErrorResponse) {
|
||||
if((<HttpErrorResponse>error).status) {
|
||||
return this.handle401Error(req,next,error)
|
||||
}
|
||||
} else {
|
||||
console.log("observable error")
|
||||
return Observable.throw(error);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
handle401Error(req: HttpRequest<any>, next: HttpHandler,err) {
|
||||
console.log("err",err)
|
||||
const cognitoService=this.injector.get(CognitoServiceProvider)
|
||||
console.log("entered",this.isRefreshingToken,this.tokenSubject);
|
||||
|
||||
if (err instanceof HttpErrorResponse) {
|
||||
if(err.status == 401 && err.error.error.toLowerCase() == 'invalid token!'){
|
||||
if (!this.isRefreshingToken) {
|
||||
this.isRefreshingToken = true;
|
||||
|
||||
// Reset here so that the following requests wait until the token
|
||||
// comes back from the refreshToken call.
|
||||
this.tokenSubject.next(null);
|
||||
|
||||
// return cognitoService.refresh()
|
||||
// .subscribe((newToken: string) => {
|
||||
// console.log("new",newToken)
|
||||
// if (newToken) {
|
||||
// this.tokenSubject.next( cognitoService.getToken().getIdToken().getJwtToken());
|
||||
|
||||
// console.log(this.tokenSubject);
|
||||
// next.handle(this.addToken1(req, cognitoService.getToken().getIdToken().getJwtToken()));
|
||||
// }
|
||||
|
||||
// // If we don't get a new token, we are in trouble so logout.
|
||||
// const awsService=this.injector.get(AwsServiceProvider)
|
||||
// awsService.logout();
|
||||
// },err=>console.log("Error refresh",err))
|
||||
return cognitoService.refresh().pipe(
|
||||
switchMap((newToken: string) =>{
|
||||
if(newToken){
|
||||
this.isRefreshingToken=false
|
||||
this.tokenSubject.next(newToken);
|
||||
|
||||
return next.handle(this.addToken(req, cognitoService.getToken().getIdToken().getJwtToken()))
|
||||
}
|
||||
const awsService=this.injector.get(AwsServiceProvider)
|
||||
return awsService.logout();
|
||||
})
|
||||
)
|
||||
|
||||
// .catch(error => {
|
||||
// // If there is an exception calling 'refreshToken', bad news so logout.
|
||||
// return this.logoutUser();
|
||||
// })
|
||||
// .finally(() => {
|
||||
// this.isRefreshingToken = false;
|
||||
// });
|
||||
} else {
|
||||
console.log("Else Part 1201")
|
||||
// return this.tokenSubject
|
||||
// // .filter(token => token != null)
|
||||
// .take(1)
|
||||
// .switchMap(token => {
|
||||
return next.handle(this.addToken(req, cognitoService.getToken().getIdToken().getJwtToken()));
|
||||
// });
|
||||
}
|
||||
}
|
||||
else{
|
||||
this.handleError('Error From Token Interceptor',err);
|
||||
}
|
||||
}
|
||||
else{
|
||||
return Observable.throw("Observable throw",err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private handleError<T> (operation = 'operation', result?: T) {
|
||||
return (error: any): Observable<T> => {
|
||||
if(error instanceof HttpErrorResponse){
|
||||
console.error("Error: " + error.status);
|
||||
if(error.status == 401){
|
||||
localStorage.clear();
|
||||
// this.router.navigate(['/authentication/login']);
|
||||
}
|
||||
return of(result as T);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -96,7 +96,7 @@ refresh() {
|
||||
|
||||
if(this.getCurrentUser() !=null){
|
||||
|
||||
this.getCurrentUser().getSession(function (err, session) {
|
||||
return this.getCurrentUser().getSession(function (err, session) {
|
||||
if (err) {
|
||||
console.log("CognitoUtil: Can't set the credentials:" + err);
|
||||
}
|
||||
|
||||
@ -65,7 +65,7 @@ public commonimage = "http://pdgenie.com/logo/avatar.jpg";
|
||||
userPoolId:'ap-south-1_qQ85yQZJO',
|
||||
clientId:'2o5c3cs9k3als16nqhp3irh8rs' //for mobile
|
||||
}
|
||||
return testing;
|
||||
return production;
|
||||
}
|
||||
|
||||
getcurrentDate()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user