From beac574c12821067008c73f44044cf2c4eb93e6b Mon Sep 17 00:00:00 2001 From: Waheed Ahmed Date: Thu, 18 Mar 2021 19:18:59 +0500 Subject: [PATCH] Fix enrolment through SSO login/register. (#211) VAN-415 --- src/data/utils/dataUtils.js | 15 +++++-- src/data/utils/index.js | 3 +- src/login/LoginPage.jsx | 47 +++++++++----------- src/login/tests/LoginPage.test.jsx | 8 ++-- src/register/RegistrationPage.jsx | 23 ++++------ src/register/tests/RegistrationPage.test.jsx | 6 +-- 6 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/data/utils/dataUtils.js b/src/data/utils/dataUtils.js index 0963afa79b..7170990bd5 100644 --- a/src/data/utils/dataUtils.js +++ b/src/data/utils/dataUtils.js @@ -29,11 +29,12 @@ export const getTpaProvider = (tpaHintProvider, primaryProviders, secondaryProvi return tpaProvider; }; -export const processTpaHintURL = (params) => { +export const getTpaHint = () => { + const params = QueryString.parse(window.location.search); let tpaHint = null; - tpaHint = params.get('tpa_hint'); + tpaHint = params.tpa_hint; if (!tpaHint) { - const next = params.get('next'); + const { next } = params; if (next) { const index = next.indexOf('tpa_hint='); if (index !== -1) { @@ -55,7 +56,7 @@ export const updatePathWithQueryParams = (path) => { }; export const getAllPossibleQueryParam = () => { - const urlParams = QueryString.parse(document.location.search); + const urlParams = QueryString.parse(window.location.search); const params = {}; Object.entries(urlParams).forEach(([key, value]) => { if (AUTH_PARAMS.indexOf(key) > -1) { @@ -65,3 +66,9 @@ export const getAllPossibleQueryParam = () => { return params; }; + +export const getActivationStatus = () => { + const params = QueryString.parse(window.location.search); + + return params.account_activation_status; +}; diff --git a/src/data/utils/index.js b/src/data/utils/index.js index 6719e5ef40..261839a22d 100644 --- a/src/data/utils/index.js +++ b/src/data/utils/index.js @@ -1,8 +1,9 @@ export { default, getTpaProvider, - processTpaHintURL, + getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam, + getActivationStatus, } from './dataUtils'; export { default as AsyncActionType } from './reduxUtils'; diff --git a/src/login/LoginPage.jsx b/src/login/LoginPage.jsx index fbc44f0f66..d1e2bc7dcb 100644 --- a/src/login/LoginPage.jsx +++ b/src/login/LoginPage.jsx @@ -29,11 +29,11 @@ import { InstitutionLogistration, AuthnValidationFormGroup, } from '../common-components'; import { - DEFAULT_REDIRECT_URL, DEFAULT_STATE, LOGIN_PAGE, REGISTER_PAGE, ENTERPRISE_LOGIN_URL, PENDING_STATE, + DEFAULT_STATE, LOGIN_PAGE, REGISTER_PAGE, ENTERPRISE_LOGIN_URL, PENDING_STATE, } from '../data/constants'; import { forgotPasswordResultSelector } from '../forgot-password'; import { - getTpaProvider, processTpaHintURL, updatePathWithQueryParams, getAllPossibleQueryParam, + getTpaProvider, getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam, getActivationStatus, } from '../data/utils'; class LoginPage extends React.Component { @@ -51,17 +51,15 @@ class LoginPage extends React.Component { institutionLogin: false, isSubmitted: false, }; + this.queryParams = getAllPossibleQueryParam(); + this.tpaHint = getTpaHint(); } componentDidMount() { - const params = (new URL(document.location)).searchParams; - const payload = { - redirect_to: params.get('next') || DEFAULT_REDIRECT_URL, - }; + const payload = { ...this.queryParams }; - const tpaHint = processTpaHintURL(params); - if (tpaHint) { - payload.tpa_hint = tpaHint; + if (this.tpaHint) { + payload.tpa_hint = this.tpaHint; } this.props.getThirdPartyAuthContext(payload); } @@ -92,10 +90,9 @@ class LoginPage extends React.Component { return; } - let payload = { email, password }; - const postParams = getAllPossibleQueryParam(); - - payload = { ...payload, ...postParams }; + const payload = { + email, password, ...this.queryParams, + }; this.props.loginRequest(payload); } @@ -148,16 +145,17 @@ class LoginPage extends React.Component { } return thirdPartyComponent; } - renderForm(params, + renderForm( currentProvider, providers, secondaryProviders, thirdPartyAuthContext, thirdPartyAuthApiStatus, submitState, - intl) { + intl, + ) { const { email, errors, password } = this.state; - const activationMsgType = params.get('account_activation_status'); + const activationMsgType = getActivationStatus(); if (this.state.institutionLogin) { return ( ; } - const provider = getTpaProvider(tpaHint, providers, secondaryProviders); - return provider ? () : this.renderForm(params, + const provider = getTpaProvider(this.tpaHint, providers, secondaryProviders); + return provider ? () : this.renderForm( currentProvider, providers, secondaryProviders, thirdPartyAuthContext, thirdPartyAuthApiStatus, submitState, - intl); + intl, + ); } - return this.renderForm(params, + return this.renderForm( currentProvider, providers, secondaryProviders, thirdPartyAuthContext, thirdPartyAuthApiStatus, submitState, - intl); + intl, + ); } } diff --git a/src/login/tests/LoginPage.test.jsx b/src/login/tests/LoginPage.test.jsx index 7e7295d2da..844a9bdce8 100644 --- a/src/login/tests/LoginPage.test.jsx +++ b/src/login/tests/LoginPage.test.jsx @@ -133,7 +133,7 @@ describe('LoginPage', () => { it('should show account activation message', () => { delete window.location; - window.location = { href: getConfig().BASE_URL.concat('/login?account_activation_status=info') }; + window.location = { href: getConfig().BASE_URL.concat('/login'), search: '?account_activation_status=info' }; const expectedMessage = 'This account has already been activated.'; @@ -429,7 +429,7 @@ describe('LoginPage', () => { }); delete window.location; - window.location = { href: getConfig().BASE_URL.concat(`/login?next=/dashboard&tpa_hint=${appleProvider.id}`) }; + window.location = { href: getConfig().BASE_URL.concat('/login'), search: `?next=/dashboard&tpa_hint=${appleProvider.id}` }; appleProvider.iconImage = null; const loginPage = mount(reduxWrapper()); @@ -451,7 +451,7 @@ describe('LoginPage', () => { }); delete window.location; - window.location = { href: getConfig().BASE_URL.concat('/login?next=/dashboard&tpa_hint=invalid') }; + window.location = { href: getConfig().BASE_URL.concat('/login'), search: '?next=/dashboard&tpa_hint=invalid' }; appleProvider.iconImage = null; const loginPage = mount(reduxWrapper()); @@ -473,7 +473,7 @@ describe('LoginPage', () => { }); delete window.location; - window.location = { href: getConfig().BASE_URL.concat(`/login?next=/dashboard&tpa_hint=${secondaryProviders.id}`) }; + window.location = { href: getConfig().BASE_URL.concat('/login'), search: `?next=/dashboard&tpa_hint=${secondaryProviders.id}` }; secondaryProviders.iconImage = null; const loginPage = mount(reduxWrapper()); diff --git a/src/register/RegistrationPage.jsx b/src/register/RegistrationPage.jsx index a7ffbac95e..212deed216 100644 --- a/src/register/RegistrationPage.jsx +++ b/src/register/RegistrationPage.jsx @@ -29,10 +29,10 @@ import { getThirdPartyAuthContext } from '../common-components/data/actions'; import { thirdPartyAuthContextSelector } from '../common-components/data/selectors'; import EnterpriseSSO from '../common-components/EnterpriseSSO'; import { - DEFAULT_REDIRECT_URL, DEFAULT_STATE, LOGIN_PAGE, PENDING_STATE, REGISTER_PAGE, + DEFAULT_STATE, LOGIN_PAGE, PENDING_STATE, REGISTER_PAGE, } from '../data/constants'; import { - getTpaProvider, processTpaHintURL, updatePathWithQueryParams, getAllPossibleQueryParam, + getTpaProvider, getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam, } from '../data/utils'; class RegistrationPage extends React.Component { @@ -41,6 +41,8 @@ class RegistrationPage extends React.Component { sendPageEvent('login_and_registration', 'register'); this.intl = props.intl; + this.queryParams = getAllPossibleQueryParam(); + this.tpaHint = getTpaHint(); this.state = { email: '', @@ -77,14 +79,10 @@ class RegistrationPage extends React.Component { } componentDidMount() { - const params = (new URL(document.location)).searchParams; - const payload = { - redirect_to: params.get('next') || DEFAULT_REDIRECT_URL, - }; + const payload = { ...this.queryParams }; - const tpaHint = processTpaHintURL(params); - if (tpaHint) { - payload.tpa_hint = tpaHint; + if (this.tpaHint) { + payload.tpa_hint = this.tpaHint; } this.props.getThirdPartyAuthContext(payload); } @@ -624,14 +622,11 @@ class RegistrationPage extends React.Component { currentProvider, finishAuthUrl, providers, secondaryProviders, } = this.props.thirdPartyAuthContext; - const params = (new URL(window.location.href)).searchParams; - const tpaHint = processTpaHintURL(params); - - if (tpaHint) { + if (this.tpaHint) { if (thirdPartyAuthApiStatus === PENDING_STATE) { return ; } - const provider = getTpaProvider(tpaHint, providers, secondaryProviders); + const provider = getTpaProvider(this.tpaHint, providers, secondaryProviders); return provider ? () : this.renderForm( currentProvider, diff --git a/src/register/tests/RegistrationPage.test.jsx b/src/register/tests/RegistrationPage.test.jsx index 97bade65ba..64bb94a74f 100644 --- a/src/register/tests/RegistrationPage.test.jsx +++ b/src/register/tests/RegistrationPage.test.jsx @@ -654,7 +654,7 @@ describe('RegistrationPageTests', () => { }); delete window.location; - window.location = { href: getConfig().BASE_URL.concat(`/login?next=/dashboard&tpa_hint=${appleProvider.id}`) }; + window.location = { href: getConfig().BASE_URL.concat('/login'), search: `?next=/dashboard&tpa_hint=${appleProvider.id}` }; appleProvider.iconImage = null; const registerPage = mount(reduxWrapper()); @@ -676,7 +676,7 @@ describe('RegistrationPageTests', () => { }); delete window.location; - window.location = { href: getConfig().BASE_URL.concat('/login?next=/dashboard&tpa_hint=invalid') }; + window.location = { href: getConfig().BASE_URL.concat('/login'), search: '?next=/dashboard&tpa_hint=invalid' }; appleProvider.iconImage = null; const registerPage = mount(reduxWrapper()); @@ -698,7 +698,7 @@ describe('RegistrationPageTests', () => { }); delete window.location; - window.location = { href: getConfig().BASE_URL.concat(`/login?next=/dashboard&tpa_hint=${secondaryProviders.id}`) }; + window.location = { href: getConfig().BASE_URL.concat('/login'), search: `?next=/dashboard&tpa_hint=${secondaryProviders.id}` }; secondaryProviders.iconImage = null; const registerPage = mount(reduxWrapper());