Files
zitadel/console/src/app/services/toast.service.ts
T
Max PeintnerandGitHub eeea1f1c4b feat(console): grpc web interceptors, cleanup services (#631)
* managementservice, authservice, change all refs

* refactor grpc auth

* fix references

* interceptor logic

* new interceptor

* appinitializers, environment loader, interceptors

* remove circular dep

* init authconfig in grpc service

* authconfig

* lint

* update mgmt service

* fix references

* update clientid, fix interceptor

* rm unused authconfig

* todo auth interceptor
2020-08-29 10:43:35 +02:00

62 lines
2.1 KiB
TypeScript

import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar';
import { TranslateService } from '@ngx-translate/core';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { WarnDialogComponent } from '../modules/warn-dialog/warn-dialog.component';
import { AuthenticationService } from './authentication.service';
@Injectable({
providedIn: 'root',
})
export class ToastService {
constructor(private dialog: MatDialog,
private snackBar: MatSnackBar,
private translate: TranslateService,
private authService: AuthenticationService,
) { }
public showInfo(message: string, i18nkey: boolean = false): void {
if (i18nkey) {
this.translate
.get(message)
.subscribe(data => {
this.showMessage(data, 'close');
});
} else {
this.showMessage(message, 'close');
}
}
public showError(grpcError: any): void {
const { message, code, metadata } = grpcError;
// TODO: remove check for code === 7
if (code === 16 || (code === 7 && message === 'invalid token')) {
const dialogRef = this.dialog.open(WarnDialogComponent, {
data: {
confirmKey: 'ACTIONS.LOGIN',
titleKey: 'ERRORS.TOKENINVALID.TITLE',
descriptionKey: 'ERRORS.TOKENINVALID.DESCRIPTION',
},
width: '400px',
});
dialogRef.afterClosed().pipe(take(1)).subscribe(resp => {
if (resp) {
this.authService.authenticate(undefined, true, true);
}
});
} else {
this.showMessage(decodeURI(message), 'close', { duration: 3000 });
}
}
private showMessage(message: string, action: string, config?: MatSnackBarConfig): Observable<void> {
const ref = this.snackBar.open(message, action, config);
return ref.onAction();
}
}