fix for 401 requests

This commit is contained in:
lotte
2018-11-27 11:45:35 +01:00
parent d46ab9637c
commit afa3a03d2c
3 changed files with 29 additions and 9 deletions

View File

@@ -148,7 +148,7 @@ describe('AuthService test', () => {
(state as any).core = Object.create({}); (state as any).core = Object.create({});
(state as any).core.auth = authenticatedState; (state as any).core.auth = authenticatedState;
}); });
authService = new AuthService({}, window, authReqService, router, cookieService, store, rdbService); authService = new AuthService({}, window, undefined, authReqService, router, cookieService, store, rdbService);
})); }));
it('should return true when user is logged in', () => { it('should return true when user is logged in', () => {
@@ -207,7 +207,7 @@ describe('AuthService test', () => {
(state as any).core = Object.create({}); (state as any).core = Object.create({});
(state as any).core.auth = authenticatedState; (state as any).core.auth = authenticatedState;
}); });
authService = new AuthService({}, window, authReqService, router, cookieService, store, rdbService); authService = new AuthService({}, window, undefined, authReqService, router, cookieService, store, rdbService);
storage = (authService as any).storage; storage = (authService as any).storage;
spyOn(storage, 'get'); spyOn(storage, 'get');
spyOn(storage, 'remove'); spyOn(storage, 'remove');

View File

@@ -9,10 +9,10 @@ import {
take, take,
withLatestFrom withLatestFrom
} from 'rxjs/operators'; } from 'rxjs/operators';
import { Inject, Injectable } from '@angular/core'; import { Inject, Injectable, Optional } from '@angular/core';
import { PRIMARY_OUTLET, Router, UrlSegmentGroup, UrlTree } from '@angular/router'; import { PRIMARY_OUTLET, Router, UrlSegmentGroup, UrlTree } from '@angular/router';
import { HttpHeaders } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http';
import { REQUEST } from '@nguniversal/express-engine/tokens'; import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
import { RouterReducerState } from '@ngrx/router-store'; import { RouterReducerState } from '@ngrx/router-store';
import { select, Store } from '@ngrx/store'; import { select, Store } from '@ngrx/store';
@@ -59,6 +59,7 @@ export class AuthService {
constructor(@Inject(REQUEST) protected req: any, constructor(@Inject(REQUEST) protected req: any,
@Inject(NativeWindowService) protected _window: NativeWindowRef, @Inject(NativeWindowService) protected _window: NativeWindowRef,
protected authRequestService: AuthRequestService, protected authRequestService: AuthRequestService,
@Optional() @Inject(RESPONSE) private response: any,
protected router: Router, protected router: Router,
protected storage: CookieService, protected storage: CookieService,
protected store: Store<AppState>, protected store: Store<AppState>,
@@ -345,6 +346,10 @@ export class AuthService {
if (this._window.nativeWindow.location) { if (this._window.nativeWindow.location) {
// Hard redirect to login page, so that all state is definitely lost // Hard redirect to login page, so that all state is definitely lost
this._window.nativeWindow.location.href = redirectUrl; this._window.nativeWindow.location.href = redirectUrl;
} else if (this.response) {
if (!this.response._headerSent) {
this.response.redirect(302, redirectUrl);
}
} else { } else {
this.router.navigateByUrl(redirectUrl); this.router.navigateByUrl(redirectUrl);
} }

View File

@@ -17,6 +17,7 @@ import { ngExpressEngine } from '@nguniversal/express-engine';
import { ROUTES } from './routes'; import { ROUTES } from './routes';
import { ENV_CONFIG } from './config'; import { ENV_CONFIG } from './config';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) { export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) {
const app = express(); const app = express();
@@ -31,9 +32,21 @@ export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) {
app.use(cookieParser()); app.use(cookieParser());
app.use(bodyParser.json()); app.use(bodyParser.json());
app.engine('html', ngExpressEngine({ app.engine('html', (_, options, callback) =>
bootstrap: bootstrap ngExpressEngine({
})); bootstrap: bootstrap,
providers: [
{
provide: REQUEST,
useValue: options.req,
},
{
provide: RESPONSE,
useValue: options.req.res,
},
],
})(_, options, callback)
);
app.set('view engine', 'html'); app.set('view engine', 'html');
app.set('views', 'src'); app.set('views', 'src');
@@ -53,9 +66,11 @@ export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) {
function ngApp(req, res) { function ngApp(req, res) {
function onHandleError(parentZoneDelegate, currentZone, targetZone, error) { function onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
if (!res._headerSent) {
console.warn('Error in SSR, serving for direct CSR'); console.warn('Error in SSR, serving for direct CSR');
res.sendFile('index.csr.html', { root: './src' }); res.sendFile('index.csr.html', { root: './src' });
} }
}
if (ENV_CONFIG.universal.preboot) { if (ENV_CONFIG.universal.preboot) {
Zone.current.fork({ name: 'CSR fallback', onHandleError }).run(() => { Zone.current.fork({ name: 'CSR fallback', onHandleError }).run(() => {