Rework JWT decoding for better security

This commit is contained in:
oitsjustjose 2022-04-15 22:38:22 -05:00
commit fb98314bf4
13 changed files with 185 additions and 113 deletions

View file

@ -0,0 +1,29 @@
import store from './store';
const decodeAndDispatch = async (token) => {
if (!token) return;
try {
const resp = await fetch(`/api/v1/user/decode?token=${token}`);
const data = await resp.json();
if (resp.ok) {
store.dispatch({ type: 'SET_USER', data: { ...data, token } });
} else {
if (resp.status === 410 && window.localStorage.getItem('auth-token')) {
window.localStorage.removeItem('auth-token');
}
store.dispatch({ type: 'SET_ALERT', data: { header: 'Login Is Invalid', content: resp.error } });
}
} catch (ex) {
store.dispatch({ type: 'SET_ALERT', data: { header: 'Login Is Invalid', content: ex } });
}
};
export const decodeFromToken = async (token) => {
decodeAndDispatch(token);
};
export const decodeFromLocalStorage = async () => {
decodeAndDispatch(window.localStorage.getItem('auth-token'));
};