<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Connecting to the existing enigma.js session in a browser in Integration, Extension &amp; APIs</title>
    <link>https://community.qlik.com/t5/Integration-Extension-APIs/Connecting-to-the-existing-enigma-js-session-in-a-browser/m-p/1577182#M10468</link>
    <description>&lt;P&gt;Hi Qlik Experts&lt;/P&gt;&lt;P&gt;I am working on a mashup application, where I connect to Qlik Engine using enigma.js library.&lt;/P&gt;&lt;P&gt;To authenticate the user I use a custom-build backend service, which authenticates against LDAP and requests a ticket from proxy service.&lt;/P&gt;&lt;P&gt;My code looks something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const enigmaConfig = {
    host: 'server',
    port: 443,
    secure: true,
    prefix: 'some',
    appId: 'appId',
    options: {
        qpsPort: 4243,
        cookie: 'QSCookie',
    },
};

const enigmaConnect({sessionId, qlikTicket}) {
    const config = { ...enigmaConfig };
    if (qlikTicket) {
        config.urlParams = {qlikTicket};
    }
    if (sessionId) {
        config.identity = sessionId;
    }
    const url = SenseUtilities.buildUrl(config);

    try {
        const global = enigma.create({
            schema,
            url: url,
        });
        const session = await global.open();
        const doc = await session.openDoc(config.appId);
        const sessions = await dispatch('getSessions'); // this function will return the list of user sessions by making a request to the back-end service, which will load them from proxy service
        const latestSession = sessions[sessions.length - 1];
        latestSession.qlikTicket = qlikTicket;
        document.cookie = `${enigmaConfig.options.cookie}=${btoa(JSON.stringify(latestSession))};secure=true`;

        return true;
    } catch (err) {
        console.log(err);
        document.cookie = `${enigmaConfig.options.cookie}=;`;
        return false;
    }
}

const open() {
    let cookieSession = null;
    let connected = false;
    try {
        cookieSession = JSON.parse(atob(parseSession(document.cookie)));
    } catch (err) {
    }

    const session = {};
    if (cookieSession) {
        session.sessionId = cookieSession.SessionId;
        // session.qlikTicket = cookieSession.qlikTicket; // here I can provide the old ticket
    } else {
        session.qlikTicket = await dispatch('getTicket');
    }
    connected = await dispatch('enigmaOpen', session);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So idea is when the user opens the connection for first time to capture the session id and store it in a cookie. Then if the user refreshes the browser while the old session is still active to reconnect to the same session by providing the identity attribute in the URL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that if I provide sessionId as identity attribute and do not provide a new qlikTicket attribute connecting to the enigma second time or provide the old ticket (which I can store in the cookie), the connection fails. But if I provide the identity with the existing sessionId and a new qlikTicket , then a session with the new sessionId is created and the old one is being removed. If I refresh the browser multiple times, then I get an error related to too many active sessions.&lt;/P&gt;&lt;P&gt;I can't use certificates in the connection parameters (or do not want to use them, because this is a front-end application which runs in the browser.&lt;/P&gt;&lt;P&gt;Could someone help me to connect to the same session? Do I miss some parameter?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Darius&lt;/P&gt;</description>
    <pubDate>Sat, 16 Nov 2024 20:55:38 GMT</pubDate>
    <dc:creator>d_pranskus</dc:creator>
    <dc:date>2024-11-16T20:55:38Z</dc:date>
    <item>
      <title>Connecting to the existing enigma.js session in a browser</title>
      <link>https://community.qlik.com/t5/Integration-Extension-APIs/Connecting-to-the-existing-enigma-js-session-in-a-browser/m-p/1577182#M10468</link>
      <description>&lt;P&gt;Hi Qlik Experts&lt;/P&gt;&lt;P&gt;I am working on a mashup application, where I connect to Qlik Engine using enigma.js library.&lt;/P&gt;&lt;P&gt;To authenticate the user I use a custom-build backend service, which authenticates against LDAP and requests a ticket from proxy service.&lt;/P&gt;&lt;P&gt;My code looks something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const enigmaConfig = {
    host: 'server',
    port: 443,
    secure: true,
    prefix: 'some',
    appId: 'appId',
    options: {
        qpsPort: 4243,
        cookie: 'QSCookie',
    },
};

const enigmaConnect({sessionId, qlikTicket}) {
    const config = { ...enigmaConfig };
    if (qlikTicket) {
        config.urlParams = {qlikTicket};
    }
    if (sessionId) {
        config.identity = sessionId;
    }
    const url = SenseUtilities.buildUrl(config);

    try {
        const global = enigma.create({
            schema,
            url: url,
        });
        const session = await global.open();
        const doc = await session.openDoc(config.appId);
        const sessions = await dispatch('getSessions'); // this function will return the list of user sessions by making a request to the back-end service, which will load them from proxy service
        const latestSession = sessions[sessions.length - 1];
        latestSession.qlikTicket = qlikTicket;
        document.cookie = `${enigmaConfig.options.cookie}=${btoa(JSON.stringify(latestSession))};secure=true`;

        return true;
    } catch (err) {
        console.log(err);
        document.cookie = `${enigmaConfig.options.cookie}=;`;
        return false;
    }
}

const open() {
    let cookieSession = null;
    let connected = false;
    try {
        cookieSession = JSON.parse(atob(parseSession(document.cookie)));
    } catch (err) {
    }

    const session = {};
    if (cookieSession) {
        session.sessionId = cookieSession.SessionId;
        // session.qlikTicket = cookieSession.qlikTicket; // here I can provide the old ticket
    } else {
        session.qlikTicket = await dispatch('getTicket');
    }
    connected = await dispatch('enigmaOpen', session);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So idea is when the user opens the connection for first time to capture the session id and store it in a cookie. Then if the user refreshes the browser while the old session is still active to reconnect to the same session by providing the identity attribute in the URL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that if I provide sessionId as identity attribute and do not provide a new qlikTicket attribute connecting to the enigma second time or provide the old ticket (which I can store in the cookie), the connection fails. But if I provide the identity with the existing sessionId and a new qlikTicket , then a session with the new sessionId is created and the old one is being removed. If I refresh the browser multiple times, then I get an error related to too many active sessions.&lt;/P&gt;&lt;P&gt;I can't use certificates in the connection parameters (or do not want to use them, because this is a front-end application which runs in the browser.&lt;/P&gt;&lt;P&gt;Could someone help me to connect to the same session? Do I miss some parameter?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Darius&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2024 20:55:38 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Integration-Extension-APIs/Connecting-to-the-existing-enigma-js-session-in-a-browser/m-p/1577182#M10468</guid>
      <dc:creator>d_pranskus</dc:creator>
      <dc:date>2024-11-16T20:55:38Z</dc:date>
    </item>
  </channel>
</rss>

