mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 12:37:53 +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 '@teamhive/capacitor-video-recorder';
|
||||
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 { VisibilityDirective } from './services/directives/visibility.directive';
|
||||
@@ -217,8 +217,9 @@ import { FirebaseX } from '@ionic-native/firebase-x/ngx'; */
|
||||
DocumentViewer,
|
||||
FFMpeg,
|
||||
{ provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptorService, multi: true },
|
||||
chatTokenInterceptor,
|
||||
tokenInterceptor,
|
||||
/* ChatTokenInterceptor */
|
||||
|
||||
|
||||
],
|
||||
bootstrap: [AppComponent],
|
||||
|
||||
@@ -23,11 +23,11 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
||||
private isRefreshing = false;
|
||||
headers: HttpHeaders;
|
||||
options: any;
|
||||
private refreshTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
|
||||
private refreshChatTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
|
||||
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,
|
||||
private RochetChatConnectorService: RochetChatConnectorService) { }
|
||||
@@ -40,6 +40,10 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
||||
return next.handle(request);
|
||||
}
|
||||
|
||||
if (SessionStore.user.Authorization) {
|
||||
request = this.addToken(request, SessionStore.user.Authorization);
|
||||
}
|
||||
|
||||
return next.handle(request).pipe(
|
||||
catchError((error) => {
|
||||
if (error instanceof HttpErrorResponse && error.status === 401) {
|
||||
@@ -53,7 +57,7 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
||||
|
||||
private shouldExcludeDomain(request: HttpRequest<any>): boolean {
|
||||
const url = request.url.toLowerCase();
|
||||
return !url.includes(this.excludedDomain.toLowerCase());
|
||||
return this.excludedDomains.some((domain) => url.includes(domain.toLowerCase()));
|
||||
}
|
||||
|
||||
private handle401Error(
|
||||
@@ -62,7 +66,7 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
||||
): Observable<HttpEvent<any>> {
|
||||
if (!this.isRefreshing) {
|
||||
this.isRefreshing = true;
|
||||
this.refreshTokenSubject.next(null);
|
||||
this.refreshChatTokenSubject.next(null);
|
||||
|
||||
return this.refreshToken().pipe(
|
||||
switchMap((token: any) => {
|
||||
@@ -77,16 +81,16 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
||||
}
|
||||
SessionStore.user.ChatData = data
|
||||
SessionStore.save()
|
||||
this.setheader()
|
||||
/* this.setheader() */
|
||||
|
||||
|
||||
|
||||
this.refreshTokenSubject.next(token.Authorization);
|
||||
this.refreshChatTokenSubject.next(token.Authorization);
|
||||
return next.handle(this.addToken(request, token.Authorization));
|
||||
})
|
||||
);
|
||||
} else {
|
||||
return this.refreshTokenSubject.pipe(
|
||||
return this.refreshChatTokenSubject.pipe(
|
||||
filter((token) => token != null),
|
||||
take(1),
|
||||
switchMap((jwt) => {
|
||||
@@ -97,13 +101,30 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
||||
}
|
||||
|
||||
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({
|
||||
/* setHeaders: {
|
||||
setHeaders: {
|
||||
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> {
|
||||
return this.http
|
||||
.get<any>(environment.apiURL + 'UserAuthentication/RegenereChatToken', {
|
||||
@@ -144,9 +165,8 @@ export class ChatTokenInterceptor implements HttpInterceptor {
|
||||
|
||||
|
||||
|
||||
export const tokenInterceptor = {
|
||||
export const chatTokenInterceptor = {
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: ChatTokenInterceptor,
|
||||
multi: true
|
||||
};
|
||||
|
||||
@@ -119,9 +119,13 @@ export class TokenInterceptor implements HttpInterceptor {
|
||||
if (environment.production) {
|
||||
window.location.pathname = '/auth'
|
||||
} 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(() =>{
|
||||
if(pathBeforeGoOut != "/auth") {
|
||||
this.httpErrorHandle.httpsSucessMessagge('sessonExpired')
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
return of(false);
|
||||
|
||||
@@ -392,61 +392,6 @@ export class ChatService {
|
||||
|
||||
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