mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
chat interceptor added
This commit is contained in:
@@ -87,7 +87,7 @@ import { LoggingInterceptorService } from './services/logging-interceptor.servic
|
|||||||
import { PopupQuestionPipe } from './modals/popup-question.pipe';
|
import { PopupQuestionPipe } from './modals/popup-question.pipe';
|
||||||
import '@teamhive/capacitor-video-recorder';
|
import '@teamhive/capacitor-video-recorder';
|
||||||
import { tokenInterceptor } from './interceptors/token.interceptors';
|
import { tokenInterceptor } from './interceptors/token.interceptors';
|
||||||
import { ChatTokenInterceptor } from './interceptors/chatToken.interceptor';
|
import { chatTokenInterceptor } from './interceptors/chatToken.interceptor';
|
||||||
|
|
||||||
import { InputFilterDirective } from './services/directives/input-filter.directive';
|
import { InputFilterDirective } from './services/directives/input-filter.directive';
|
||||||
import { VisibilityDirective } from './services/directives/visibility.directive';
|
import { VisibilityDirective } from './services/directives/visibility.directive';
|
||||||
@@ -217,8 +217,9 @@ import { FirebaseX } from '@ionic-native/firebase-x/ngx'; */
|
|||||||
DocumentViewer,
|
DocumentViewer,
|
||||||
FFMpeg,
|
FFMpeg,
|
||||||
{ provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptorService, multi: true },
|
{ provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptorService, multi: true },
|
||||||
|
chatTokenInterceptor,
|
||||||
tokenInterceptor,
|
tokenInterceptor,
|
||||||
/* ChatTokenInterceptor */
|
|
||||||
|
|
||||||
],
|
],
|
||||||
bootstrap: [AppComponent],
|
bootstrap: [AppComponent],
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
|||||||
private isRefreshing = false;
|
private isRefreshing = false;
|
||||||
headers: HttpHeaders;
|
headers: HttpHeaders;
|
||||||
options: any;
|
options: any;
|
||||||
private refreshTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
|
private refreshChatTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
|
||||||
private excludedDomain = 'https://gdchat-dev.dyndns.info';// Add other domains as needed
|
private excludedDomains = ['Login',environment.apiURL];// Add other domains as needed
|
||||||
|
|
||||||
constructor(private http: HttpClient, private router: Router, private p: PermissionService, private NetworkServiceService: NetworkServiceService,
|
constructor(private http: HttpClient, private router: Router, private p: PermissionService, private NetworkServiceService: NetworkServiceService,
|
||||||
private RochetChatConnectorService: RochetChatConnectorService) { }
|
private RochetChatConnectorService: RochetChatConnectorService) { }
|
||||||
@@ -40,6 +40,10 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
|||||||
return next.handle(request);
|
return next.handle(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SessionStore.user.Authorization) {
|
||||||
|
request = this.addToken(request, SessionStore.user.Authorization);
|
||||||
|
}
|
||||||
|
|
||||||
return next.handle(request).pipe(
|
return next.handle(request).pipe(
|
||||||
catchError((error) => {
|
catchError((error) => {
|
||||||
if (error instanceof HttpErrorResponse && error.status === 401) {
|
if (error instanceof HttpErrorResponse && error.status === 401) {
|
||||||
@@ -53,7 +57,7 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
|||||||
|
|
||||||
private shouldExcludeDomain(request: HttpRequest<any>): boolean {
|
private shouldExcludeDomain(request: HttpRequest<any>): boolean {
|
||||||
const url = request.url.toLowerCase();
|
const url = request.url.toLowerCase();
|
||||||
return !url.includes(this.excludedDomain.toLowerCase());
|
return this.excludedDomains.some((domain) => url.includes(domain.toLowerCase()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private handle401Error(
|
private handle401Error(
|
||||||
@@ -62,7 +66,7 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
|||||||
): Observable<HttpEvent<any>> {
|
): Observable<HttpEvent<any>> {
|
||||||
if (!this.isRefreshing) {
|
if (!this.isRefreshing) {
|
||||||
this.isRefreshing = true;
|
this.isRefreshing = true;
|
||||||
this.refreshTokenSubject.next(null);
|
this.refreshChatTokenSubject.next(null);
|
||||||
|
|
||||||
return this.refreshToken().pipe(
|
return this.refreshToken().pipe(
|
||||||
switchMap((token: any) => {
|
switchMap((token: any) => {
|
||||||
@@ -77,16 +81,16 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
|||||||
}
|
}
|
||||||
SessionStore.user.ChatData = data
|
SessionStore.user.ChatData = data
|
||||||
SessionStore.save()
|
SessionStore.save()
|
||||||
this.setheader()
|
/* this.setheader() */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
this.refreshTokenSubject.next(token.Authorization);
|
this.refreshChatTokenSubject.next(token.Authorization);
|
||||||
return next.handle(this.addToken(request, token.Authorization));
|
return next.handle(this.addToken(request, token.Authorization));
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return this.refreshTokenSubject.pipe(
|
return this.refreshChatTokenSubject.pipe(
|
||||||
filter((token) => token != null),
|
filter((token) => token != null),
|
||||||
take(1),
|
take(1),
|
||||||
switchMap((jwt) => {
|
switchMap((jwt) => {
|
||||||
@@ -97,13 +101,30 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private addToken(request: HttpRequest<any>, token: string) {
|
private addToken(request: HttpRequest<any>, token: string) {
|
||||||
|
let headers = new HttpHeaders();
|
||||||
|
|
||||||
|
|
||||||
|
console.log('X-User-Id', SessionStore.user.ChatData.data.userId)
|
||||||
|
console.log('X-Auth-Token', SessionStore.user.ChatData.data.authToken)
|
||||||
|
headers = headers.set('X-User-Id', SessionStore.user.ChatData.data.userId);
|
||||||
|
headers = headers.set('X-Auth-Token', SessionStore.user.ChatData.data.authToken);
|
||||||
|
|
||||||
return request.clone({
|
return request.clone({
|
||||||
/* setHeaders: {
|
setHeaders: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
}, */
|
...headers.keys().reduce((acc, key) => ({ ...acc, [key]: headers.get(key) }), {}),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* private addToken(request: HttpRequest<any>, token: string) {
|
||||||
|
return request.clone({
|
||||||
|
setHeaders: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} */
|
||||||
|
|
||||||
private refreshToken(): Observable<any> {
|
private refreshToken(): Observable<any> {
|
||||||
return this.http
|
return this.http
|
||||||
.get<any>(environment.apiURL + 'UserAuthentication/RegenereChatToken', {
|
.get<any>(environment.apiURL + 'UserAuthentication/RegenereChatToken', {
|
||||||
@@ -144,9 +165,8 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const tokenInterceptor = {
|
export const chatTokenInterceptor = {
|
||||||
provide: HTTP_INTERCEPTORS,
|
provide: HTTP_INTERCEPTORS,
|
||||||
useClass: ChatTokenInterceptor,
|
useClass: ChatTokenInterceptor,
|
||||||
multi: true
|
multi: true
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -119,9 +119,13 @@ export class TokenInterceptor implements HttpInterceptor {
|
|||||||
if (environment.production) {
|
if (environment.production) {
|
||||||
window.location.pathname = '/auth'
|
window.location.pathname = '/auth'
|
||||||
} else {
|
} else {
|
||||||
/* const pathBeforeGoOut = window.location.pathname */
|
const pathBeforeGoOut = window.location.pathname
|
||||||
|
console.log('Before auth',window.location.pathname)
|
||||||
this.router.navigateByUrl('/auth', { replaceUrl: true }).then(() =>{
|
this.router.navigateByUrl('/auth', { replaceUrl: true }).then(() =>{
|
||||||
|
if(pathBeforeGoOut != "/auth") {
|
||||||
this.httpErrorHandle.httpsSucessMessagge('sessonExpired')
|
this.httpErrorHandle.httpsSucessMessagge('sessonExpired')
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return of(false);
|
return of(false);
|
||||||
|
|||||||
@@ -392,61 +392,6 @@ export class ChatService {
|
|||||||
|
|
||||||
async refreshtoken() {
|
async refreshtoken() {
|
||||||
|
|
||||||
if(this.headers && SessionStore.user.ChatData) {
|
|
||||||
this.headers = this.headers.set('Authorization', 'Bearer ' + SessionStore.user.Authorization);
|
|
||||||
let options = {
|
|
||||||
headers: this.headers
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
let res = await this.http.get(environment.apiURL + 'UserAuthentication/RegenereChatToken', options).toPromise();
|
|
||||||
|
|
||||||
let data = {
|
|
||||||
status: res['status'],
|
|
||||||
data: {
|
|
||||||
userId: res['data'].userId,
|
|
||||||
authToken: res['data'].authToken
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SessionStore.user.ChatData = data
|
|
||||||
SessionStore.save()
|
|
||||||
this.setheader()
|
|
||||||
this.timerEventTriggerDateLastUpdate = new Date();
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Offline) {
|
|
||||||
this.RochetChatConnectorService.registerCallback({
|
|
||||||
requestId: 'refreshtoken',
|
|
||||||
type: 'reConnect',
|
|
||||||
funx: async () => {
|
|
||||||
|
|
||||||
this.resetTimer();
|
|
||||||
await this.refreshtoken();
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if(SessionStore.user.Authorization != '') {
|
|
||||||
setTimeout(async () => {
|
|
||||||
this.resetTimer();
|
|
||||||
await this.refreshtoken();
|
|
||||||
}, 60000)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if(!SessionStore.user.ChatData) {
|
|
||||||
// do nothing
|
|
||||||
} else if (!this.headers) {
|
|
||||||
this.setheader()
|
|
||||||
this.refreshtoken()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user