-
Apr 17th, 2024, 03:21 PM
#1
[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.
-
Apr 18th, 2024, 08:09 AM
#2
Re: localstorage Issue Opening Hyperlink
As a workaround, I think that I'm going to do the following:
- Append the person's email that I'm sending this to as a query string parameter in the href
- Check if the query string parameter exists when I hit my route and it cannot access localStorage/sessionStorage
- If so, then hit the server and ask if a token exists (I'm storing them in a database)
- If there is, then set the storage token, otherwise redirect to login
-
Apr 23rd, 2024, 11:33 AM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|