-
Notifications
You must be signed in to change notification settings - Fork 8
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 #74 from michaelcaxias/fix/login-validation
fix: check if user is logged in validation
- Loading branch information
Showing
7 changed files
with
115 additions
and
50 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
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,51 @@ | ||
import React, { useContext, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import axios from 'axios'; | ||
import { MyContext } from '../context/Provider'; | ||
import { useNavigate } from 'react-router'; | ||
import Loading from '../components/Loading/Loading'; | ||
|
||
export default function LoginWrap({ children }) { | ||
const { setUserInfo, changeSignedInState, loading, setLoading } = useContext(MyContext); | ||
const navigate = useNavigate(); | ||
|
||
const requestUserInfo = async () => { | ||
const accessToken = localStorage.getItem('accessToken'); | ||
|
||
try { | ||
const userInfo = await axios.get( | ||
'https://www.googleapis.com/oauth2/v3/userinfo', | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
} | ||
); | ||
|
||
setUserInfo(userInfo.data); | ||
changeSignedInState(true); | ||
|
||
} catch (error) { | ||
if (error.response.status === 401) { | ||
changeSignedInState(false); | ||
navigate('/'); | ||
} | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
requestUserInfo(); | ||
}, []); | ||
|
||
if (loading) return <Loading />; | ||
|
||
return ( | ||
<div>{children}</div> | ||
); | ||
} | ||
|
||
LoginWrap.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; |
f521d06
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
trybe-schedule – ./
trybe-schedule-michaelcaxias.vercel.app
trybe-schedule.vercel.app
trybe-schedule-git-master-michaelcaxias.vercel.app
www.trybescheduler.com.br
trybescheduler.com.br