generated from goitacademy/react-homework-template
-
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.
- Loading branch information
1 parent
39dc38e
commit 12df7c9
Showing
3 changed files
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Navigate } from 'react-router-dom'; | ||
import { useSelector } from 'react-redux'; | ||
import { | ||
selectIsLoggedIn, | ||
selectIsRefreshing, | ||
} from '../../redux/auth/selectors'; | ||
|
||
const AuthRoute = ({ component: Component, redirectTo = '/' }) => { | ||
const isLoggedIn = useSelector(selectIsLoggedIn); | ||
const isRefreshing = useSelector(selectIsRefreshing); | ||
|
||
const shouldRedirect = !isLoggedIn && !isRefreshing; | ||
return shouldRedirect ? <Navigate to={redirectTo} /> : <Component />; | ||
}; | ||
|
||
export default AuthRoute; |
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,11 @@ | ||
import { Navigate } from 'react-router-dom'; | ||
import { useSelector } from 'react-redux'; | ||
import { selectIsLoggedIn } from '../../redux/auth/selectors'; | ||
|
||
const GuestRoute = ({ component: Component, redirectTo = '/' }) => { | ||
const isLoggedIn = useSelector(selectIsLoggedIn); | ||
|
||
return isLoggedIn ? <Navigate to={redirectTo} /> : <Component />; | ||
}; | ||
|
||
export default GuestRoute; |
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,46 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { register, logIn, logOut, refreshUser } from './operations'; | ||
|
||
const initialState = { | ||
user: { name: null, email: null }, | ||
token: null, | ||
isLoggedIn: false, | ||
isRefreshing: false, | ||
}; | ||
|
||
const authSlice = createSlice({ | ||
name: 'auth', | ||
initialState, | ||
reducers: {}, | ||
extraReducers: builder => { | ||
builder | ||
.addCase(register.fulfilled, (state, action) => { | ||
state.user = action.payload.user; | ||
state.token = action.payload.token; | ||
state.isLoggedIn = true; | ||
}) | ||
.addCase(logIn.fulfilled, (state, action) => { | ||
state.user = action.payload.user; | ||
state.token = action.payload.token; | ||
state.isLoggedIn = true; | ||
}) | ||
.addCase(logOut.fulfilled, (state, action) => { | ||
state.user = action.payload.user; | ||
state.token = action.payload.token; | ||
state.isLoggedIn = false; | ||
}) | ||
.addCase(refreshUser.pending, state => { | ||
state.isRefreshing = true; | ||
}) | ||
.addCase(refreshUser.fulfilled, (state, action) => { | ||
state.user = action.payload; | ||
state.isLoggedIn = true; | ||
state.isRefreshing = false; | ||
}) | ||
.addCase(refreshUser.rejected, state => { | ||
state.isRefreshing = false; | ||
}); | ||
}, | ||
}); | ||
|
||
export const authReducer = authSlice.reducer; |