Rework JWT decoding for better security
This commit is contained in:
parent
dd52d44cc2
commit
fb98314bf4
13 changed files with 185 additions and 113 deletions
29
frontend/src/redux/decode.js
Normal file
29
frontend/src/redux/decode.js
Normal 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'));
|
||||
};
|
||||
Loading…
Reference in a new issue