-
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.
feat: Add OpenClassrooms-Student-Center#3 (Logout) + refactor NavBar …
…+ add Redux (store + api thunk + userSlice), implement PrivateRoute
- Loading branch information
Showing
11 changed files
with
215 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { Navigate } from 'react-router-dom'; | ||
import PropTypes from 'prop-types'; | ||
|
||
// PrivateRoute component used to protect routes that require authentication | ||
const PrivateRoute = ({ component: Component }) => { | ||
const isAuthenticated = useSelector((state) => state.auth.isAuthenticated); | ||
|
||
return isAuthenticated ? <Component /> : <Navigate to='/login' />; | ||
}; | ||
|
||
PrivateRoute.propTypes = { | ||
component: PropTypes.elementType.isRequired, | ||
}; | ||
|
||
export default PrivateRoute; |
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,9 +1,13 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom/client' | ||
import App from './App.jsx' | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import App from './App.jsx'; | ||
import { Provider } from 'react-redux'; | ||
import { store } from './service/store.js'; | ||
|
||
ReactDOM.createRoot(document.getElementById('root')).render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
) | ||
<Provider store={store}> | ||
<App /> | ||
</Provider> | ||
</React.StrictMode> | ||
); |
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,17 @@ | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import authSlice from '../service/user/userSlice'; | ||
|
||
// The value of isAuthenticated is determined by | ||
// whether there is a 'token' item in the local storage. | ||
const preloadedState = { | ||
auth: { | ||
isAuthenticated: !!localStorage.getItem('token'), | ||
}, | ||
}; | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
auth: authSlice, | ||
}, | ||
preloadedState, | ||
}); |
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,24 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
|
||
// This async thunk dispatches actions to authenticate a user. | ||
export const authenticate = createAsyncThunk( | ||
'user/authenticate', | ||
async ({ email, password }) => { | ||
const response = await fetch('http://localhost:3001/api/v1/user/login', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ email, password }), | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
if (response.ok) { | ||
localStorage.setItem('token', data.body.token); | ||
console.log(data.message); | ||
return data; | ||
} else { | ||
window.alert(data.message); | ||
throw new Error(data.message); | ||
} | ||
} | ||
); |
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,21 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { authenticate } from './userApi'; | ||
|
||
// The authSlice reducer manages the state of the user's authentication status. | ||
const authSlice = createSlice({ | ||
name: 'auth', | ||
initialState: { isAuthenticated: false }, | ||
reducers: {}, | ||
// The authenticate.fulfilled action changes the value of isAuthenticated to true. | ||
extraReducers: (builder) => { | ||
builder | ||
.addCase(authenticate.fulfilled, (state) => { | ||
state.isAuthenticated = true; | ||
}) | ||
.addCase(authenticate.rejected, (state) => { | ||
state.isAuthenticated = false; | ||
}); | ||
}, | ||
}); | ||
|
||
export default authSlice.reducer; |