Merge remote-tracking branch 'origin/master'

urls
FlayInAHook 4 years ago
commit 5249773bee

@ -13,18 +13,6 @@ import { DefaultOptions } from '@apollo/client/core/ApolloClient';
const uri = environment.apiUrl + '/graphql'; // <-- add the URL of the GraphQL server here const uri = environment.apiUrl + '/graphql'; // <-- add the URL of the GraphQL server here
const authMiddleware = new ApolloLink((operation, forward) => {
//Add token here TODO: use AuthService to get the Token
operation.setContext({
headers: {
authorization: localStorage.getItem('requestToken') || null,
}
});
return forward(operation).map((data) => {
return data;
});
});
const defaultOptions: DefaultOptions = { const defaultOptions: DefaultOptions = {
watchQuery: { watchQuery: {
fetchPolicy: 'no-cache', fetchPolicy: 'no-cache',
@ -38,7 +26,7 @@ const defaultOptions: DefaultOptions = {
export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> { export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> {
return { return {
link: concat(authMiddleware, httpLink.create({ uri })), link: httpLink.create({ uri }),
cache: new InMemoryCache({}), cache: new InMemoryCache({}),
defaultOptions: defaultOptions, defaultOptions: defaultOptions,
}; };

@ -8,7 +8,7 @@ import { catchError, filter, take, switchMap } from 'rxjs/operators';
export class TokenInterceptor implements HttpInterceptor { export class TokenInterceptor implements HttpInterceptor {
private isRefreshing = false; private isRefreshing = false;
private refreshTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(null); private requestTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(null);
constructor(public authService: AuthService) { } constructor(public authService: AuthService) { }
@ -20,7 +20,6 @@ export class TokenInterceptor implements HttpInterceptor {
return next.handle(request).pipe(catchError(error => { return next.handle(request).pipe(catchError(error => {
if (error instanceof HttpErrorResponse && error.status === 401) { if (error instanceof HttpErrorResponse && error.status === 401) {
console.log("catching error");
return this.handle401Error(request, next); return this.handle401Error(request, next);
} else { } else {
return throwError(error); return throwError(error);
@ -39,17 +38,17 @@ export class TokenInterceptor implements HttpInterceptor {
private handle401Error(request: HttpRequest<any>, next: HttpHandler) { private handle401Error(request: HttpRequest<any>, next: HttpHandler) {
if (!this.isRefreshing) { if (!this.isRefreshing) {
this.isRefreshing = true; this.isRefreshing = true;
this.refreshTokenSubject.next(null); this.requestTokenSubject.next(null);
return this.authService.refreshToken().pipe( return this.authService.refreshToken().pipe(
switchMap((token: any) => { switchMap((token: any) => {
this.isRefreshing = false; this.isRefreshing = false;
this.refreshTokenSubject.next(token.jwt); this.requestTokenSubject.next(token.request_token);
return next.handle(this.addToken(request, token.jwt)); return next.handle(this.addToken(request, token.request_token));
})); }));
} else { } else {
return this.refreshTokenSubject.pipe( return this.requestTokenSubject.pipe(
filter(token => token != null), filter(token => token != null),
take(1), take(1),
switchMap(jwt => { switchMap(jwt => {

@ -9,10 +9,17 @@
matTooltip="Tabllendaten aktualisieren. Achtung! Alle ungespeicherten Änderungen gehen verloren." matTooltip="Tabllendaten aktualisieren. Achtung! Alle ungespeicherten Änderungen gehen verloren."
(click)="reloadTable()" (click)="reloadTable()"
[disabled]="reloadingTable" [disabled]="reloadingTable"
i18n
> >
<mat-icon class="spin">sync</mat-icon> <mat-icon class="spin">sync</mat-icon>
</button> </button>
<button
mat-raised-button
class="table-control-button"
(click)="addEmptyRow()"
[disabled]="reloadingTable"
>
<mat-icon class="spin">add</mat-icon>
</button>
<mat-form-field> <mat-form-field>
<mat-label>Filter</mat-label> <mat-label>Filter</mat-label>
<input <input
@ -23,7 +30,7 @@
/> />
</mat-form-field> </mat-form-field>
<mat-paginator <mat-paginator
[pageSizeOptions]="[25, 30, 50, 100, 500]" [pageSizeOptions]="[15, 25, 30, 50, 100, 500]"
showFirstLastButtons showFirstLastButtons
></mat-paginator> ></mat-paginator>
</div> </div>
@ -60,6 +67,7 @@
</td> </td>
</ng-container> </ng-container>
<!-- Other Columns --> <!-- Other Columns -->
<ng-container <ng-container
*ngFor="let column of columnInfo" *ngFor="let column of columnInfo"
@ -72,8 +80,8 @@
</th> </th>
<td mat-cell *matCellDef="let element"> <td mat-cell *matCellDef="let element">
<app-cell <app-cell
*ngIf="!column.readonly && element.isLockedByMe; else stringValue" *ngIf="column.type === 'Boolean' || (element.newObject || !column.readonly && element.isLockedByMe); else stringValue"
[editable]="true" [editable]="element.newObject || !column.readonly && element.isLockedByMe"
[(value)]="element[column.name]" [(value)]="element[column.name]"
[inputType]="column.type" [inputType]="column.type"
></app-cell> ></app-cell>
@ -85,7 +93,7 @@
<ng-container matColumnDef="buttons" stickyEnd> <ng-container matColumnDef="buttons" stickyEnd>
<th mat-header-cell *matHeaderCellDef></th> <th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let element"> <td mat-cell *matCellDef="let element">
<div class="button-wrapper"> <div class="button-wrapper" *ngIf="!element.newObject">
<button <button
mat-icon-button mat-icon-button
(click)="edit(element)" (click)="edit(element)"

@ -1,8 +1,8 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { finalize, map, tap } from 'rxjs/operators'; import { catchError, finalize, map, tap } from 'rxjs/operators';
import { environment } from '../../environments/environment'; import { environment } from '../../environments/environment';
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject, of } from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -44,23 +44,37 @@ export class AuthService {
logout() { logout() {
// remove token from local storage to log user out // remove token from local storage to log user out
return this.http return this.http
.post<any>(`${environment.authUrl}/logout`, { request_token: this.getRequestToken() }).pipe(finalize(() => { .post<any>(`${environment.authUrl}/logout`, {
this.removeTokens(); request_token: this.getRequestToken(),
this.checkIfUserIsLoggedIn(); })
} .pipe(
)); finalize(() => {
this.removeTokens();
this.checkIfUserIsLoggedIn();
})
);
} }
refreshToken() { refreshToken() {
return this.http.post<any>(`${environment.authUrl}/new-token`, { return this.http
'refresh_token': this.getRefreshToken() .post<any>(`${environment.authUrl}/new-token`, {
}).pipe(tap((tokens: any) => { refresh_token: this.getRefreshToken(),
this.storeTokens(tokens); })
})); .pipe(
} tap((tokens: any) => {
this.storeTokens(tokens);
private storeRequestToken(jwt: string) { })
localStorage.setItem(this.REQUEST_TOKEN, jwt); )
.pipe(
catchError((error: any) => {
if (error.status === 400) {
this.removeTokens();
this.checkIfUserIsLoggedIn();
location.replace('/login');
}
return of();
})
);
} }
private storeTokens(tokens: any) { private storeTokens(tokens: any) {
@ -72,6 +86,4 @@ export class AuthService {
localStorage.removeItem(this.REQUEST_TOKEN); localStorage.removeItem(this.REQUEST_TOKEN);
localStorage.removeItem(this.REFRESH_TOKEN); localStorage.removeItem(this.REFRESH_TOKEN);
} }
} }

Loading…
Cancel
Save