Skip to content

Commit

Permalink
Fix enrolment through SSO login/register. (#211)
Browse files Browse the repository at this point in the history
VAN-415
  • Loading branch information
waheedahmed authored Mar 18, 2021
1 parent d861fac commit beac574
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
15 changes: 11 additions & 4 deletions src/data/utils/dataUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -65,3 +66,9 @@ export const getAllPossibleQueryParam = () => {

return params;
};

export const getActivationStatus = () => {
const params = QueryString.parse(window.location.search);

return params.account_activation_status;
};
3 changes: 2 additions & 1 deletion src/data/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export {
default,
getTpaProvider,
processTpaHintURL,
getTpaHint,
updatePathWithQueryParams,
getAllPossibleQueryParam,
getActivationStatus,
} from './dataUtils';
export { default as AsyncActionType } from './reduxUtils';
47 changes: 22 additions & 25 deletions src/login/LoginPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 (
<InstitutionLogistration
Expand Down Expand Up @@ -274,31 +272,30 @@ class LoginPage extends React.Component {
} = this.props;
const { currentProvider, 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 <Skeleton height={36} />;
}
const provider = getTpaProvider(tpaHint, providers, secondaryProviders);
return provider ? (<EnterpriseSSO provider={provider} intl={intl} />) : this.renderForm(params,
const provider = getTpaProvider(this.tpaHint, providers, secondaryProviders);
return provider ? (<EnterpriseSSO provider={provider} intl={intl} />) : 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,
);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/login/tests/LoginPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand Down Expand Up @@ -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(<IntlLoginPage {...props} />));
Expand All @@ -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(<IntlLoginPage {...props} />));
Expand All @@ -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(<IntlLoginPage {...props} />));
Expand Down
23 changes: 9 additions & 14 deletions src/register/RegistrationPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: '',
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 <Skeleton height={36} />;
}
const provider = getTpaProvider(tpaHint, providers, secondaryProviders);
const provider = getTpaProvider(this.tpaHint, providers, secondaryProviders);
return provider ? (<EnterpriseSSO provider={provider} intl={intl} />)
: this.renderForm(
currentProvider,
Expand Down
6 changes: 3 additions & 3 deletions src/register/tests/RegistrationPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<IntlRegistrationPage {...props} />));
Expand All @@ -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(<IntlRegistrationPage {...props} />));
Expand All @@ -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(<IntlRegistrationPage {...props} />));
Expand Down

0 comments on commit beac574

Please sign in to comment.