diff --git a/dist/redux-oidc.js b/dist/redux-oidc.js index 8e22f4f..42f58d0 100644 --- a/dist/redux-oidc.js +++ b/dist/redux-oidc.js @@ -1 +1 @@ -!function(e,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r(require("react"),require("immutable"),require("oidc-client"));else if("function"==typeof define&&define.amd)define(["react","immutable","oidc-client"],r);else{var t="object"==typeof exports?r(require("react"),require("immutable"),require("oidc-client")):r(e.react,e.immutable,e["oidc-client"]);for(var n in t)("object"==typeof exports?exports:e)[n]=t[n]}}(this,function(e,r,t){return function(e){function r(n){if(t[n])return t[n].exports;var o=t[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,r),o.loaded=!0,o.exports}var t={};return r.m=e,r.c=t,r.p="",r(0)}([function(e,r,t){e.exports=t(8)},function(e,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.STORAGE_KEY="oidc.expired",r.USER_EXPIRED="redux-oidc/USER_EXPIRED",r.REDIRECT_SUCCESS="redux-oidc/REDIRECT_SUCCESS",r.USER_LOADED="redux-oidc/USER_LOADED",r.SILENT_RENEW_ERROR="redux-oidc/SILENT_RENEW_ERROR",r.SESSION_TERMINATED="redux-oidc/SESSION_TERMINATED",r.USER_EXPIRING="redux-oidc/USER_EXPIRING",r.USER_FOUND="redux-oidc/USER_FOUND",r.LOADING_USER="redux-oidc/LOADING_USER"},function(e,r,t){"use strict";function n(){return{type:l.USER_EXPIRED}}function o(e){return{type:l.REDIRECT_SUCCESS,payload:e}}function i(e){return{type:l.USER_FOUND,payload:e}}function s(e){return{type:l.SILENT_RENEW_ERROR,payload:e}}function u(){return{type:l.SESSION_TERMINATED}}function a(){return{type:l.USER_EXPIRING}}function c(){return{type:l.LOADING_USER}}Object.defineProperty(r,"__esModule",{value:!0}),r.userExpired=n,r.redirectSuccess=o,r.userFound=i,r.silentRenewError=s,r.sessionTerminated=u,r.userExpiring=a,r.loadingUser=c;var l=t(1)},function(e,r,t){"use strict";function n(e){return new o.UserManager(e)}Object.defineProperty(r,"__esModule",{value:!0}),r["default"]=n;var o=t(13)},function(e,r){e.exports=require("react")},function(e,r,t){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}function o(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}function i(e,r){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!r||"object"!=typeof r&&"function"!=typeof r?e:r}function s(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function, not "+typeof r);e.prototype=Object.create(r&&r.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),r&&(Object.setPrototypeOf?Object.setPrototypeOf(e,r):e.__proto__=r)}Object.defineProperty(r,"__esModule",{value:!0});var u=function(){function e(e,r){for(var t=0;t this.onRedirectSuccess(user)) .catch((error) => this.onRedirectError(error)); } diff --git a/src/oidcMiddleware.js b/src/oidcMiddleware.js index ded700d..8fda4ae 100644 --- a/src/oidcMiddleware.js +++ b/src/oidcMiddleware.js @@ -22,7 +22,7 @@ export function getUserSuccessCallback(next, userManager, user, triggerAuthFlow, if (triggerAuthFlow) { // IF: auth flow should be triggered userManager.signinRedirect({ data: { - redirectUrl: window.location.href + redirectUrl: window.location.pathname } }); } else { @@ -45,11 +45,11 @@ export function getUserErrorCallback(error) { // the middleware creator function export default function createOidcMiddleware(userManager, shouldValidate, triggerAuthFlow, callbackRoute) { - if (!userManager) { + if (!userManager || !userManager.getUser || !userManager.signinRedirect) { throw new Error('You must provide a user manager!'); } - if (!callbackRoute) { + if (typeof(callbackRoute) !== 'string') { throw new Error('You must provide the callback route!'); } diff --git a/tests/callbackComponent.test.js b/tests/callbackComponent.test.js index 4f4cf5c..e073b13 100644 --- a/tests/callbackComponent.test.js +++ b/tests/callbackComponent.test.js @@ -88,4 +88,15 @@ describe('', () => { expect(() => component.onRedirectError(error)).toThrow(/error/); expect(removeItemStub.calledWith(STORAGE_KEY)).toEqual(true); }); + + it('should call the signinSilentCallback with a route when it has been provided', () => { + const route = '/some/route'; + props = { ...props, route }; + component = new CallbackComponent(props); + component.context = Object.assign({}, { ...component.context }, { ...contextMock }); + + component.componentDidMount(); + + expect(signinRedirectCallbackStub.calledWith(route)).toEqual(true); + }); }); diff --git a/tests/oidcMiddleware.test.js b/tests/oidcMiddleware.test.js index 56b0d1f..f10cdcf 100644 --- a/tests/oidcMiddleware.test.js +++ b/tests/oidcMiddleware.test.js @@ -23,13 +23,12 @@ describe('createOidcMiddleware()', () => { let getStateStub; let action; let stateMock; - let href = 'http://some.url.com'; let pathname = '/callback'; let callbackRoute = '/callback'; beforeEach(() => { windowMock = { - location: { href, pathname } + location: { pathname } }; oldWindow = window; window = windowMock; @@ -97,6 +96,17 @@ describe('createOidcMiddleware()', () => { expect(nextFunction.length).toEqual(1); }); + it('should throw an error when no user manager has been provided', () => { + expect(() => {createOidcMiddleware(null, null, null, callbackRoute)}).toThrow(/You must provide a user manager!/); + expect(() => {createOidcMiddleware({}, null, null, callbackRoute)}).toThrow(/You must provide a user manager!/); + expect(() => {createOidcMiddleware('userManager', null, null, callbackRoute)}).toThrow(/You must provide a user manager!/); + }); + + it('should throw an error when no callback route has been provided', () => { + expect(() => {createOidcMiddleware(userManagerMock, null, null, null)}).toThrow(/You must provide the callback route!/); + expect(() => {createOidcMiddleware(userManagerMock, null, null, {})}).toThrow(/You must provide the callback route!/); + }); + it('should call the shouldValidate() function with the redux state and dispatched action', () => { const shouldValidate = sinon.stub(); createOidcMiddleware(userManagerMock, shouldValidate, null, callbackRoute)(storeMock)(nextStub)(action); @@ -178,7 +188,7 @@ describe('createOidcMiddleware()', () => { const result = getUserSuccessCallback(nextStub, userManagerMock, user, true, action); const stateData = { data: { - redirectUrl: href + redirectUrl: pathname } }; @@ -230,8 +240,7 @@ describe('createOidcMiddleware()', () => { windowMock = { location: { - pathname: '/someRoute', - href + pathname: '/someRoute' } }; window = windowMock;