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,64 @@
|
||||
import { type BetterAuthOptions } from 'better-auth';
|
||||
import { sendEmail } from '@cio/email';
|
||||
|
||||
type EmailVerificationOptions = Parameters<
|
||||
NonNullable<NonNullable<BetterAuthOptions['emailVerification']>['sendVerificationEmail']>
|
||||
>[0];
|
||||
|
||||
type ChangeEmailConfirmationOptions = Parameters<
|
||||
NonNullable<NonNullable<NonNullable<BetterAuthOptions['user']>['changeEmail']>['sendChangeEmailConfirmation']>
|
||||
>[0];
|
||||
|
||||
/**
|
||||
* Email verification flow.
|
||||
*
|
||||
* 1. user says I want to change my email from [email protected] to [email protected]
|
||||
* 2. we send an email to [email protected] asking user to verify if they really want to change their email to [email protected] `sendChangeEmailConfirmation()`. [better-auth].
|
||||
* 3. user clicks the link in the email
|
||||
* 4. we update the email in the database. [better-auth].
|
||||
* 5. we send a confirmation email to [email protected] `sendVerificationEmail()`. [dashboard].
|
||||
* 6. user clicks the link in the email
|
||||
* 7. we mark [email protected] as verified. [better-auth].
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sends a verification email to the user when they sign up or change their email address.
|
||||
* @param options - The options for sending the verification email.
|
||||
* @returns The verification email.
|
||||
*/
|
||||
export const sendVerificationEmail = async (options: EmailVerificationOptions) => {
|
||||
const { user, url } = options;
|
||||
console.log('\nsendVerificationEmail');
|
||||
|
||||
// The purpose of this is to prevent verification email trigger from better-auth because we already have a flow in the UI for triggering this email.
|
||||
if (!url.includes('trigger=app') && !url.includes('trigger%3Dapp')) {
|
||||
return;
|
||||
}
|
||||
|
||||
await sendEmail('verifyEmail', {
|
||||
to: user.email,
|
||||
fields: {
|
||||
link: url,
|
||||
userName: user.name || undefined
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends a confirmation email to the user when they change their email address.
|
||||
* @param options - The options for sending the change email confirmation.
|
||||
* @returns The confirmation email.
|
||||
*/
|
||||
export const sendChangeEmailConfirmation = async (options: ChangeEmailConfirmationOptions) => {
|
||||
const { user, newEmail, url } = options;
|
||||
console.log('\nsendChangeEmailConfirmation');
|
||||
|
||||
await sendEmail('verifyEmail', {
|
||||
to: user.email,
|
||||
fields: {
|
||||
link: url,
|
||||
newEmail,
|
||||
userName: user.name || undefined
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user