From afb1c2663069d74474311bf7da30f62720a3f83b Mon Sep 17 00:00:00 2001 From: Michal Kawka Date: Mon, 18 Nov 2024 10:40:39 +0100 Subject: [PATCH] add CallbackHandler.tsx --- frontend/src/CallbackHandler.tsx | 82 ++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 frontend/src/CallbackHandler.tsx diff --git a/frontend/src/CallbackHandler.tsx b/frontend/src/CallbackHandler.tsx new file mode 100644 index 0000000..490e014 --- /dev/null +++ b/frontend/src/CallbackHandler.tsx @@ -0,0 +1,82 @@ +import {Loader2} from 'lucide-react'; +import {useEffect} from 'react'; +import {useNavigate} from 'react-router-dom'; +import {signInAndUp} from 'supertokens-web-js/recipe/thirdparty'; +import {Card, CardContent} from './components/ui/card'; + +const CallbackHandler = () => { + const navigate = useNavigate(); + + useEffect(() => { + async function handleGithubCallback() { + try { + const response = await signInAndUp(); + + if (response.status === 'OK') { + console.log('User is: ', response.user); + if ( + response.createdNewRecipeUser && + response.user.loginMethods.length === 1 + ) { + // Sign-up successful + // You can perform any additional sign-up logic here + } else { + // Sign-in successful + // You can perform any additional sign-in logic here + } + navigate('/'); // Redirect to home or desired page + } else if (response.status === 'SIGN_IN_UP_NOT_ALLOWED') { + // Display a user-friendly error message + window.alert(response.reason); + } else { + // Handle cases where no email is provided by the third-party provider + window.alert( + 'No email provided by social login. Please use another form of login.' + ); + navigate('/auth'); // Redirect back to the login page + } + } catch (err) { + console.log('Github error is: ', err); + if (err.isSuperTokensGeneralError === true) { + // Handle custom error messages sent from your API + // window.alert(err.message); + } else { + // window.alert(err.message); + } + } + } + + handleGithubCallback(); + }, [navigate]); + + return ( +
+ + + {/*
*/} + {/* */} + {/*
*/} +
+

+ Authenticating +

+

+ Please wait while we process your login... +

+
+
+ +
+
+
+
+ ); +}; + +export default CallbackHandler;