Results 1 to 3 of 3

Thread: [RESOLVED] localstorage Issue Opening Hyperlink

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,802

    Resolved [RESOLVED] localstorage Issue Opening Hyperlink

    I am using localStorage or sessionStorage to persist a JWT token when logging in. localStorage is used when the "remember me" is checked otherwise it uses session storage.

    This is what I'm doing to persist the storage:
    Code:
    $.post(`${utility.getAjaxPrefix}/server/api/auth/login.php`, model)
        .then(results => {
            if (model.RememberMe) {	
                localStorage.jwt = results;	
            } else {
                sessionStorage.setItem('jwt', results);
            }	
            window.location.href = `${utility.getAjaxPrefix}pages/login.html`;
        })
    And this is what I'm doing to get the storage:
    Code:
    utility.getJsonWebToken = () => {
        const key = 'jwt';
        return localStorage.getItem(key) || sessionStorage.getItem(key);
    }
    This works fine for nearly everything, but recently I did have an issue. I have email notifications sent when records are created and at the end there is an anchor tag with the href set to view the record in the website.

    If I open this hyperlink in a browser based email client then I can get the JWT just fine. However, if I open the hyperlink from an email client application outside of the browser then it cannot access the JWT and so I get redirected to the login screen.

    A workaround I have right now is for users to copy/paste the link into their browser, but ideally I would rather than just click the anchor and have the browser open it for them and recognize the JWT.

    Do any of y'all have an idea on how to resolve this?
    Last edited by dday9; Apr 17th, 2024 at 03:47 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,802

    Re: localstorage Issue Opening Hyperlink

    As a workaround, I think that I'm going to do the following:
    1. Append the person's email that I'm sending this to as a query string parameter in the href
    2. Check if the query string parameter exists when I hit my route and it cannot access localStorage/sessionStorage
    3. If so, then hit the server and ask if a token exists (I'm storing them in a database)
    4. If there is, then set the storage token, otherwise redirect to login
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,802

    Re: localstorage Issue Opening Hyperlink

    So this was a misunderstanding on my end. The issue was not presenting itself when the user was checking off "Remember Me?" which was using localStorage, rather the issue was happening when the user did not check it off which caused the application to use sessionStorage.

    This is as expected and so I'm not making any changes.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width