2019-03-02 22:35:43 +05:30
|
|
|
import { mergeUrlParams, setUrlFragment } from '~/lib/utils/url_utility';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure the given URL fragment is preserved by appending it to sign-in/sign-up form actions and
|
|
|
|
* OAuth/SAML login links.
|
|
|
|
*
|
|
|
|
* @param fragment {string} - url fragment to be preserved
|
|
|
|
*/
|
|
|
|
export default function preserveUrlFragment(fragment = '') {
|
|
|
|
if (fragment) {
|
|
|
|
const normalFragment = fragment.replace(/^#/, '');
|
|
|
|
|
|
|
|
// Append the fragment to all sign-in/sign-up form actions so it is preserved when the user is
|
|
|
|
// eventually redirected back to the originally requested URL.
|
2020-06-23 00:09:42 +05:30
|
|
|
const forms = document.querySelectorAll('#signin-container .tab-content form');
|
2021-03-08 18:12:59 +05:30
|
|
|
Array.prototype.forEach.call(forms, (form) => {
|
2019-03-02 22:35:43 +05:30
|
|
|
const actionWithFragment = setUrlFragment(form.getAttribute('action'), `#${normalFragment}`);
|
|
|
|
form.setAttribute('action', actionWithFragment);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Append a redirect_fragment query param to all oauth provider links. The redirect_fragment
|
|
|
|
// query param will be available in the omniauth callback upon successful authentication
|
2020-06-23 00:09:42 +05:30
|
|
|
const oauthForms = document.querySelectorAll('#signin-container .omniauth-container form');
|
2021-03-08 18:12:59 +05:30
|
|
|
Array.prototype.forEach.call(oauthForms, (oauthForm) => {
|
2019-03-02 22:35:43 +05:30
|
|
|
const newHref = mergeUrlParams(
|
|
|
|
{ redirect_fragment: normalFragment },
|
2020-06-23 00:09:42 +05:30
|
|
|
oauthForm.getAttribute('action'),
|
2019-03-02 22:35:43 +05:30
|
|
|
);
|
2020-06-23 00:09:42 +05:30
|
|
|
oauthForm.setAttribute('action', newHref);
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|