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.
Merge pull request #576 from hackforla/refactor-admin-login-component
Refactor admin login component
- Loading branch information
Showing
6 changed files
with
177 additions
and
149 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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); | ||
|
||
useEffect(() => { | ||
const search = props.location.search; | ||
const params = new URLSearchParams(search); | ||
const api_token = params.get('token'); | ||
const isValid = isValidToken(api_token); | ||
setMagicLink(isValid); | ||
}, []); | ||
|
||
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
HEADERS, | ||
CHECK_USER, | ||
SIGN_IN, | ||
AUTH_VERIFY_SIGN_IN, | ||
} from '../utils/endpoints'; | ||
|
||
/** | ||
* Method sent request to the backend to check if user exist in the DB | ||
* @returns user data otherwise null | ||
* @param email user email | ||
* @param auth_origin auth origin 'LOG_IN' or 'CREATE_ACCOUNT' | ||
*/ | ||
export async function checkUser(email, auth_origin) { | ||
try { | ||
const response = await fetch(CHECK_USER, { | ||
method: 'POST', | ||
headers: HEADERS, | ||
body: JSON.stringify({ email: email, auth_origin: auth_origin }), | ||
}); | ||
return await response.json(); | ||
} catch (error) { | ||
console.log('User is not registered in the app'); | ||
console.log(error); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Method sent request to the backend to check if user can login in app | ||
* @returns true if user can login otherwise null | ||
* @param email user email | ||
* @param auth_origin auth origin 'LOG_IN' or "CREATE_ACCOUNT' | ||
*/ | ||
export async function checkAuth(email, auth_origin) { | ||
try { | ||
const response = await fetch(SIGN_IN, { | ||
method: 'POST', | ||
headers: HEADERS, | ||
body: JSON.stringify({ email: email, auth_origin: auth_origin }), | ||
}); | ||
return response.status === 200; | ||
} catch (error) { | ||
console.log('User is not authorized in app'); | ||
console.log(error); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Method sent request to the backend to check if token is valid | ||
* @returns true if is valid otherwise false | ||
* @param api_token token | ||
*/ | ||
export async function isValidToken(api_token) { | ||
try { | ||
const response = await fetch(AUTH_VERIFY_SIGN_IN, { | ||
method: 'POST', | ||
headers: { | ||
...HEADERS, | ||
'x-access-token': api_token, | ||
}, | ||
}); | ||
return response.status === 200; | ||
} catch (error) { | ||
console.log('Token is not valid'); | ||
console.log(error); | ||
return false; | ||
} | ||
} |
Oops, something went wrong.