summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pages/sessions/new/preserve_url_fragment.js
blob: e617fecaa0f659063b7b723e36d7fbd79fff64d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.
    const forms = document.querySelectorAll('#signin-container form');
    Array.prototype.forEach.call(forms, form => {
      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
    const anchors = document.querySelectorAll('#signin-container a.oauth-login');
    Array.prototype.forEach.call(anchors, anchor => {
      const newHref = mergeUrlParams(
        { redirect_fragment: normalFragment },
        anchor.getAttribute('href'),
      );
      anchor.setAttribute('href', newHref);
    });
  }
}