generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 87
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
790628d
commit c40140e
Showing
2 changed files
with
41 additions
and
32 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 |
---|---|---|
@@ -1,47 +1,29 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { Redirect } from 'react-router-dom'; | ||
import { isValidToken } from '../../services/user.service'; | ||
|
||
import '../../sass/MagicLink.scss'; | ||
import useAuth from '../../hooks/useAuth'; | ||
|
||
const HandleAuth = (props) => { | ||
const auth = useAuth(); | ||
const [isMagicLinkValid, setMagicLink] = useState(null); | ||
const headerToSend = process.env.REACT_APP_CUSTOM_REQUEST_HEADER; | ||
|
||
async function isValidToken() { | ||
useEffect(() => { | ||
const search = props.location.search; | ||
const params = new URLSearchParams(search); | ||
const api_token = params.get('token'); | ||
|
||
try { | ||
const response = await fetch('/api/auth/verify-signin', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-access-token': api_token, | ||
'x-customrequired-header': headerToSend, | ||
}, | ||
}); | ||
// eslint-disable-next-line no-unused-vars | ||
const body = await response; | ||
setMagicLink(response.status === 200); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
isValidToken(); | ||
const isValid = isValidToken(api_token); | ||
setMagicLink(isValid); | ||
}, []); | ||
|
||
if (isMagicLinkValid) { | ||
return <Redirect to="/admin" />; | ||
} else { | ||
return ( | ||
<div className="flex-container"> | ||
<div>Sorry, the link is not valid.</div> | ||
</div> | ||
); | ||
} | ||
return auth.user && isMagicLinkValid ? ( | ||
<Redirect to="/admin" /> | ||
) : ( | ||
<div className="flex-container"> | ||
<div>Sorry, the link is not valid anymore.</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default HandleAuth; |
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