supertokens / supertokens-auth-react

ReactJS authentication module for SuperTokens
https://supertokens.com
Other
270 stars 84 forks source link

EmailPassword Recipe: Email verification #102

Closed kant01ne closed 3 years ago

kant01ne commented 3 years ago

EmailPassword Recipe: Email verification

Sub issue of https://github.com/supertokens/supertokens-core/issues/124

isEmailVerified

Configs

SuperTokens.init({
  recipes: [
     EmailPassword.init({
            emailVerificationFeature: {
                mode: "OFF" | "REQUIRED" | "OPTIONAL",
                disableDefaultImplementation: boolean,
                sendVerifyEmailScreen: {
                  style: {...}   
                },
                sendVerifyEmailBanner: {
                   style: {...}   
                },
                verifyEmailLinkClickedScreen: {
                   style: {...}  
                }
           }
        }
     })
  ]

Default URLs

For required mode email verify UI: {websiteBasePath}/verify-email?rid=emailpassword For email clicked UI: {websiteBasePath}/verify-email?rid=emailpassword&token=TOKEN

Email verification (Required mode)

Auth Component

import {EmailPasswordAuth} from "supertokens-auth-react/recipe/emailpassword";

<EmailPasswordAuth
  onCallIsEmailVerifiedAPI?: (headers: HeadersInit) => Promise<IsEmailVerifiedAPIResponse>
  doesSessionExist?: () => Promise<boolean>
  onHandleShowEmailVerificationScreen?: () => Promise<boolean> // if true, we do nothing, if false, we do default redirection.
>
  <LoggedInComponents />
</EmailPasswordAuth>

EmailVerificationScreen

import EmailVerification from "supertokens-auth-react/recipe/emailpassword";

<EmailVerification
  doesSessionExist?: () => Promise<boolean>
  onCallVerifyEmailAPI?: (requestJson: RequestJson, headers: HeadersInit) => Promise<VerifyEmailAPIResponse>
  onCallSendVerifyEmailAPI?: (headers: HeadersInit) => Promise<SendVeriyEmailAPIResponse>
  signOut?: () => Promise<SignOutAPIResponse>
  onHandleSuccess?: (context: {
        action: "VERIFY_EMAIL_SENT" | "EMAIL_VERIFIED_SUCCESSFUL"
    }): Promise<boolean>;
>

EmailVerification Themes

import {EmailVerification, EmailVerificationScreenTheme} from "supertokens-auth-react/recipe/emailpassword";

<EmailVerification>
  <EmailVerificationScreenTheme/>
</EmailVerification>

// OR 
import {EmailVerification} from "supertokens-auth-react/recipe/emailpassword";
import {EmailVerificationScreenThemeCustomTheme} from "customTheme";

<EmailVerification>
  <EmailVerificationScreenThemeCustomTheme/>
</EmailVerification>

// With
type EmailVerificationScreenThemeProps = {
    sendVerifyEmailScreen: {
           callAPI: () => Promise<SendVerifyEmailThemeResponse>;
           styleFromInit?: Styles;
    }

    verifyEmailLinkClickedScreen: {
               styleFromInit: Styles;
               callAPI: () => Promise<VerifyEmailThemeResponse>;
               redirectToVerifyEmailScreen: Promise<void>
               onSuccess: () => Promise<void>;
               onContinueClicked: () => Promise<void>
    }

}
kant01ne commented 3 years ago

TODO Email verified banner (Optional mode)

   <EmailVerification>
       <EmailVerificationBannerTheme />
   <EmailVerification>

We can update the banner (update its color and text) to "We have sent you an email" if successful. Hence, making it harder to send it multiple times (would need a page refresh) which I think is fine.


TODO Later

kant01ne commented 3 years ago

Email Verification Required Mode Release

kant01ne commented 3 years ago

As raised in #110, we need a way to know the login URL in EmailPasswordAuth in case it gets overwritten, and would need yet another props for this. Not going to fix now with a new props as we are discussing https://github.com/supertokens/supertokens-auth-react/issues/117 tmrw.