Instruction file imported from supertokens/create-supertokens-app (
.cursor/rules/react.mdc). Copyright stays with the author.
React
| factor | react imports | react inits |
|---|---|---|
emailpassword |
import EmailPassword from "supertokens-auth-react/recipe/emailpassword"; | EmailPassword.init(), |
thirdparty |
import ThirdParty from "supertokens-auth-react/recipe/thirdparty"; | ThirdParty.init({ signInAndUpFeature: { providers: [ ThirdParty.Github.init(), ThirdParty.Google.init(), ThirdParty.Apple.init(), ThirdParty.Twitter.init(), ], }, }), |
link_email |
import Passwordless, { PasswordlessComponentsOverrideProvider } from "supertokens-auth-react/recipe/passwordless"; | Passwordless.init({ contactMethod: "EMAIL_OR_PHONE", }), |
link_phone |
import Passwordless, { PasswordlessComponentsOverrideProvider } from "supertokens-auth-react/recipe/passwordless"; | Passwordless.init({ contactMethod: "EMAIL_OR_PHONE", }), |
otp_phone |
import Passwordless, { PasswordlessComponentsOverrideProvider } from "supertokens-auth-react/recipe/passwordless"; | Passwordless.init({ contactMethod: "EMAIL_OR_PHONE", }), |
otp_email |
import Passwordless, { PasswordlessComponentsOverrideProvider } from "supertokens-auth-react/recipe/passwordless"; | Passwordless.init({ contactMethod: "EMAIL_OR_PHONE", }), |
totp |
import TOTP from "supertokens-auth-react/recipe/totp";import { TOTPPreBuiltUI } from "supertokens-auth-react/recipe/totp/prebuiltui"; | TOTP.init(), |
Notes:
- Session, Dashboard, UserRoles also imported for all factors, in addition to their own config.
- if both link_email and link_phone are present as factors, the contactMethod in init becomes "EMAIL_OR_PASSWORD".
- if both otp_email and otp_phone are present as factors, the flowType in init becomes "USER_INPUT_CODE_AND_MAGIC_LINK".
- If a second factor is present, always include the MultiFactorAuth recipe.
- Second factors specific behavior always go in the override function of the MultiFactorAuth recipe!
- There are prebuilt UI override components for the react prebuilt UI. Those are always exported from the generated config file.
Examples
All auth
import EmailPassword from "supertokens-auth-react/recipe/emailpassword";
import ThirdParty from "supertokens-auth-react/recipe/thirdparty";
import Passwordless, { PasswordlessComponentsOverrideProvider } from "supertokens-auth-react/recipe/passwordless";
import { EmailPasswordPreBuiltUI } from "supertokens-auth-react/recipe/emailpassword/prebuiltui";
import { ThirdPartyPreBuiltUI } from "supertokens-auth-react/recipe/thirdparty/prebuiltui";
import { PasswordlessPreBuiltUI } from "supertokens-auth-react/recipe/passwordless/prebuiltui";
import Session from "supertokens-auth-react/recipe/session";
import React from "react";
export function getApiDomain() {
const apiPort = import.meta.env.VITE_APP_API_PORT || 3001;
const apiUrl = import.meta.env.VITE_APP_API_URL || `http://localhost:${apiPort}`;
return apiUrl;
}
export function getWebsiteDomain() {
const websitePort = import.meta.env.VITE_APP_WEBSITE_PORT || 3000;
const websiteUrl = import.meta.env.VITE_APP_WEBSITE_URL || `http://localhost:${websitePort}`;
return websiteUrl;
}
export const SuperTokensConfig = {
appInfo: {
appName: "SuperTokens Demo App",
apiDomain: getApiDomain(),
websiteDomain: getWebsiteDomain(),
},
// recipeList contains all the modules that you want to
// use from SuperTokens. See the full list here: https://supertokens.com/docs/guides
recipeList: [
EmailPassword.init(),
ThirdParty.init({
signInAndUpFeature: {
providers: [
ThirdParty.Github.init(),
ThirdParty.Google.init(),
ThirdParty.Apple.init(),
ThirdParty.Twitter.init(),
],
},
}),
Passwordless.init({
contactMethod: "EMAIL_OR_PHONE",
}),
Session.init(),
],
getRedirectionURL: async (context) => {
if (context.action === "SUCCESS" && context.newSessionCreated) {
return "/dashboard";
}
},
};
export const recipeDetails = {
docsLink: "https://supertokens.com/docs/thirdpartypasswordless/introduction",
};
export const PreBuiltUIList = [EmailPasswordPreBuiltUI, ThirdPartyPreBuiltUI, PasswordlessPreBuiltUI];
export const ComponentWrapper = (props: { children: JSX.Element }): JSX.Element => {
return (
<PasswordlessComponentsOverrideProvider
components={{
PasswordlessUserInputCodeFormFooter_Override: ({ DefaultComponent, ...props }) => {
const loginAttemptInfo = props.loginAttemptInfo;
let showQuotaMessage = false;
if (loginAttemptInfo.contactMethod === "PHONE") {
showQuotaMessage = true;
}
return (
<div
style={{
width: "100%",
}}
>
<DefaultComponent {...props} />
{showQuotaMessage && (
<div
style={{
width: "100%",
paddingLeft: 12,
paddingRight: 12,
paddingTop: 6,
paddingBottom: 6,
borderRadius: 4,
backgroundColor: "#EF9A9A",
margin: 0,
boxSizing: "border-box",
MozBoxSizing: "border-box",
WebkitBoxSizing: "border-box",
fontSize: 12,
textAlign: "start",
fontWeight: "bold",
lineHeight: "18px",
}}
>
There is a daily quota for the free SMS service, if you do not receive the SMS
please try again tomorrow.
</div>
)}
</div>
);
},
}}
>
{props.children}
</PasswordlessComponentsOverrideProvider>
);
};
Multi-factor auth
import ThirdParty, { Google, Github, Apple, Twitter } from "supertokens-auth-react/recipe/thirdparty";
import { ThirdPartyPreBuiltUI } from "supertokens-auth-react/recipe/thirdparty/prebuiltui";
import EmailPassword from "supertokens-auth-react/recipe/emailpassword";
import { EmailPasswordPreBuiltUI } from "supertokens-auth-react/recipe/emailpassword/prebuiltui";
import Passwordless from "supertokens-auth-react/recipe/passwordless";
import { PasswordlessPreBuiltUI } from "supertokens-auth-react/recipe/passwordless/prebuiltui";
import MultiFactorAuth from "supertokens-auth-react/recipe/multifactorauth";
import { MultiFactorAuthPreBuiltUI } from "supertokens-auth-react/recipe/multifactorauth/prebuiltui";
import EmailVerification from "supertokens-auth-react/recipe/emailverification";
import { EmailVerificationPreBuiltUI } from "supertokens-auth-react/recipe/emailverification/prebuiltui";
import TOTP from "supertokens-auth-react/recipe/totp";
import { TOTPPreBuiltUI } from "supertokens-auth-react/recipe/totp/prebuiltui";
import Session from "supertokens-auth-react/recipe/session";
export function getApiDomain() {
const apiPort = import.meta.env.VITE_APP_API_PORT || 3001;
const apiUrl = import.meta.env.VITE_APP_API_URL || `http://localhost:${apiPort}`;
return apiUrl;
}
export function getWebsiteDomain() {
const websitePort = import.meta.env.VITE_APP_WEBSITE_PORT || 3000;
const websiteUrl = import.meta.env.VITE_APP_WEBSITE_URL || `http://localhost:${websitePort}`;
return websiteUrl;
}
export const SuperTokensConfig = {
appInfo: {
appName: "SuperTokens Demo App",
apiDomain: getApiDomain(),
websiteDomain: getWebsiteDomain(),
},
// recipeList contains all the modules that you want to
// use from SuperTokens. See the full list here: https://supertokens.com/docs/guides
recipeList: [
ThirdParty.init({
signInAndUpFeature: {
providers: [Github.init(), Google.init(), Apple.init(), Twitter.init()],
},
}),
EmailPassword.init(),
Passwordless.init({
contactMethod: "EMAIL_OR_PHONE",
}),
MultiFactorAuth.init({ firstFactors: ["thirdparty", "emailpassword"] }),
EmailVerification.init({
mode: "REQUIRED",
}),
TOTP.init(),
Session.init(),
],
getRedirectionURL: async (context) => {
if (context.action === "SUCCESS" && context.newSessionCreated) {
return "/dashboard";
}
},
};
export const recipeDetails = {
docsLink: "https://supertokens.com/docs/mfa/introduction",
};
export const PreBuiltUIList = [
ThirdPartyPreBuiltUI,
EmailPasswordPreBuiltUI,
PasswordlessPreBuiltUI,
MultiFactorAuthPreBuiltUI,
EmailVerificationPreBuiltUI,
TOTPPreBuiltUI,
];
export const ComponentWrapper = (props: { children: JSX.Element }): JSX.Element => {
return props.children;
};
Multitenancy
import ThirdParty from "supertokens-auth-react/recipe/thirdparty";
import EmailPassword from "supertokens-auth-react/recipe/emailpassword";
import Passwordless from "supertokens-auth-react/recipe/passwordless";
import { ThirdPartyPreBuiltUI } from "supertokens-auth-react/recipe/thirdparty/prebuiltui";
import { EmailPasswordPreBuiltUI } from "supertokens-auth-react/recipe/emailpassword/prebuiltui";
import { PasswordlessPreBuiltUI } from "supertokens-auth-react/recipe/passwordless/prebuiltui";
import Session from "supertokens-auth-react/recipe/session";
import Multitenancy from "supertokens-auth-react/recipe/multitenancy";
export function getApiDomain() {
const apiPort = process.env.REACT_APP_API_PORT || 3001;
const apiUrl = process.env.REACT_APP_API_URL || `http://localhost:${apiPort}`;
return apiUrl;
}
export function getWebsiteDomain() {
const websitePort = process.env.REACT_APP_WEBSITE_PORT || 3000;
const websiteUrl = process.env.REACT_APP_WEBSITE_URL || `http://localhost:${websitePort}`;
return websiteUrl;
}
export const styleOverride = `
[data-supertokens~=tenants-link] {
margin-top: 8px;
}
`;
export const SuperTokensConfig = {
appInfo: {
appName: "SuperTokens Demo App",
apiDomain: getApiDomain(),
websiteDomain: getWebsiteDomain(),
},
usesDynamicLoginMethods: true,
// recipeList contains all the modules that you want to
// use from SuperTokens. See the full list here: https://supertokens.com/docs/guides
style: styleOverride,
recipeList: [
EmailPassword.init(),
ThirdParty.init(),
Passwordless.init({
contactMethod: "EMAIL",
}),
Session.init(),
Multitenancy.init({
override: {
functions: (oI) => {
return {
...oI,
getTenantId: async () => {
const tenantIdInStorage = localStorage.getItem("tenantId");
return tenantIdInStorage === null ? undefined : tenantIdInStorage;
},
};
},
},
}),
],
};
export const recipeDetails = {
docsLink: "https://supertokens.com/docs/multitenancy/introduction",
};
export const PreBuiltUIList = [ThirdPartyPreBuiltUI, EmailPasswordPreBuiltUI, PasswordlessPreBuiltUI];
These rules and examples apply only to template.ts - and nowhere outside of it.