Skip to content

Commit

Permalink
update dependencies; align code with quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisintech committed Oct 30, 2024
1 parent 4bd5864 commit 696e458
Show file tree
Hide file tree
Showing 7 changed files with 1,235 additions and 3,341 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=
# Visit https://dashboard.clerk.com to find your API Keys
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=
38 changes: 21 additions & 17 deletions app/(auth)/sign-in.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
import { useSignIn } from "@clerk/clerk-expo";
import { Link, useRouter } from "expo-router";
import { Text, TextInput, Button, View } from "react-native";
import React from "react";
import { useSignIn } from '@clerk/clerk-expo';
import { Link, useRouter } from 'expo-router';
import { Text, TextInput, Button, View } from 'react-native';
import React from 'react';

export default function Page() {
const { signIn, setActive, isLoaded } = useSignIn();
const router = useRouter();

const [emailAddress, setEmailAddress] = React.useState("");
const [password, setPassword] = React.useState("");
const [emailAddress, setEmailAddress] = React.useState('');
const [password, setPassword] = React.useState('');

// Handle the submission of the sign-in form
const onSignInPress = React.useCallback(async () => {
if (!isLoaded) {
return;
}
if (!isLoaded) return;

// Start the sign-in process using the email and password provided
try {
const signInAttempt = await signIn.create({
identifier: emailAddress,
password,
});

if (signInAttempt.status === "complete") {
// If sign-in process is complete, set the created session as active
// and redirect the user
if (signInAttempt.status === 'complete') {
await setActive({ session: signInAttempt.createdSessionId });
router.replace("/");
router.replace('/');
} else {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
// If the status is not complete, check why. User may need to
// complete further steps.
console.error(JSON.stringify(signInAttempt, null, 2));
}
} catch (err: any) {
} catch (err) {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
console.error(JSON.stringify(err, null, 2));
}
}, [isLoaded, emailAddress, password]);
Expand All @@ -39,16 +43,16 @@ export default function Page() {
<TextInput
autoCapitalize="none"
value={emailAddress}
placeholder="Email..."
placeholder="Enter email"
onChangeText={(emailAddress) => setEmailAddress(emailAddress)}
/>
<TextInput
value={password}
placeholder="Password..."
placeholder="Enter password"
secureTextEntry={true}
onChangeText={(password) => setPassword(password)}
/>
<Button title="Sign In" onPress={onSignInPress} />
<Button title="Sign in" onPress={onSignInPress} />
<View>
<Text>Don't have an account?</Text>
<Link href="/sign-up">
Expand Down
110 changes: 61 additions & 49 deletions app/(auth)/sign-up.tsx
Original file line number Diff line number Diff line change
@@ -1,90 +1,102 @@
import * as React from "react";
import { TextInput, Button, View } from "react-native";
import { useSignUp } from "@clerk/clerk-expo";
import { useRouter } from "expo-router";
import * as React from 'react';
import { Text, TextInput, Button, View } from 'react-native';
import { useSignUp } from '@clerk/clerk-expo';
import { useRouter } from 'expo-router';

export default function SignUpScreen() {
const { isLoaded, signUp, setActive } = useSignUp();
const router = useRouter();

const [emailAddress, setEmailAddress] = React.useState("");
const [password, setPassword] = React.useState("");
const [emailAddress, setEmailAddress] = React.useState('');
const [password, setPassword] = React.useState('');
const [pendingVerification, setPendingVerification] = React.useState(false);
const [code, setCode] = React.useState("");
const [code, setCode] = React.useState('');

// Handle submission of sign-up form
const onSignUpPress = async () => {
if (!isLoaded) {
return;
}
if (!isLoaded) return;

console.log(emailAddress, password);

// Start sign-up process using email and password provided
try {
await signUp.create({
emailAddress,
password,
});

await signUp.prepareEmailAddressVerification({ strategy: "email_code" });
// Send user an email with verification code
await signUp.prepareEmailAddressVerification({ strategy: 'email_code' });

// Set 'pendingVerification' to true to display second form
// and capture OTP code
setPendingVerification(true);
} catch (err: any) {
} catch (err) {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
console.error(JSON.stringify(err, null, 2));
}
};

const onPressVerify = async () => {
if (!isLoaded) {
return;
}
// Handle submission of verification form
const onVerifyPress = async () => {
if (!isLoaded) return;

try {
const completeSignUp = await signUp.attemptEmailAddressVerification({
// Use the code the user provided to attempt verification
const signUpAttempt = await signUp.attemptEmailAddressVerification({
code,
});

if (completeSignUp.status === "complete") {
await setActive({ session: completeSignUp.createdSessionId });
router.replace("/");
// If verification was completed, set the session to active
// and redirect the user
if (signUpAttempt.status === 'complete') {
await setActive({ session: signUpAttempt.createdSessionId });
router.replace('/');
} else {
console.error(JSON.stringify(completeSignUp, null, 2));
// If the status is not complete, check why. User may need to
// complete further steps.
console.error(JSON.stringify(signUpAttempt, null, 2));
}
} catch (err: any) {
} catch (err) {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
console.error(JSON.stringify(err, null, 2));
}
};

if (pendingVerification) {
return (
<>
<Text>Verify your email</Text>
<TextInput
value={code}
placeholder="Enter your verification code"
onChangeText={(code) => setCode(code)}
/>
<Button title="Verify" onPress={onVerifyPress} />
</>
);
}

return (
<View>
{!pendingVerification && (
<>
<TextInput
autoCapitalize="none"
value={emailAddress}
placeholder="Email..."
onChangeText={(email) => setEmailAddress(email)}
/>
<TextInput
value={password}
placeholder="Password..."
secureTextEntry={true}
onChangeText={(password) => setPassword(password)}
/>
<Button title="Sign Up" onPress={onSignUpPress} />
</>
)}
{pendingVerification && (
<>
<TextInput
value={code}
placeholder="Code..."
onChangeText={(code) => setCode(code)}
/>
<Button title="Verify Email" onPress={onPressVerify} />
</>
)}
<>
<Text>Sign up</Text>
<TextInput
autoCapitalize="none"
value={emailAddress}
placeholder="Enter email"
onChangeText={(email) => setEmailAddress(email)}
/>
<TextInput
value={password}
placeholder="Enter password"
secureTextEntry={true}
onChangeText={(password) => setPassword(password)}
/>
<Button title="Continue" onPress={onSignUpPress} />
</>
</View>
);
}
23 changes: 10 additions & 13 deletions app/(home)/index.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { SignedIn, SignedOut, useUser } from "@clerk/clerk-expo";
import { Link } from "expo-router";
import { Text, View } from "react-native";
import { SignedIn, SignedOut, useUser } from '@clerk/clerk-expo';
import { Link } from 'expo-router';
import { Text, View } from 'react-native';

export default function Page() {
const { user } = useUser();

return (
<View>
<SignedIn>
<Text>Welcome, {user?.emailAddresses[0].emailAddress}</Text>
<Text>Hello {user?.emailAddresses[0].emailAddress}</Text>
</SignedIn>
<SignedOut>
<View>
<Text>Clerk 🤝 Expo</Text>
<Link href="/sign-in">
<Text>Sign In</Text>
</Link>
<Link href="/sign-up">
<Text>Sign Up</Text>
</Link>
</View>
<Link href="/(auth)/sign-in">
<Text>Sign in</Text>
</Link>
<Link href="/(auth)/sign-up">
<Text>Sign up</Text>
</Link>
</SignedOut>
</View>
);
Expand Down
13 changes: 6 additions & 7 deletions cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as SecureStore from "expo-secure-store";
import { Platform } from "react-native";
import { TokenCache } from "@clerk/clerk-expo/dist/cache";
import * as SecureStore from 'expo-secure-store';
import { Platform } from 'react-native';
import { TokenCache } from '@clerk/clerk-expo/dist/cache';

const createTokenCache = (): TokenCache => {
return {
Expand All @@ -10,11 +10,11 @@ const createTokenCache = (): TokenCache => {
if (item) {
console.log(`${key} was used 🔐 \n`);
} else {
console.log("No values stored under key: " + key);
console.log('No values stored under key: ' + key);
}
return item;
} catch (error) {
console.error("secure store get item error: ", error);
console.error('secure store get item error: ', error);
await SecureStore.deleteItemAsync(key);
return null;
}
Expand All @@ -26,6 +26,5 @@ const createTokenCache = (): TokenCache => {
};

// SecureStore is not supported on the web
// https://github.com/expo/expo/issues/7744#issuecomment-611093485
export const tokenCache =
Platform.OS !== "web" ? createTokenCache() : undefined;
Platform.OS !== 'web' ? createTokenCache() : undefined;
Loading

0 comments on commit 696e458

Please sign in to comment.