-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): removed auth context flow in favor of redux;
- fixed avatar placeholders; - adjusted profile requests not to fetch own data - overall code cleanup
- Loading branch information
1 parent
060f80a
commit dbbc01f
Showing
25 changed files
with
289 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
// src/components/ProtectedRoute.js | ||
import React from 'react'; | ||
import { Navigate } from 'react-router-dom'; | ||
import { useAuth } from '../lib/contexts/AuthContext'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
const ProtectedRoute = ({ children }) => { | ||
const { token } = useAuth() | ||
|
||
const { token } = useSelector((state) => state.auth); | ||
|
||
// return children | ||
return token ? children : <Navigate to="/" />; | ||
}; | ||
|
||
export default ProtectedRoute; | ||
export default ProtectedRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; | ||
import { API_ENDPOINT } from '../../config'; | ||
|
||
// Async thunk for fetching user data | ||
export const fetchUserData = createAsyncThunk( | ||
'user/fetchUserData', | ||
async (token, { rejectWithValue }) => { | ||
try { | ||
const response = await fetch(`${API_ENDPOINT}/users/me`, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error( | ||
`Failed to fetch user data. Status: ${response.status}` | ||
); | ||
} | ||
|
||
const user = await response.json(); | ||
return user; | ||
} catch (error) { | ||
return rejectWithValue(error.message); | ||
} | ||
} | ||
); | ||
|
||
const userSlice = createSlice({ | ||
name: 'auth', | ||
initialState: { | ||
user: JSON.parse(localStorage.getItem('user')) || null, | ||
token: localStorage.getItem('token') || null, | ||
isLoading: false, | ||
error: null, | ||
}, | ||
reducers: { | ||
setToken: (state, action) => { | ||
state.token = action.payload; | ||
localStorage.setItem('token', action.payload); | ||
}, | ||
clearToken: (state) => { | ||
state.token = null; | ||
localStorage.removeItem('token'); | ||
}, | ||
updateUser: (state, action) => { | ||
state.user = action.payload; | ||
if (action.payload) { | ||
localStorage.setItem('user', JSON.stringify(action.payload)); | ||
} else { | ||
localStorage.removeItem('user'); | ||
} | ||
}, | ||
}, | ||
extraReducers: (builder) => { | ||
builder | ||
.addCase(fetchUserData.pending, (state) => { | ||
state.isLoading = true; | ||
state.error = null; | ||
}) | ||
.addCase(fetchUserData.fulfilled, (state, action) => { | ||
state.isLoading = false; | ||
state.user = action.payload; | ||
localStorage.setItem('user', JSON.stringify(action.payload)); | ||
}) | ||
.addCase(fetchUserData.rejected, (state, action) => { | ||
state.isLoading = false; | ||
state.error = action.payload; | ||
state.user = null; | ||
localStorage.removeItem('user'); | ||
}); | ||
}, | ||
}); | ||
|
||
export const { setToken, clearToken, updateUser } = userSlice.actions; | ||
|
||
export default userSlice.reducer; |
Oops, something went wrong.