-
Notifications
You must be signed in to change notification settings - Fork 19
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 #875 from exogee-technology/feat/login-with-redirect
Auth0 Login with Redirect
- Loading branch information
Showing
33 changed files
with
132 additions
and
79 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
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
50 changes: 37 additions & 13 deletions
50
src/packages/auth-ui-components/src/components/auth-zero/login/component.tsx
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,48 +1,72 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
import { Button } from '@exogee/graphweaver-admin-ui-components'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useNavigate, useSearchParams } from 'react-router-dom'; | ||
import { getAuth0Client } from '../client'; | ||
|
||
export const Auth0 = () => { | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState<string | undefined>(); | ||
|
||
const [searchParams] = useSearchParams(); | ||
const shouldRedirect = useRef(true); | ||
const navigate = useNavigate(); | ||
|
||
// In this effect we are checking if the user is coming back from the Auth0 login page or | ||
// if the user is coming to the page for the first time. If the user is coming back from | ||
// the Auth0 login page, we process the redirect. If the user is coming to the page for the | ||
// first time, we redirect the user to the Auth0 login page. | ||
useEffect(() => { | ||
if (shouldRedirect.current) { | ||
shouldRedirect.current = false; | ||
requestLogin(); | ||
const code = searchParams.get('code'); | ||
const state = searchParams.get('state'); | ||
|
||
if (code && state) { | ||
// The user is coming back from the Auth0 login page | ||
processRedirect(); | ||
} else { | ||
// The user is coming to the page for the first time | ||
requestLogin(); | ||
} | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!loading && !error) { | ||
navigate('/'); | ||
// This function is called when the user clicks the login button or when the user is coming to the page for the first time | ||
const requestLogin = useCallback(async () => { | ||
try { | ||
const client = await getAuth0Client(); | ||
const options = { | ||
authorizationParams: { | ||
redirect_uri: window.location.toString(), | ||
}, | ||
}; | ||
await client.loginWithRedirect(options); | ||
} catch (e: any) { | ||
if (e.message) setError(e.message); | ||
setLoading(false); | ||
} | ||
}, [loading, error]); | ||
}, []); | ||
|
||
const requestLogin = useCallback(async () => { | ||
// This function is called when the user is coming back from the Auth0 login page and we need to process the redirect | ||
// This function will handle the redirect and redirect the user to the home page | ||
const processRedirect = useCallback(async () => { | ||
try { | ||
const client = await getAuth0Client(); | ||
await client.loginWithPopup(); | ||
await client.handleRedirectCallback(); | ||
navigate('/'); | ||
} catch (e: any) { | ||
if (e.message) setError(e.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, []); | ||
|
||
const handleRetry = () => { | ||
requestLogin(); | ||
}; | ||
|
||
if (loading) return <div>Loading...</div>; | ||
|
||
return ( | ||
<div> | ||
{error && <div>{error}</div>} | ||
<Button onClick={handleRetry}>Retry</Button> | ||
<Button onClick={requestLogin}>Login</Button> | ||
</div> | ||
); | ||
}; |
10 changes: 8 additions & 2 deletions
10
src/packages/auth-ui-components/src/components/auth-zero/logout/component.tsx
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
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
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
Oops, something went wrong.