|
| 1 | +import { useSignUp } from "@clerk/clerk-expo"; |
| 2 | +import React from "react"; |
| 3 | +import { Button, View } from "react-native"; |
| 4 | + |
| 5 | +import * as AuthSession from "expo-auth-session"; |
| 6 | + |
| 7 | +const SignUpWithOAuth = () => { |
| 8 | + const { isLoaded, signUp, setSession } = useSignUp(); |
| 9 | + |
| 10 | + if (!isLoaded) return null; |
| 11 | + |
| 12 | + const handleSignUpWithDiscordPress = async () => { |
| 13 | + try { |
| 14 | + const redirectUrl = AuthSession.makeRedirectUri({ |
| 15 | + path: "/oauth-native-callback", |
| 16 | + }); |
| 17 | + |
| 18 | + await signUp.create({ |
| 19 | + strategy: "oauth_discord", |
| 20 | + redirectUrl, |
| 21 | + }); |
| 22 | + |
| 23 | + const { |
| 24 | + verifications: { |
| 25 | + externalAccount: { externalVerificationRedirectURL }, |
| 26 | + }, |
| 27 | + } = signUp; |
| 28 | + |
| 29 | + const result = await AuthSession.startAsync({ |
| 30 | + authUrl: externalVerificationRedirectURL!.toString(), |
| 31 | + returnUrl: redirectUrl, |
| 32 | + }); |
| 33 | + |
| 34 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 35 | + // @ts-ignore |
| 36 | + const { type, params } = result || {}; |
| 37 | + console.log; |
| 38 | + if (type !== "success") { |
| 39 | + throw "Something went wrong during the OAuth flow. Try again."; |
| 40 | + } |
| 41 | + |
| 42 | + // Get the rotatingTokenNonce from the redirect URL parameters |
| 43 | + const { rotating_token_nonce: rotatingTokenNonce } = params; |
| 44 | + |
| 45 | + await signUp.reload({ rotatingTokenNonce }); |
| 46 | + |
| 47 | + const { createdSessionId } = signUp; |
| 48 | + |
| 49 | + if (!createdSessionId) { |
| 50 | + throw "Something went wrong during the Sign in OAuth flow. Please ensure that all sign in requirements are met."; |
| 51 | + } |
| 52 | + |
| 53 | + await setSession(createdSessionId); |
| 54 | + |
| 55 | + return; |
| 56 | + } catch (err) { |
| 57 | + console.log(JSON.stringify(err, null, 2)); |
| 58 | + console.log("error signing in", err); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + return ( |
| 63 | + <View className="my-8 rounded-lg border-2 border-gray-500 p-4"> |
| 64 | + <Button |
| 65 | + title="Sign Up with Discord" |
| 66 | + onPress={handleSignUpWithDiscordPress} |
| 67 | + /> |
| 68 | + </View> |
| 69 | + ); |
| 70 | +}; |
| 71 | + |
| 72 | +export default SignUpWithOAuth; |
0 commit comments