generated from goitacademy/react-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from 001elijah/auth
Auth add login,register,current,logout
- Loading branch information
Showing
16 changed files
with
327 additions
and
18 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
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,27 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { Navigate } from 'react-router'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { selectorIsAuth } from 'redux/Auth/authSelectors'; | ||
|
||
export const PrivateRoute = ({ component, redirectTo = '/' }) => { | ||
const isAuth = useSelector(selectorIsAuth); | ||
|
||
return isAuth ? component : <Navigate to={redirectTo} />; | ||
}; | ||
|
||
export const PublicRoute = ({ component, redirectTo = '/home' }) => { | ||
const isAuth = useSelector(selectorIsAuth); | ||
|
||
return !isAuth ? component : <Navigate to={redirectTo} />; | ||
}; | ||
|
||
PrivateRoute.propTypes = { | ||
component: PropTypes.object.isRequired, | ||
redirectTo: PropTypes.string, | ||
}; | ||
|
||
PublicRoute.propTypes = { | ||
component: PropTypes.object.isRequired, | ||
redirectTo: PropTypes.string, | ||
}; |
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,5 +1,12 @@ | ||
import React from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { logoutUser } from 'redux/Auth/authOperations'; | ||
|
||
export const HomePage = () => { | ||
return <div>HomePage</div>; | ||
const dispatch = useDispatch(); | ||
return ( | ||
<div> | ||
<button onClick={() => dispatch(logoutUser())}>Log logOut</button> | ||
</div> | ||
); | ||
}; |
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,69 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
|
||
import { | ||
registerUserApi, | ||
loginUserApi, | ||
currentUserApi, | ||
logoutUserApi, | ||
} from 'services/backendAPI'; | ||
|
||
export const registerUser = createAsyncThunk( | ||
'auth/register', | ||
async (dataUser, { rejectWithValue }) => { | ||
try { | ||
const data = await registerUserApi(dataUser); | ||
return data; | ||
} catch (error) { | ||
return rejectWithValue(error.message); | ||
} | ||
}, | ||
); | ||
|
||
export const loginUser = createAsyncThunk( | ||
'auth/login', | ||
async (dataUser, { rejectWithValue }) => { | ||
try { | ||
const userData = await loginUserApi(dataUser); | ||
return userData; | ||
} catch (error) { | ||
return rejectWithValue(error.message); | ||
} | ||
}, | ||
); | ||
|
||
export const currentUser = createAsyncThunk( | ||
'auth/current', | ||
async (_, { getState, rejectWithValue, dispatch }) => { | ||
const { token } = getState().auth; | ||
try { | ||
const data = await currentUserApi(token); | ||
return data; | ||
} catch (error) { | ||
setTimeout(() => { | ||
dispatch(logoutUser(token)); | ||
}, 0); | ||
return rejectWithValue(error.message); | ||
} | ||
}, | ||
{ | ||
condition(_, { getState }) { | ||
const { token } = getState().auth; | ||
return Boolean(token); | ||
}, | ||
}, | ||
); | ||
|
||
export const logoutUser = createAsyncThunk( | ||
'auth/logout', | ||
async (_, { getState, rejectWithValue }) => { | ||
const { token } = getState().auth; | ||
try { | ||
await logoutUserApi(token); | ||
return null; | ||
} catch (error) { | ||
return rejectWithValue(error.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,3 @@ | ||
export const selectorIsAuth = state => Boolean(state.auth.token); | ||
|
||
export const selectorTheme = state => state.auth.theme; |
Oops, something went wrong.