Session Cookie

Session Cookie

jpeprah
Shopify Partner
6 0 0

I want to create sometime like a session cookie for shopify website, when customer is log into account for 30 days and click on my account menu or link it will automaticaly direct them to their account page, after 30 days it should redirect them to the sign in page to establish a new session cookie for 30 days.  Please I will need help to accomplish this process.

Replies 7 (7)

BSSCommerce-TC
Shopify Partner
215 46 49

Hi @jpeprah ,

 

You can try using the code snippet below.

Step 1: Go to Online Store > Themes > edit code and find theme.liquid

Step 2: Insert  it just above the closing  </body> tag.

<script id="session-cookie">
document.addEventListener('DOMContentLoaded', function() {
    
    function getCookie(name) {
        let value = `; ${document.cookie}`;
        let parts = value.split(`; ${name}=`);
        if (parts.length === 2) return parts.pop().split(';').shift();
    }

    function setCookie(name, value, days) {
        let expires = '';
        if (days) {
            let date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = `; expires=${date.toUTCString()}`;
        }
        document.cookie = `${name}=${(value || '')}${expires}; path=/`;
    }

    const customerCookie = getCookie('customer');

    if (customerCookie) {
        const lastRedirectDate = getCookie('account_redirect_date');
        const now = new Date().getTime();
        const redirectDate = lastRedirectDate ? new Date(parseInt(lastRedirectDate)).getTime() : 0;
        const daysSinceRedirect = (now - redirectDate) / (1000 * 60 * 60 * 24);

        if (daysSinceRedirect < 30) {
            window.location.href = '/account';
        } else {
            document.cookie = 'customer=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
            setCookie('account_redirect_date', now, 30);
            window.location.href = '/account/login';
        }
    } else {
        document.cookie = 'account_redirect_date=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
    }
});
</script

Hope this can help you !

 

If our suggestions are useful, please let us know by giving it a like, marking it as a solution.


MIDA: Heatmap, Record & Replay |BLOOP Referral Program, Reward |

Need help from our expert? Kindly share your request with us via community@bsscommerce.com


BSS Commerce - Full-service eCommerce Agency
jpeprah
Shopify Partner
6 0 0

Thank you for your swift response, but unfortunately that solution did not work, I log in but did not remember the session cookie, I click on my account and instead of taking me to the account page, I was redirected to login again to login even though I I have already login.

jpeprah
Shopify Partner
6 0 0

Any help will be appreciated, I have been working on it for days.

jpeprah
Shopify Partner
6 0 0

I was able to figure this out. Thank you.

BSSCommerce-TC
Shopify Partner
215 46 49

Hi @jpeprah , 

 

You need to ensure that when you successfully log in, the customer's information is saved in a cookie.

If our suggestions are useful, please let us know by giving it a like, marking it as a solution.


MIDA: Heatmap, Record & Replay |BLOOP Referral Program, Reward |

Need help from our expert? Kindly share your request with us via community@bsscommerce.com


BSS Commerce - Full-service eCommerce Agency
jpeprah
Shopify Partner
6 0 0

Any how do I do that? I want  session information to be saved in a cookie for 30days so that customers don't have to log in every time. 

parvz_hek
Shopify Partner
1 0 0

How did you login? Through API?