setVerified(true)}
+ />
+ );
}
- }
-
- if (expired) {
- return (
- Email link has expired
- );
- }
- if (verified) {
return (
- Signed in on other tab
+
);
}
- return (
-
- );
- }
-
- // pages/verification.jsx
- // Handle email link verification results. This is
- // the final step in the email link flow.
- function Verification() {
- const [
- verificationStatus,
- setVerificationStatus,
- ] = React.useState("loading");
+ // A page which verifies email addresses with email links.
+ function VerifyWithEmailLink({
+ emailAddress,
+ onVerify,
+ }) {
+ const { startEmailLinkFlow } = useEmailLink(emailAddress);
- const { handleEmailLinkVerification } = useClerk();
+ React.useEffect(() => {
+ verify();
+ }, []);
- React.useEffect(() => {
async function verify() {
- try {
- await handleEmailLinkVerification({
- redirectUrl: "https://redirect-to-pending-sign-up",
- redirectUrlComplete: "https://redirect-when-sign-up-complete",
- });
- // If we're not redirected at this point, it means
- // that the flow has completed on another device.
- setVerificationStatus("verified");
- } catch (err) {
- // Verification has failed.
- let status = "failed";
- if (isEmailLinkError(err) && err.code === EmailLinkErrorCode.Expired) {
- status = "expired";
- }
- setVerificationStatus(status);
- }
- }
- verify();
- }, []);
-
- if (verificationStatus === "loading") {
- return Loading...
- }
-
- if (verificationStatus === "failed") {
- return (
- Email link verification failed
- );
- }
-
- if (verificationStatus === "expired") {
- return (
- Email link expired
- );
- }
-
- return (
-
- Successfully signed up. Return to the original tab to continue.
-
- );
- }
- ```
-
- ```jsx {{ prettier: false }}
- import React from "react";
- import {
- BrowserRouter as Router,
- Routes,
- Route,
- useNavigate,
- } from 'react-router-dom';
- import {
- ClerkProvider,
- ClerkLoaded,
- EmailLinkErrorCode,
- isEmailLinkError,
- UserButton,
- useClerk,
- useSignUp,
- SignedOut,
- SignedIn,
- } from '@clerk/clerk-react';
-
- const publishableKey = process.env.REACT_APP_CLERK_PUBLISHABLE_KEY;
-
- function App() {
- return (
-
-
-
- {/* Root path shows sign up page. */}
-
-
-
-
-
-
-
- >
- }
- />
-
- {/* Define a /verification route that handles email link result */}
-
-
-
- }
- />
-
-
-
- );
- }
-
- // Render the sign up form.
- // Collect user's email address and send an email link with which
- // they can sign up.
- function SignUpEmailLink() {
- const [emailAddress, setEmailAddress] = React.useState("");
- const [expired, setExpired] = React.useState(false);
- const [verified, setVerified] = React.useState(false);
- const navigate = useNavigate();
- const { signUp, isLoaded, setActive } = useSignUp();
-
- if (!isLoaded) {
- return null;
- }
-
- const { startEmailLinkFlow, cancelEmailLinkFlow } =
- signUp.createEmailLinkFlow();
-
- async function submit(e) {
- e.preventDefault();
- setExpired(false);
- setVerified(false);
-
- // Start the sign up flow, by collecting
- // the user's email address.
- await signUp.create({ emailAddress });
-
- // Start the email link flow.
- // Pass your app URL that users will be navigated
- // when they click the email link from their
- // email inbox.
- // su will hold the updated sign up object.
- const su = await startEmailLinkFlow({
- redirectUrl: "https://your-app.domain.com/verification",
- });
-
- // Check the verification result.
- const verification = su.verifications.emailAddress;
- if (verification.verifiedFromTheSameClient()) {
- setVerified(true);
- // If you're handling the verification result from
- // another route/component, you should return here.
- // See the component as an
- // example below.
- // If you want to complete the flow on this tab,
- // don't return. Check the sign up status instead.
- return;
- } else if (verification.status === "expired") {
- setExpired(true);
- }
-
- if (su.status === "complete") {
- // Sign up is complete, we have a session.
- // Navigate to the after sign up URL.
- setActive({
- session: su.createdSessionId,
- beforeEmit: () => navigate("/after-sign-up-path"),
+ // Start the email link flow.
+ // Pass your app URL that users will be navigated
+ // when they click the email link from their
+ // email inbox.
+ const res = await startEmailLinkFlow({
+ redirectUrl: "https://redirect-from-email-email-link",
});
- return;
- }
- }
-
- if (expired) {
- return (
- Email link has expired
- );
- }
-
- if (verified) {
- return (
- Signed in on other tab
- );
- }
-
- return (
-
- );
- }
- // Handle email link verification results. This is
- // the final step in the email link flow.
- function EmailLinkVerification() {
- const [
- verificationStatus,
- setVerificationStatus,
- ] = React.useState("loading");
-
- const { handleEmailLinkVerification } = useClerk();
-
- React.useEffect(() => {
- async function verify() {
- try {
- await handleEmailLinkVerification({
- redirectUrl: "https://redirect-to-pending-sign-up",
- redirectUrlComplete: "https://redirect-when-sign-up-complete",
- });
- // If we're not redirected at this point, it means
- // that the flow has completed on another device.
- setVerificationStatus("verified");
- } catch (err) {
- // Verification has failed.
- let status = "failed";
- if (isEmailLinkError(err) && err.code === EmailLinkErrorCode.Expired) {
- status = "expired";
- }
- setVerificationStatus(status);
+ // res will hold the updated EmailAddress object.
+ if (res.verification.status === "verified") {
+ onVerify();
+ } else {
+ // act accordingly
}
}
- verify();
- }, []);
-
- if (verificationStatus === "loading") {
- return Loading...
- }
-
- if (verificationStatus === "failed") {
- return (
- Email link verification failed
- );
- }
- if (verificationStatus === "expired") {
return (
- Email link expired
+
+ Waiting for verification...
+
);
}
+ ```
- return (
-
- Successfully signed up. Return to the original tab to continue.
-
- );
- }
+ ```jsx {{ prettier: false }}
+ import React from "react";
+ import { useUser, useEmailLink } from "@clerk/clerk-react";
- export default App;
- ```
+ // A page where users can add a new email address.
+ function NewEmailPage() {
+ const [email, setEmail] = React.useState('');
+ const [emailAddress, setEmailAddress] = React.useState(null);
+ const [verified, setVerified] = React.useState(false);
- ```js {{ prettier: false }}
- const signUp = window.Clerk.client.signUp;
- const {
- startEmailLinkFlow,
- cancelEmailLinkFlow,
- } = signUp.createEmailLinkFlow();
+ const { user } = useUser();
- const res = await startEmailLinkFlow({
- // Pass your app URL that users will be navigated
- // when they click the email link from their
- // email inbox.
- redirectUrl: "https://redirect-from-email-email-link"
- });
- if (res.status === "completed") {
- // sign up completed
- } else {
- // sign up still pending
- }
- // Cleanup
- cancelEmailLinkFlow();
- ```
-