Skip to content

Instantly share code, notes, and snippets.

@sardeie
Forked from dsharrison/TransferSession.page
Created June 6, 2020 22:27
Show Gist options
  • Select an option

  • Save sardeie/481c48e7ff9fdb53f61a9d21f4f69961 to your computer and use it in GitHub Desktop.

Select an option

Save sardeie/481c48e7ff9fdb53f61a9d21f4f69961 to your computer and use it in GitHub Desktop.

Revisions

  1. @dsharrison dsharrison created this gist Feb 27, 2017.
    9 changes: 9 additions & 0 deletions TransferSession.page
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <apex:page showHeader="false" sidebar="false">

    <!--
    Replace <your_resource> with the name of the static resource with
    your application files in it. This assumes that you are using a zipped static
    resource with your javascript files in a 'js/' folder.
    -->
    <apex:includeScript value="{!URLFOR($Resource.<your_resource>, '/js/transfer-session.js')}" />
    </apex:page>
    117 changes: 117 additions & 0 deletions transfer-session.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,117 @@
    /**
    * Transfer cookies between Lightning (with Locker Service) and Visualforce. Cookies
    * must be set as URL parameters and the "nextURL" parameter must be supplied.
    * Cookies will be set for apex using the apex__ prefix and will be set for
    * Lightning (with Locker Service) using the LSKey[<namespace>] prefix. Note that
    * all cookies will be set as session cookies. If cookies should be given an
    * expiration, the next page you navigate to should adjust the cookie expiration.
    */
    (function() {

    /**
    * The namespace for your application. If this is not in a package, set the
    * namespace to "c". Note that this is case sensitive.
    * @type {String}
    */
    var namespace = '<YOUR_NAMESPACE>';

    /**
    * Create a cookie for the given name, value, and expiration in days.
    * @param {String} name The name of the cookie to set.
    * @param {String} value The value of the cookie to set.
    * @param {Integer} days The number of days the cookie is good for. If not
    * supplied a session cookie will be set.
    */
    function createCookie(name, value, days) {
    var expires;

    if(days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
    }
    else {
    expires = "";
    }

    // Note: the name would usually be escaped when setting a cookie but
    // Locker Service uses braces in the cookie name.
    document.cookie = name + "=" + escape(value) + expires + "; path=/";
    }

    /**
    * Get the query parameters for the current url as an object.
    * @return {Object} An object with a key for each query parameter.
    */
    function getQueryParams() {
    var qs = window.location.search.split('+').join(' ');
    var params = {};
    var tokens;
    var re = /[?&]?([^=]+)=([^&]*)/g;

    while((tokens = re.exec(qs)) !== null) {
    params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
    }

    return params;
    }

    /**
    * Set cookies for both Apex and Visualforce for the given name and value.
    * @param {String} name The cookie name.
    * @param {String} value The cookie value.
    */
    function setCookies(name, value) {
    createCookie(getApexCookieName(name), value, null);
    createCookie(getLightningCookieName(name), value, null);
    }

    /**
    * Get the apex specific cookie name.
    * @param {String} name The generic cookie name.
    * @return {String} The name of the cookie as handled in Apex.
    */
    function getApexCookieName(name) {
    return 'apex__' + name;
    }

    /**
    * Get the Lightning (Locker Service) specific cookie name.
    * @param {String} name The generic cookie name.
    * @return {String} The name of the cookie as handled in Locker Service.
    */
    function getLightningCookieName(name) {
    return 'LSKey[' + namespace + ']' + name;
    }

    /**
    * Transfer all url parameters to Apex and Lightning (with Locker Service)
    * cookies. This will update window.location to nextURL after all cookies have
    * been set.
    * @throws An exception if the "nextURL" query parameter is not present on
    * the page.
    */
    function doTransfer() {

    var params = getQueryParams();
    var nextURL = params.nextURL;
    delete params.nextURL;

    if(!nextURL) {
    throw 'No next page found. Please ensure the nextURL parameter is set in the URL. Note that this is case sensitive.';
    }

    for(var key in params) {
    if(params.hasOwnProperty(key)) {
    setCookies(key, params[key]);
    }
    }

    window.location = nextURL;
    }

    // Run the transfer which will redirect the user after updating all
    // cookie values.
    doTransfer();

    })();