feat: migrate auth from supabase to better auth (#596)
* feat: add README for @cio/db package and remove existingAccounts.json * load session, load profile and organization, add create profile hook * chore: cache session request The trick was passing all the cookies to the backend. Previously I was only passing the better-auth.session_token and not better-auth.session_data. Read about the difference here https://www.better-auth.com/docs/concepts/cookies#custom-cookies * feat: login, logout, onboarding, verify email, reset password. * feat: complete change email and email verification * feat: standardise how we make rpc calls and create org endpoints * fix ui errors * fix: handle potential undefined customerId and standardize avatar URL property * refactor: standardize custom domain handling and update telegram chat ID references * chore: add tailwind prefix to @cio/ui this fixed the media query issues because @cio/ui classes were overriding this dashboard tailwind build * chore: clean up org sidebar * chore: persist mode state * feat: implement order breadcrumbs * chore: upgrade to better auth 1.4 * chore: address common auth flow bugs and setup app for others to setup locally.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
export const EMAIL_IDS = ['forgotPassword', 'welcome', 'verifyEmail', 'onPasswordReset'] as const;
|
||||
|
||||
export const EMAIL_FROM = '"Best from ClassroomIO" <[email protected]>';
|
||||
export const EMAIL_REPLY_TO = '"Best from ClassroomIO" <[email protected]>';
|
||||
@@ -0,0 +1,18 @@
|
||||
import { FromData } from '../types';
|
||||
|
||||
// format: "ClassroomIO Developers (via ClassroomIO.com)" <[email protected]>
|
||||
export function extractNameAndEmail(str: string): FromData | undefined {
|
||||
// Use regular expressions to match the name and email
|
||||
const regex = /"(.*?)"\s+<\s*(.*?)@(.*?)\s*>/;
|
||||
const match = str.match(regex);
|
||||
|
||||
if (match) {
|
||||
// Extract the name and email from the match groups
|
||||
const name = match[1];
|
||||
const email = match[2] + '@' + match[3];
|
||||
return { name, email };
|
||||
} else {
|
||||
// Return undefined if the format doesn't match
|
||||
return { name: str, email: str };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { EmailResponse } from '../types';
|
||||
import type { TEmailData } from '@cio/utils/validation/mail';
|
||||
import type { Transporter } from 'nodemailer';
|
||||
import { env } from '../../config/env';
|
||||
import nodemailer from 'nodemailer';
|
||||
|
||||
let transporter: Transporter | undefined;
|
||||
|
||||
const setupTransporter = async () => {
|
||||
if (!env.SMTP_HOST || !env.SMTP_PORT || !env.SMTP_USER || !env.SMTP_PASSWORD) {
|
||||
console.error('SMTP configuration missing');
|
||||
}
|
||||
|
||||
try {
|
||||
transporter = nodemailer.createTransport({
|
||||
host: env.SMTP_HOST,
|
||||
port: parseInt(env.SMTP_PORT || '465', 10),
|
||||
secure: true,
|
||||
auth: {
|
||||
user: env.SMTP_USER,
|
||||
pass: env.SMTP_PASSWORD
|
||||
}
|
||||
});
|
||||
|
||||
await transporter.verify();
|
||||
|
||||
return transporter;
|
||||
} catch (error) {
|
||||
console.error('Transporter error:', error);
|
||||
}
|
||||
};
|
||||
|
||||
setupTransporter();
|
||||
|
||||
export async function sendWithNodemailer(emailData: TEmailData): Promise<EmailResponse> {
|
||||
const { from, to, subject, content, replyTo } = emailData;
|
||||
|
||||
if (!transporter) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Email transporter not initialized'
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await transporter.sendMail({
|
||||
from: from || env.SMTP_SENDER,
|
||||
to,
|
||||
subject,
|
||||
replyTo,
|
||||
html: content
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
details: result
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error occurred',
|
||||
details: error
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { EmailResponse } from '../types';
|
||||
import { SendMailClient } from 'zeptomail';
|
||||
import type { TEmailData } from '@cio/utils/validation/mail';
|
||||
import { env } from '../../config/env';
|
||||
import { extractNameAndEmail } from '../functions/email-helpers';
|
||||
|
||||
export const zohoClient = new SendMailClient({
|
||||
url: 'api.zeptomail.eu/',
|
||||
token: env.ZOHO_TOKEN!
|
||||
});
|
||||
|
||||
export async function sendWithZoho(emailData: TEmailData): Promise<EmailResponse> {
|
||||
const { from, to, subject, content, replyTo } = emailData;
|
||||
|
||||
const fromData = extractNameAndEmail(from || '');
|
||||
|
||||
if (!fromData) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Invalid from data'
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await zohoClient.sendMail({
|
||||
from: {
|
||||
address: fromData.email,
|
||||
name: fromData.name
|
||||
},
|
||||
to: [
|
||||
{
|
||||
email_address: {
|
||||
address: to
|
||||
}
|
||||
}
|
||||
],
|
||||
subject,
|
||||
htmlbody: content,
|
||||
reply_to: getReplyTo(replyTo)
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
details: result
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error sending email with Zoho:', error);
|
||||
console.log(error.error.details?.[0]);
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error occurred',
|
||||
details: error
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function getReplyTo(replyTo?: string): { address: string; name: string }[] | undefined {
|
||||
if (!replyTo) return undefined;
|
||||
|
||||
const fromData = extractNameAndEmail(replyTo);
|
||||
if (!fromData) return undefined;
|
||||
|
||||
return [
|
||||
{
|
||||
address: fromData.email,
|
||||
name: fromData.name
|
||||
}
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { EMAIL_IDS } from './constants';
|
||||
import type { EmailSchemas } from '../emails';
|
||||
|
||||
export interface EmailResponse {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
details?: unknown;
|
||||
}
|
||||
|
||||
export interface FromData {
|
||||
name: string;
|
||||
email: string;
|
||||
}
|
||||
export type EmailId = (typeof EMAIL_IDS)[number];
|
||||
|
||||
/**
|
||||
* Conditional type that looks up the schema type for a given EmailId
|
||||
*
|
||||
* @example
|
||||
* type ForgotPasswordSchema = EmailSchemaFor<'forgotPassword'>;
|
||||
*/
|
||||
export type EmailSchemaFor<TEmailId extends EmailId> = TEmailId extends keyof EmailSchemas
|
||||
? EmailSchemas[TEmailId]
|
||||
: never;
|
||||
Reference in New Issue
Block a user