feat: email verification (#9081)

* Verify - inital email commit

* Add token type - api redirect - migration

* Redirect and valid api callback route

* Update email design

* Change signup URL to redirect to verify-email

* Add feature flag - add a11y text to email - add top banner

* Shell shouldnt redirect to onboarding if the user needs to verify account

* Move flag check to server

* Cleanup

* Rate limit

* Fix redirects

* Remove api signup mess

* Double negation for forced bool

* Fix props

* Update packages/emails/templates/account-verify-email.ts

* Enable migration by default

* Fix typos

* Fix google verify issue

* Update packages/features/auth/lib/verifyEmail.ts

Co-authored-by: Hariom Balhara <[email protected]>

* NITS: @harioms addressed

* Remove schema changes

* Fix NITs+ improvments

* Update apps/web/pages/api/auth/verify-email.ts

Co-authored-by: Omar López <[email protected]>

* Update packages/features/ee/common/components/LicenseRequired.tsx

Co-authored-by: Omar López <[email protected]>

* Update apps/web/pages/api/auth/verify-email.ts

Co-authored-by: Omar López <[email protected]>

* Always preloads feature flags

* Update verifyEmail.ts

* Update schema.prisma

* Type fix

---------

Co-authored-by: Hariom Balhara <[email protected]>
Co-authored-by: Omar López <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-06-07 07:27:48 +00:00
1 parent 057cf273dd
commit 9e70a985e4
23 files changed
+493 -21

No files matched your search

@@ -25,6 +25,7 @@ export async function getUserFromSession(ctx: TRPCContextInner, session: Maybe<S
username: true,
name: true,
email: true,
emailVerified: true,
bio: true,
timeZone: true,
weekStart: true,
@@ -18,6 +18,7 @@ import { ZUpdateUserDefaultConferencingAppInputSchema } from "./updateUserDefaul
type AppsRouterHandlerCache = {
me?: typeof import("./me.handler").meHandler;
shouldVerifyEmail?: typeof import("./shouldVerifyEmail.handler").shouldVerifyEmailHandler;
avatar?: typeof import("./avatar.handler").avatarHandler;
deleteMe?: typeof import("./deleteMe.handler").deleteMeHandler;
deleteMeWithoutPassword?: typeof import("./deleteMeWithoutPassword.handler").deleteMeWithoutPasswordHandler;
@@ -368,4 +369,18 @@ export const loggedInViewerRouter = router({
return UNSTABLE_HANDLER_CACHE.updateUserDefaultConferencingApp({ ctx, input });
}),
shouldVerifyEmail: authedProcedure.query(async ({ ctx }) => {
if (!UNSTABLE_HANDLER_CACHE.shouldVerifyEmail) {
UNSTABLE_HANDLER_CACHE.shouldVerifyEmail = (
await import("./shouldVerifyEmail.handler")
).shouldVerifyEmailHandler;
}
// Unreachable code but required for type safety
if (!UNSTABLE_HANDLER_CACHE.shouldVerifyEmail) {
throw new Error("Failed to load handler");
}
return UNSTABLE_HANDLER_CACHE.shouldVerifyEmail({ ctx });
}),
});
@@ -0,0 +1,21 @@
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
type ShouldVerifyEmailType = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
};
export const shouldVerifyEmailHandler = async ({ ctx }: ShouldVerifyEmailType) => {
const { user } = ctx;
const isVerified = !!user.emailVerified;
const isCalProvider = user.identityProvider === "CAL"; // We dont need to verify on OAUTH providers as they are already verified by the provider
const obj = {
id: user.id,
email: user.email,
isVerified: isVerified || !isCalProvider,
};
return obj;
};
@@ -0,0 +1 @@
export {};
@@ -6,6 +6,7 @@ import { ZVerifyPasswordInputSchema } from "./verifyPassword.schema";
type AuthRouterHandlerCache = {
changePassword?: typeof import("./changePassword.handler").changePasswordHandler;
verifyPassword?: typeof import("./verifyPassword.handler").verifyPasswordHandler;
resendVerifyEmail?: typeof import("./resendVerifyEmail.handler").resendVerifyEmail;
};
const UNSTABLE_HANDLER_CACHE: AuthRouterHandlerCache = {};
@@ -46,4 +47,19 @@ export const authRouter = router({
input,
});
}),
resendVerifyEmail: authedProcedure.mutation(async ({ ctx }) => {
if (!UNSTABLE_HANDLER_CACHE.resendVerifyEmail) {
UNSTABLE_HANDLER_CACHE.resendVerifyEmail = await import("./resendVerifyEmail.handler").then(
(mod) => mod.resendVerifyEmail
);
}
if (!UNSTABLE_HANDLER_CACHE.resendVerifyEmail) {
throw new Error("Failed to load handler");
}
return UNSTABLE_HANDLER_CACHE.resendVerifyEmail({
ctx,
});
}),
});
@@ -0,0 +1,30 @@
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
import logger from "@calcom/lib/logger";
import type { TrpcSessionUser } from "../../../trpc";
type ResendEmailOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
};
const log = logger.getChildLogger({ prefix: [`[[Auth] `] });
export const resendVerifyEmail = async ({ ctx }: ResendEmailOptions) => {
if (ctx.user.emailVerified) {
log.info(`User ${ctx.user.id} already verified email`);
return {
ok: true,
skipped: true,
};
}
const email = await sendEmailVerification({
email: ctx.user.email,
username: ctx.user?.username ?? undefined,
language: ctx.user.locale,
});
return email;
};