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'));
|
||||
};
|
||||
|
|
@ -1,43 +1,29 @@
|
|||
import JwtDecode from 'jwt-decode';
|
||||
import moment from 'moment';
|
||||
|
||||
const getUserFromLocalStorage = () => {
|
||||
const u = JSON.parse(window.localStorage.getItem('redux-persistent'));
|
||||
if (!u) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (moment().isAfter(moment(u.expiresAt))) {
|
||||
window.localStorage.removeItem('redux-persistent');
|
||||
return null;
|
||||
}
|
||||
return u;
|
||||
};
|
||||
import { decodeFromLocalStorage, decodeFromToken } from './decode';
|
||||
|
||||
const defaultState = {
|
||||
user: getUserFromLocalStorage(),
|
||||
user: null,
|
||||
i18n: null,
|
||||
langs: null,
|
||||
alert: null,
|
||||
};
|
||||
|
||||
const decodeUser = (token) => {
|
||||
const user = JwtDecode(token);
|
||||
const userData = { ...user, token };
|
||||
window.localStorage.setItem('redux-persistent', JSON.stringify(userData));
|
||||
return userData;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line default-param-last
|
||||
export default (state = defaultState, action) => {
|
||||
switch (action.type) {
|
||||
case '@@INIT':
|
||||
decodeFromLocalStorage();
|
||||
return state;
|
||||
case 'SET_USER_TOKEN':
|
||||
window.localStorage.setItem('auth-token', action.token);
|
||||
decodeFromToken(action.token);
|
||||
return state;
|
||||
case 'SET_USER':
|
||||
return {
|
||||
...state,
|
||||
user: decodeUser(action.token),
|
||||
user: action.data,
|
||||
};
|
||||
case 'CLEAR_USER':
|
||||
window.localStorage.removeItem('redux-persistent');
|
||||
window.localStorage.removeItem('auth-token');
|
||||
return {
|
||||
...state,
|
||||
user: null,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,9 @@
|
|||
/* eslint-disable no-underscore-dangle */
|
||||
import { createStore } from 'redux';
|
||||
import Reducer from './reducers';
|
||||
|
||||
export default createStore(Reducer);
|
||||
export default createStore(
|
||||
Reducer,
|
||||
window.__REDUX_DEVTOOLS_EXTENSION__
|
||||
&& window.__REDUX_DEVTOOLS_EXTENSION__(),
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue