Skip to content

Commit

Permalink
Remove dependency on the window object for SSR support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sambego committed Jul 7, 2020
1 parent ab42f6f commit 14797de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion projects/angular-jwt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@auth0/angular-jwt",
"version": "5.0.0",
"version": "5.0.1",
"description": "JSON Web Token helper library for Angular",
"private": false,
"repository": {
Expand Down
18 changes: 13 additions & 5 deletions projects/angular-jwt/src/lib/jwt.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
HttpEvent,
HttpInterceptor,
} from "@angular/common/http";
import { DOCUMENT } from "@angular/common";
import { JwtHelperService } from "./jwthelper.service";
import { JWT_OPTIONS } from "./jwtoptions.token";

Expand All @@ -26,7 +27,8 @@ export class JwtInterceptor implements HttpInterceptor {

constructor(
@Inject(JWT_OPTIONS) config: any,
public jwtHelper: JwtHelperService
public jwtHelper: JwtHelperService,
@Inject(DOCUMENT) private document: Document
) {
this.tokenGetter = config.tokenGetter;
this.headerName = config.headerName || "Authorization";
Expand All @@ -41,11 +43,11 @@ export class JwtInterceptor implements HttpInterceptor {
}

isAllowedDomain(request: HttpRequest<any>): boolean {
const requestUrl: URL = new URL(request.url, window.location.origin);
const requestUrl: URL = new URL(request.url, this.document.location.origin);

// If the host equals the current window origin,
// the domain is allowed by default
if (requestUrl.host === window.location.host) {
if (requestUrl.host === this.document.location.host) {
return true;
}

Expand All @@ -68,12 +70,18 @@ export class JwtInterceptor implements HttpInterceptor {
}

isDisallowedRoute(request: HttpRequest<any>): boolean {
const requestedUrl: URL = new URL(request.url, window.location.origin);
const requestedUrl: URL = new URL(
request.url,
this.document.location.origin
);

return (
this.disallowedRoutes.findIndex((route: string | RegExp) => {
if (typeof route === "string") {
const parsedRoute: URL = new URL(route, window.location.origin);
const parsedRoute: URL = new URL(
route,
this.document.location.origin
);
return (
parsedRoute.hostname === requestedUrl.hostname &&
parsedRoute.pathname === requestedUrl.pathname
Expand Down

0 comments on commit 14797de

Please sign in to comment.