Manual actions
Overview
Although the SuperTokens covers the entire email verification process you can also intervene manually in the process. The following page shows you what SDK methods you can use to adjust the verification flow.
Generate a link
You can use the backend SDK to generate the email verification link as shown below:
import EmailVerification from "supertokens-node/recipe/emailverification";
import supertokens from "supertokens-node";
async function createEmailVerificationLink(recipeUserId: supertokens.RecipeUserId, email: string) {
try {
// Create an email verification link for the user
const linkResponse = await EmailVerification.createEmailVerificationLink("public", recipeUserId, email);
if (linkResponse.status === "OK") {
console.log(linkResponse.link);
} else {
// user's email is already verified
}
} catch (err) {
console.error(err);
}
}
Notice that the first argument to the function call above is "public"
. This refers to the default tenant ID that SuperTokens uses. It means that users belonging to the "public"
tenant can only consume the generated email verification link.
If you are using the multi tenancy feature, you can pass in the tenantId
that contains this user, which you can fetch by getting the user object for this userId
.
Finally, the generated link uses the configured websiteDomain
from the appInfo
object (in supertokens.init
), however, you can change the domain of the generated link to match that of the tenant ID.
Mark the email as verified
To manually mark an email as verified, you need to first create an email verification token for the user and then use the token to verify the user's email.
import EmailVerification from "supertokens-node/recipe/emailverification";
import supertokens from "supertokens-node";
async function manuallyVerifyEmail(recipeUserId: supertokens.RecipeUserId) {
try {
// Create an email verification token for the user
const tokenRes = await EmailVerification.createEmailVerificationToken("public", recipeUserId);
// If the token creation is successful, use the token to verify the user's email
if (tokenRes.status === "OK") {
await EmailVerification.verifyEmailUsingToken("public", tokenRes.token);
}
} catch (err) {
console.error(err);
}
}
Notice that the first argument of the function call above is "public"
.
This refers to the "public"
tenantId
(which is the default tenantId
).
In case you are using the multi tenancy feature, you can still pass in the "public"
tenant ID here. Even if the user ID is not part of that tenant, you can pass it because the system creates and consumes the token in one shot.
Mark the email as unverified
To manually mark an email as unverified, you need to first retrieve the user's email address and then update their email verification status in the database.
import EmailVerification from "supertokens-node/recipe/emailverification";
import supertokens from "supertokens-node";
async function manuallyUnverifyEmail(recipeUserId: supertokens.RecipeUserId) {
try {
// Set email verification status to false
await EmailVerification.unverifyEmail(recipeUserId);
} catch (err) {
console.error(err);
}
}
For a multi tenant setup, the function above does not take a tenant ID. A user ID and the associated email verification status is unique on an app level (and not a tenant level).