-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user): add email verification recipe (#482)
- Loading branch information
1 parent
61530ef
commit 3d24b17
Showing
12 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...ages/user/src/supertokens/recipes/config/email-verification/sendEmailVerificationEmail.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import emailVerification from "supertokens-node/recipe/emailverification"; | ||
|
||
import getOrigin from "../../../../lib/getOrigin"; | ||
import sendEmail from "../../../../lib/sendEmail"; | ||
|
||
import type { FastifyInstance, FastifyRequest } from "fastify"; | ||
import type { EmailDeliveryInterface } from "supertokens-node/lib/build/ingredients/emaildelivery/types"; | ||
import type { TypeEmailVerificationEmailDeliveryInput } from "supertokens-node/recipe/emailverification/types"; | ||
|
||
const sendEmailVerificationEmail = ( | ||
originalImplementation: EmailDeliveryInterface<TypeEmailVerificationEmailDeliveryInput>, | ||
fastify: FastifyInstance | ||
): typeof emailVerification.sendEmail => { | ||
const websiteDomain = fastify.config.appOrigin[0] as string; | ||
|
||
return async (input) => { | ||
const request: FastifyRequest = input.userContext._default.request.request; | ||
|
||
const url = | ||
request.headers.referer || request.headers.origin || request.hostname; | ||
|
||
const origin = getOrigin(url) || websiteDomain; | ||
|
||
const emailVerifyLink = input.emailVerifyLink.replace( | ||
websiteDomain, | ||
origin | ||
); | ||
|
||
sendEmail({ | ||
fastify, | ||
subject: "Email Verification", | ||
templateName: "email-verification", | ||
to: input.user.email, | ||
templateData: { | ||
emailVerifyLink, | ||
}, | ||
}); | ||
}; | ||
}; | ||
|
||
export default sendEmailVerificationEmail; |
43 changes: 43 additions & 0 deletions
43
packages/user/src/supertokens/recipes/config/emailVerificationRecipeConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import sendEmailVerificationEmail from "./email-verification/sendEmailVerificationEmail"; | ||
import { EMAIL_VERIFICATION_MODE } from "../../../constants"; | ||
|
||
import type { | ||
EmailVerificationSendEmailWrapper as SendEmailWrapper, | ||
EmailVerificationRecipe, | ||
} from "../../types"; | ||
import type { FastifyInstance } from "fastify"; | ||
import type { TypeInput as EmailVerificationRecipeConfig } from "supertokens-node/recipe/emailverification/types"; | ||
|
||
const getEmailVerificationRecipeConfig = ( | ||
fastify: FastifyInstance | ||
): EmailVerificationRecipeConfig => { | ||
const { config } = fastify; | ||
|
||
let emailVerification: EmailVerificationRecipe = {}; | ||
|
||
if (typeof config.user.supertokens.recipes?.emailVerification === "object") { | ||
emailVerification = config.user.supertokens.recipes.emailVerification; | ||
} | ||
|
||
return { | ||
mode: emailVerification?.mode || EMAIL_VERIFICATION_MODE, | ||
emailDelivery: { | ||
override: (originalImplementation) => { | ||
let sendEmailConfig: SendEmailWrapper | undefined; | ||
|
||
if (emailVerification?.sendEmail) { | ||
sendEmailConfig = emailVerification.sendEmail; | ||
} | ||
|
||
return { | ||
...originalImplementation, | ||
sendEmail: sendEmailConfig | ||
? sendEmailConfig(originalImplementation, fastify) | ||
: sendEmailVerificationEmail(originalImplementation, fastify), | ||
}; | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export default getEmailVerificationRecipeConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/user/src/supertokens/recipes/initEmailVerificationRecipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import EmailVerification from "supertokens-node/recipe/emailverification"; | ||
|
||
import getEmailVerificationRecipeConfig from "./config/emailVerificationRecipeConfig"; | ||
|
||
import type { SupertokensRecipes } from "../types"; | ||
import type { FastifyInstance } from "fastify"; | ||
|
||
const init = (fastify: FastifyInstance) => { | ||
const emailVerification: SupertokensRecipes["emailVerification"] = | ||
fastify.config.user.supertokens.recipes?.emailVerification; | ||
|
||
if (typeof emailVerification === "function") { | ||
return EmailVerification.init(emailVerification(fastify)); | ||
} | ||
|
||
return EmailVerification.init(getEmailVerificationRecipeConfig(fastify)); | ||
}; | ||
|
||
export default init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters