Remove sensitive data from url, but keep state on refresh #2398
-
Consider having clicked a password forgotten url from a mail, with an unique hash. With plain JavaScript I would do something like this: let hash = location.pathname.split('/')[2];
if (hash) {
history.replaceState({
hash,
}, '', '/password-reset');
return;
}
hash = history.state.hash;
if (!hash) {
console.warn('Invalid hash');
return;
}
console.log('Hash is', hash);
// prints t7Usw3hPpQ The main advantage of this is, if the user (accidentally) refreshes the page, the hash is preserved. While I can easily remove the hash from the url using vue-router on mount, after refresh the state from both vue-router and History API do not include the state I provided. I there currently any functionality to mimic this behavior? Or should I do a feature request so router will somehow preserve the original History API state? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Apparently there is a {
path: '/password/:hash',
name: 'password-redirect',
redirect: (to: RouteLocationGeneric): RouteLocationRaw => ({
name: 'password',
state: {
hash: to.params.hash,
},
}),
},
{
path: '/password',
name: 'password',
component: Password,
} In the const hash = history.state.hash?.toString();
console.log(hash); |
Beta Was this translation helpful? Give feedback.
Apparently there is a
state
property to attach metadata to the History API, so something like this works perfectly:In the
Password
component, thehash
property in the state will be present, even on refresh: