Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Add welcome email functionality to authentication flow

Bill Yang0457f36b14f5
diff --git a/server/src/lib/auth.ts b/server/src/lib/auth.ts
index 3f27048bc..6a4837d08 100644
--- a/server/src/lib/auth.ts
+++ b/server/src/lib/auth.ts
@@ -1,4 +1,5 @@
import { betterAuth } from "better-auth";
+import { createAuthMiddleware } from "better-auth/api";
import { admin, captcha, emailOTP, organization, apiKey } from "better-auth/plugins";
import dotenv from "dotenv";
import { asc, eq } from "drizzle-orm";
@@ -8,12 +9,10 @@ import { db } from "../db/postgres/postgres.js";
import * as schema from "../db/postgres/schema.js";
import { user } from "../db/postgres/schema.js";
import { DISABLE_SIGNUP, IS_CLOUD } from "./const.js";
-import { sendEmail, sendInvitationEmail } from "./email/email.js";
+import { sendEmail, sendInvitationEmail, sendWelcomeEmail } from "./email/email.js";
dotenv.config();
-type AuthType = ReturnType<typeof betterAuth> | null;
-
const pluginList = [
admin(),
apiKey(),
@@ -176,4 +175,14 @@ export const auth = betterAuth({
},
},
},
+ hooks: {
+ after: createAuthMiddleware(async ctx => {
+ if (ctx.path.startsWith("/sign-up") && IS_CLOUD) {
+ const newSession = ctx.context.newSession;
+ if (newSession) {
+ sendWelcomeEmail(newSession.user.email, newSession.user.name);
+ }
+ }
+ }),
+ },
});
diff --git a/server/src/lib/email/email.ts b/server/src/lib/email/email.ts
index 699aa6efa..2f84bb682 100644
--- a/server/src/lib/email/email.ts
+++ b/server/src/lib/email/email.ts
@@ -86,3 +86,31 @@ export const sendWeeklyReportEmail = async (
await sendEmail(email, subject, html);
};
+
+export const sendWelcomeEmail = async (email: string, name?: string) => {
+ if (!resend) return;
+
+ const greeting = name ? `Hi ${name}` : "Hi there";
+ const text = `${greeting},
+
+Welcome to Rybbit! Thanks for signing up.
+
+I'm excited to have you on board. Rybbit is fully self-funded and we're fully committed to making an analytics platform that only serves the interests of our users.
+
+If you run into any issues or have any questions or suggestions, just reply to this email - I'd love to hear from you.
+
+Cheers,
+Bill`;
+
+ try {
+ await resend.emails.send({
+ from: "Bill from Rybbit <[email protected]>",
+ replyTo: "[email protected]",
+ to: email,
+ subject: "Welcome to Rybbit!",
+ text,
+ });
+ } catch (error) {
+ console.error("Failed to send welcome email:", error);
+ }
+};