Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit 624f203

Browse files
authored
Merge pull request #20 from t3dotgg/main
Clean up mobile clerk integration, fix type errors
2 parents 264589d + e1060a4 commit 624f203

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

apps/expo/src/_app.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ import { HomeScreen } from "./screens/home";
77
import { SignInSignUpScreen } from "./screens/signin";
88
import { ClerkProvider, SignedIn, SignedOut } from "@clerk/clerk-expo";
99
import { tokenCache } from "./utils/cache";
10-
11-
// Find this in your Dashboard.
12-
const clerk_frontend_api = "YOUR_CLERK_FRONTEND_API";
10+
import { CLERK_FRONTEND_API } from "./constants";
1311

1412
export const App = () => {
1513
return (
16-
<ClerkProvider frontendApi={clerk_frontend_api} tokenCache={tokenCache}>
14+
<ClerkProvider frontendApi={CLERK_FRONTEND_API} tokenCache={tokenCache}>
1715
<SignedIn>
1816
<TRPCProvider>
1917
<SafeAreaProvider>

apps/expo/src/components/SignInWithOAuth.tsx

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,50 @@ const SignInWithOAuth = () => {
2424
firstFactorVerification: { externalVerificationRedirectURL },
2525
} = signIn;
2626

27-
const result = await AuthSession.startAsync({
27+
if (!externalVerificationRedirectURL)
28+
throw "Something went wrong during the OAuth flow. Try again.";
29+
30+
const authResult = await AuthSession.startAsync({
2831
authUrl: externalVerificationRedirectURL.toString(),
2932
returnUrl: redirectUrl,
3033
});
3134

32-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
33-
// @ts-ignore
34-
const { type, params } = result || {};
35-
36-
if (type !== "success") {
35+
if (authResult.type !== "success") {
3736
throw "Something went wrong during the OAuth flow. Try again.";
3837
}
3938

4039
// Get the rotatingTokenNonce from the redirect URL parameters
41-
const { rotating_token_nonce: rotatingTokenNonce } = params;
40+
const { rotating_token_nonce: rotatingTokenNonce } = authResult.params;
4241

4342
await signIn.reload({ rotatingTokenNonce });
4443

4544
const { createdSessionId } = signIn;
4645

47-
if (!createdSessionId) {
48-
if (signIn.firstFactorVerification.status === "transferable") {
49-
console.log("Didn't have an account transferring");
50-
51-
await signUp.create({ transfer: true });
52-
53-
const { rotating_token_nonce: rotatingTokenNonce } = params;
54-
55-
await signUp.reload({ rotatingTokenNonce });
56-
57-
const { createdSessionId } = signUp;
58-
if (!createdSessionId) {
59-
throw "Something went wrong during the Sign up OAuth flow. Please ensure that all sign up requirements are met.";
60-
}
61-
await setSession(createdSessionId);
62-
63-
return;
46+
if (createdSessionId) {
47+
// If we have a createdSessionId, then auth was successful
48+
await setSession(createdSessionId);
49+
} else {
50+
// If we have no createdSessionId, then this is a first time sign-in, so
51+
// we should process this as a signUp instead
52+
// Throw if we're not in the right state for creating a new user
53+
if (
54+
!signUp ||
55+
signIn.firstFactorVerification.status !== "transferable"
56+
) {
57+
throw "Something went wrong during the Sign up OAuth flow. Please ensure that all sign up requirements are met.";
6458
}
65-
throw "Something went wrong during the Sign in OAuth flow. Please ensure that all sign in requirements are met.";
66-
}
6759

68-
await setSession(createdSessionId);
60+
console.log(
61+
"Didn't have an account transferring, following through with new account sign up",
62+
);
6963

70-
return;
64+
// Create user
65+
await signUp.create({ transfer: true });
66+
await signUp.reload({
67+
rotatingTokenNonce: authResult.params.rotating_token_nonce,
68+
});
69+
await setSession(signUp.createdSessionId);
70+
}
7171
} catch (err) {
7272
console.log(JSON.stringify(err, null, 2));
7373
console.log("error signing in", err);

apps/expo/src/constants.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* URL key for the Clerk auth API. You can find this in your Clerk dashboard:
3+
* https://dashboard.clerk.dev
4+
*
5+
* NOTE: we recommend putting the frontend api key here instead of in your .env
6+
* files for two reasons:
7+
* 1. It's okay for this to be "public" (CLERK_API_KEY and CLERK_JWT_KEY should
8+
* NEVER be public)
9+
* 2. Parsing the .env file in Metro/Expo runs the risk of including the
10+
* variables above that we don't want (and it's obnoxious to do right as a
11+
* result)
12+
*/
13+
export const CLERK_FRONTEND_API = "";
14+
15+
if (CLERK_FRONTEND_API === "") {
16+
throw new Error("CLERK_FRONTEND_API is not defined");
17+
}

apps/expo/src/utils/trpc.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const TRPCProvider: React.FC<{
4545
async headers() {
4646
const authToken = await getToken();
4747
return {
48-
Authorization: authToken,
48+
Authorization: authToken ?? undefined,
4949
};
5050
},
5151
url: `${getBaseUrl()}/api/trpc`,

0 commit comments

Comments
 (0)