diff options
author | Sunil Shetye | 2025-07-13 13:43:30 +0530 |
---|---|---|
committer | Sunil Shetye | 2025-07-13 13:43:30 +0530 |
commit | 715b5f1809897541c0f78808b92e4f6cf6d9d644 (patch) | |
tree | cbeff97a7fd01ad291dc0c4a62e36bb08950286e | |
parent | 38f5f610bafa5c16be13eb522d426ae263d3ad71 (diff) | |
download | Common-Interface-Project-715b5f1809897541c0f78808b92e4f6cf6d9d644.tar.gz Common-Interface-Project-715b5f1809897541c0f78808b92e4f6cf6d9d644.tar.bz2 Common-Interface-Project-715b5f1809897541c0f78808b92e4f6cf6d9d644.zip |
use token from login directly
-rw-r--r-- | blocks/eda-frontend/src/App.js | 16 | ||||
-rw-r--r-- | blocks/eda-frontend/src/redux/authSlice.js | 13 |
2 files changed, 13 insertions, 16 deletions
diff --git a/blocks/eda-frontend/src/App.js b/blocks/eda-frontend/src/App.js index a1af8fcf..1ceed868 100644 --- a/blocks/eda-frontend/src/App.js +++ b/blocks/eda-frontend/src/App.js @@ -22,11 +22,6 @@ import api from './utils/Api' const PrivateRoute = ({ component: Component, ...rest }) => { const isAuthenticated = useSelector(state => state.auth.isAuthenticated) const isLoading = useSelector(state => state.auth.isLoading) - const dispatch = useDispatch() - - useEffect(() => { - dispatch(loadUser()) - }, [dispatch]) return ( <Route @@ -51,11 +46,6 @@ PrivateRoute.propTypes = { const PublicRoute = ({ component: Component, restricted, nav, ...rest }) => { const isAuthenticated = useSelector(state => state.auth.isAuthenticated) const isLoading = useSelector(state => state.auth.isLoading) - const dispatch = useDispatch() - - useEffect(() => { - dispatch(loadUser()) - }, [dispatch]) return ( <Route @@ -81,6 +71,8 @@ PublicRoute.propTypes = { } const App = () => { + const dispatch = useDispatch() + useEffect(() => { const initializeCsrf = async () => { try { @@ -93,6 +85,10 @@ const App = () => { initializeCsrf() }, []) + useEffect(() => { + dispatch(loadUser()) + }, [dispatch]) + return ( // Handles Routing for an application <HashRouter> diff --git a/blocks/eda-frontend/src/redux/authSlice.js b/blocks/eda-frontend/src/redux/authSlice.js index bb8e9644..9632b86c 100644 --- a/blocks/eda-frontend/src/redux/authSlice.js +++ b/blocks/eda-frontend/src/redux/authSlice.js @@ -17,9 +17,9 @@ const initialState = { // Api call for maintaining user login state throughout the application export const loadUser = createAsyncThunk( 'auth/loadUser', - async (_, { getState, rejectWithValue }) => { + async (tokenFromLogin, { getState, rejectWithValue }) => { // Get token from localstorage - const token = getState().auth.token + const token = tokenFromLogin || getState().auth.token if (!token) return rejectWithValue('No token found') // add headers @@ -70,16 +70,17 @@ export const login = createAsyncThunk( password }) if ([200, 201, 204].includes(res.status)) { - localStorage.setItem(tokenKey, res.data.auth_token) + const token = res.data.auth_token + localStorage.setItem(tokenKey, token) if (toUrl === '') { - dispatch(loadUser()) + dispatch(loadUser(token)) } else if (!allowedUrls.includes(toUrl)) { console.log('Not redirecting to', toUrl) - dispatch(loadUser()) + dispatch(loadUser(token)) } else { window.open(toUrl, '_self') } - return res.data.auth_token + return token } return loginError(res, rejectWithValue) |