koa-redirect-loop
Advanced tools
Comparing version 2.0.6 to 3.0.0
27
index.js
@@ -7,3 +7,4 @@ const Url = require('url-parse'); | ||
this.config = { | ||
defaultPath: '/', | ||
getDefaultPath: (ctx) => | ||
ctx.request.locale ? `/${ctx.request.locale}` : '/', | ||
maxRedirects: 5, | ||
@@ -14,4 +15,5 @@ logger: console, | ||
if (!isSANB(this.config.defaultPath)) | ||
throw new Error('defaultPath must be a String'); | ||
if (isSANB(this.config.defaultPath)) | ||
this.config.getDefaultPath = () => this.config.defaultPath; | ||
if ( | ||
@@ -22,2 +24,3 @@ typeof this.config.maxRedirects !== 'number' || | ||
throw new Error('maxRedirects must be a Number greater than zero'); | ||
this.middleware = this.middleware.bind(this); | ||
@@ -51,2 +54,4 @@ } | ||
const defaultPath = config.getDefaultPath(ctx); | ||
if (url === 'back') { | ||
@@ -60,9 +65,8 @@ // | ||
new Url(ctx.href, {}).origin | ||
? new Url(ctx.get('Referrer'), {}).pathname || '/' | ||
: alt || '/'; | ||
? new Url(ctx.get('Referrer'), {}).pathname || defaultPath | ||
: alt || defaultPath; | ||
} | ||
const previousPreviousPath = | ||
ctx.session.prevPrevPath || config.defaultPath; | ||
const previousPath = ctx.session.prevPath || config.defaultPath; | ||
const previousPreviousPath = ctx.session.prevPrevPath || defaultPath; | ||
const previousPath = ctx.session.prevPath || defaultPath; | ||
const previousMethod = ctx.session.prevMethod || ctx.method; | ||
@@ -86,6 +90,9 @@ const maxRedirects = ctx.session.maxRedirects || 1; | ||
const { pathname } = new Url(previousPreviousPath, {}); | ||
address = pathname === previousPreviousPath ? '/' : pathname || '/'; | ||
address = | ||
pathname === previousPreviousPath | ||
? defaultPath | ||
: pathname || defaultPath; | ||
} | ||
} else if (maxRedirects > config.maxRedirects) { | ||
address = config.defaultPath; | ||
address = defaultPath; | ||
} | ||
@@ -92,0 +99,0 @@ |
{ | ||
"name": "koa-redirect-loop", | ||
"description": "Prevent redirect loops with sessions since HTTP referrer header is unreliable and ensures sessions are saved upon redirect", | ||
"version": "2.0.6", | ||
"version": "3.0.0", | ||
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)", | ||
@@ -6,0 +6,0 @@ "ava": { |
@@ -56,2 +56,3 @@ # koa-redirect-loop | ||
* `getDefaultPath` (Function) - function which accepts `ctx` argument and returns a path to fallback to, defaults to either `/${ctx.request.locale}` (e.g. if using `@ladjs/i18n`) or `/` | ||
* `defaultPath` (String) - path to fallback to, defaults to `'/'` | ||
@@ -58,0 +59,0 @@ * `maxRedirects` (Number) - maximum number of redirects to allow, defaults to `5` |
9413
115
77