<!DOCTYPE HTML>
<html>  
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!-- Start of LiveChat (www.livechatinc.com) code -->
<script>
    window.__lc = window.__lc || {};
    window.__lc.accessToken = "";
    window.__lc.license = 10146642;
    window.__lc.clientId = "7761e5df8494a735f5ff01a444eefc77"; //webchat
    window.__lc.entityId = "9d5d0a26-ba9d-4cdc-7a3f-c35a6cf469b3";
    window.__lc.custom_identity_provider = () => {
        const fetchLiveChatToken = () => {
            // fetch a token from LiveChat Accounts API here
            var fetchToken = new Promise( function(resolve, reject) {
                $.ajax("https://accounts.livechat.com/customer/token",{
                    'data': JSON.stringify({
                        "grant_type": "cookie",
                        "client_id": window.__lc.clientId,
                        // "entity_id": window.__lc.entityId,
                        "response_type": "token",
                        "license_id": window.__lc.license,
                        "redirect_uri": "https://windev888.com"     
                    }),
                    'type': 'POST',
                    'processData': false,
                    'contentType': 'application/json' //typically 'application/x-www-form-urlencoded', but the service you are calling may expect 'text/json'... check with the service to see what they expect as content-type in the HTTP header.
                }).done(function( data ) {
                    resolve(data);
                });
            });

            return fetchToken;
        }

        const identityTransfer1 = () => {
            // transfer identity
            var fetchTransfer = new Promise( function(resolve, reject) {
                $.ajax("https://accounts.livechat.com/customer/identity_transfer",{
                    'data': JSON.stringify({
                        "bearer_type": "customer",
                        "client_id": window.__lc.clientId,
                        "customer_id": window.__lc.customerId,
                    }), 
                    'headers': {
                        'Authorization':'Bearer ' + window.__lc.accessToken
                    },
                    'type': 'POST',
                    'processData': false,
                    'contentType': 'application/json' //typically 'application/x-www-form-urlencoded', but the service you are calling may expect 'text/json'... check with the service to see what they expect as content-type in the HTTP header.
                }).done(function( data ) {
                    window.__lc.indentityTransferToken = data.identity_transfer_token
                    resolve(data);
                });
            });

            return fetchTransfer;
        }

        const identityTransfer2 = () => {
            // transfer identity
            var fetchTransfer = new Promise( function(resolve, reject) {
                $.ajax("https://accounts.livechat.com/customer/token",{
                    'data': JSON.stringify({
                        "grant_type": "identity_token",
                        "client_id": window.__lc.clientId,
                        "response_type": "token",
                        "code": window.__lc.indentityTransferToken
                    }), 
                    'headers': {
                        'Authorization':'Bearer ' + window.__lc.accessToken
                    },
                    'type': 'POST',
                    'processData': false,
                    'contentType': 'application/json' //typically 'application/x-www-form-urlencoded', but the service you are calling may expect 'text/json'... check with the service to see what they expect as content-type in the HTTP header.
                }).done(function( data ) {
                    resolve(data);
                });
            });

            return fetchTransfer;
        }

        let tokenPromise = null
        const fetchToken = () => {
            tokenPromise = fetchLiveChatToken()
                .then(response => {
                    tokenPromise = null
                    console.log('fetchToken')
                    console.log(response)
                    // window.__lc.accessToken = response.access_token
                    // identityResponse1 = identityTransfer1()
                    //     .then(response => {
                    //         console.log('identityResponse1')
                    //         console.log(response)
                    //         identityResponse2 = identityTransfer2()
                    //             .then(response => {
                    //                 console.log('identityTransfer2')
                    //                 console.log(response)
                    //                 return {
                    //                     accessToken: window.__lc.accessToken = response.access_token,
                    //                     entityId: response.entity_id,
                    //                     expiresIn: response.expires_in * 1000,
                    //                     tokenType: response.token_type,
                    //                     creationDate: new Date().getTime(),
                    //                     licenseId: window.__lc.license,
                    //                 }
                    //             })
                    //     })
                    return {
                        accessToken: window.__lc.accessToken = response.access_token,
                        entityId: response.entity_id,
                        expiresIn: response.expires_in * 1000,
                        tokenType: response.token_type,
                        creationDate: new Date().getTime(),
                        licenseId: window.__lc.license,
                    }
                })
        }

        return {
            getToken: () => tokenPromise || fetchToken(),
            getFreshToken: () => tokenPromise || fetchToken(),
            hasToken: () => Promise.resolve(false),
            invalidate: () => Promise.resolve()
        }
    }
    ;(function(n,t,c){function i(n){return e._h?e._h.apply(null,n):e._q.push(n)}var e={_q:[],_h:null,_v:"2.0",on:function(){i(["on",c.call(arguments)])},once:function(){i(["once",c.call(arguments)])},off:function(){i(["off",c.call(arguments)])},get:function(){if(!e._h)throw new Error("[LiveChatWidget] You can't use getters before load.");return i(["get",c.call(arguments)])},call:function(){i(["call",c.call(arguments)])},init:function(){var n=t.createElement("script");n.async=!0,n.type="text/javascript",n.src="https://cdn.livechatinc.com/tracking.js",t.head.appendChild(n)}};!n.__lc.asyncInit&&e.init(),n.LiveChatWidget=n.LiveChatWidget||e}(window,document,[].slice))
</script>
<noscript><a href="https://www.livechatinc.com/chat-with/10146642/" rel="nofollow">Chat with us</a>, powered by <a href="https://www.livechatinc.com/?welcome" rel="noopener nofollow" target="_blank">LiveChat</a></noscript>
<!-- End of LiveChat code -->
</body>
</html>

Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.