Added new auth redirect code for ssr.

This commit is contained in:
Michael W Spalti
2019-09-17 12:44:02 -07:00
parent 8fe617bd74
commit e70b9c5994
2 changed files with 11 additions and 6 deletions

View File

@@ -368,7 +368,7 @@ export class AuthService {
} }
private navigateToRedirectUrl(url: string) { protected navigateToRedirectUrl(url: string) {
// in case the user navigates directly to /login (via bookmark, etc), or the route history is not found. // in case the user navigates directly to /login (via bookmark, etc), or the route history is not found.
if (isEmpty(url) || url.startsWith(LOGIN_ROUTE)) { if (isEmpty(url) || url.startsWith(LOGIN_ROUTE)) {
this.router.navigate(['/']); this.router.navigate(['/']);

View File

@@ -1,12 +1,12 @@
import { map, switchMap, take } from 'rxjs/operators'; import { filter, map, switchMap, take } from 'rxjs/operators';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { HttpHeaders } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http';
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service'; import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
import { AuthStatus } from './models/auth-status.model'; import { AuthStatus } from './models/auth-status.model';
import { isNotEmpty } from '../../shared/empty.util'; import { isEmpty, isNotEmpty } from '../../shared/empty.util';
import { AuthService } from './auth.service'; import { AuthService, LOGIN_ROUTE } from './auth.service';
import { AuthTokenInfo } from './models/auth-token-info.model'; import { AuthTokenInfo } from './models/auth-token-info.model';
import { CheckAuthenticationTokenAction } from './auth.actions'; import { CheckAuthenticationTokenAction } from './auth.actions';
import { EPerson } from '../eperson/models/eperson.model'; import { EPerson } from '../eperson/models/eperson.model';
@@ -67,10 +67,15 @@ export class ServerAuthService extends AuthService {
const url = decodeURIComponent(redirectUrl); const url = decodeURIComponent(redirectUrl);
this.router.navigateByUrl(url); this.router.navigateByUrl(url);
} else { } else {
this.router.navigate(['/']); // If redirectUrl is empty use history. For ssr the history array should contain the requested url.
this.routeService.getHistory().pipe(
filter((history) => history.length > 0),
take(1)
).subscribe((history) => {
this.navigateToRedirectUrl(history[history.length - 1] || '');
});
} }
}) })
} }
} }