-
-
Notifications
You must be signed in to change notification settings - Fork 132
feat(auth): send auth mail via Bento queue #3139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
riderx
wants to merge
8
commits into
main
Choose a base branch
from
cursor/053ed9be
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,167
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5860f91
feat(auth): send auth mail via Bento queue
riderx 4d2af74
fix(auth): use Bento auth_* event names
riderx d132b39
fix(auth): enqueue raw GoTrue send-email event
riderx 29271db
fix(auth): describe send_email as cron-drained queue job
riderx ce0f517
fix(test): include send_email in high_frequency_queues list
riderx fe67d80
chore: merge main into auth send-email branch
riderx 1377b8b
fix(auth): deliver email-change to the right addresses
riderx a5c8d51
fix(auth): keep canonical email in Bento payload
riderx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,54 @@ | ||
| import type { GoTrueSendEmailEvent } from '../utils/auth_email.ts' | ||
| import type { MiddlewareKeyVariables } from '../utils/hono.ts' | ||
| import { Hono } from 'hono/tiny' | ||
| import { | ||
| authEmailDeliveriesFromGoTrueEvent, | ||
| buildAuthEmailBentoDetails, | ||
| getAuthEmailBentoEvent, | ||
| } from '../utils/auth_email.ts' | ||
| import { isBentoConfigured, trackBentoEvent } from '../utils/bento.ts' | ||
| import { BRES, middlewareAPISecret, parseBody, quickError, simpleError } from '../utils/hono.ts' | ||
| import { cloudlog } from '../utils/logging.ts' | ||
| import { getEnv } from '../utils/utils.ts' | ||
|
|
||
| export const app = new Hono<MiddlewareKeyVariables>() | ||
|
|
||
| app.post('/', middlewareAPISecret, async (c) => { | ||
| const deliveries = authEmailDeliveriesFromGoTrueEvent(await parseBody<GoTrueSendEmailEvent>(c)) | ||
| .filter(item => item.payload.email_action_type) | ||
| if (deliveries.length === 0) | ||
| throw simpleError('invalid_payload', 'Invalid send_email payload') | ||
|
|
||
| if (!isBentoConfigured(c)) { | ||
| quickError(500, 'bento_not_configured', 'Bento is not configured for auth email delivery', { | ||
| email_action_type: deliveries[0]?.payload.email_action_type, | ||
| }) | ||
| } | ||
|
|
||
| const supabaseUrl = getEnv(c, 'SUPABASE_URL') | ||
| const webappUrl = getEnv(c, 'WEBAPP_URL') | ||
|
|
||
| for (const delivery of deliveries) { | ||
| const details = buildAuthEmailBentoDetails(delivery.payload, supabaseUrl, webappUrl) | ||
| const eventName = getAuthEmailBentoEvent(delivery.payload.email_action_type) | ||
|
|
||
| cloudlog({ | ||
| requestId: c.get('requestId'), | ||
| message: 'send_email queue message', | ||
| email_action_type: delivery.payload.email_action_type, | ||
| event: eventName, | ||
| has_confirmation_url: Boolean(details.confirmation_url), | ||
| has_token: Boolean(details.token), | ||
| }) | ||
|
|
||
| const result = await trackBentoEvent(c, delivery.email, { ...details }, eventName) | ||
| if (result === false) { | ||
| quickError(500, 'bento_auth_email_delivery_failed', 'Bento auth email delivery failed', { | ||
| email_action_type: delivery.payload.email_action_type, | ||
| event: eventName, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return c.json(BRES) | ||
| }) | ||
This file contains hidden or 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,196 @@ | ||
| import { trimTrailingSlashes } from './utils.ts' | ||
|
|
||
| export const AUTH_EMAIL_EVENT_PREFIX = 'auth_' | ||
|
|
||
| export const AUTH_EMAIL_EVENTS = { | ||
| email_change: 'auth_email_change', | ||
| email_change_current: 'auth_email_change', | ||
| email_change_new: 'auth_email_change', | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| email_changed_notification: 'auth_email_changed_notification', | ||
| invite: 'auth_invite', | ||
| magiclink: 'auth_magic_link', | ||
| mfa_factor_enrolled_notification: 'auth_mfa_factor_enrolled_notification', | ||
| mfa_factor_unenrolled_notification: 'auth_mfa_factor_unenrolled_notification', | ||
| password_changed_notification: 'auth_password_changed_notification', | ||
| reauthentication: 'auth_reauthentication', | ||
| recovery: 'auth_recovery', | ||
| signup: 'auth_confirmation', | ||
| } as const | ||
|
|
||
| export interface GoTrueSendEmailEvent { | ||
| email_data?: { | ||
| email_action_type?: string | ||
| factor_type?: string | ||
| new_email?: string | ||
| old_email?: string | ||
| redirect_to?: string | ||
| site_url?: string | ||
| token?: string | ||
| token_hash?: string | ||
| token_hash_new?: string | ||
| token_new?: string | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| user?: { | ||
| email?: string | ||
| new_email?: string | ||
| } | ||
| } | ||
|
|
||
| export interface AuthEmailPayload { | ||
| email: string | ||
| email_action_type: string | ||
| factor_type?: string | ||
| new_email?: string | ||
| old_email?: string | ||
| redirect_to?: string | ||
| site_url?: string | ||
| token?: string | ||
| token_hash?: string | ||
| } | ||
|
|
||
| export interface AuthEmailDelivery { | ||
| email: string | ||
| payload: AuthEmailPayload | ||
| } | ||
|
|
||
| export interface AuthEmailBentoDetails { | ||
| confirmation_link: string | ||
| confirmation_url: string | ||
| email: string | ||
| factor_type: string | ||
| new_email: string | ||
| old_email: string | ||
| site_url: string | ||
| token: string | ||
| } | ||
|
|
||
| function textField(value: string | undefined): string { | ||
| return typeof value === 'string' ? value.trim() : '' | ||
| } | ||
|
|
||
| function verifyType(emailActionType: string): string { | ||
| if (emailActionType === 'email_change_current' || emailActionType === 'email_change_new') | ||
| return 'email_change' | ||
| return emailActionType | ||
| } | ||
|
|
||
| function delivery( | ||
| email: string, | ||
| event: GoTrueSendEmailEvent, | ||
| token: string, | ||
| tokenHash: string, | ||
| ): AuthEmailDelivery { | ||
| const emailData = event.email_data ?? {} | ||
| const user = event.user ?? {} | ||
| return { | ||
| email, | ||
|
riderx marked this conversation as resolved.
|
||
| payload: { | ||
| email: textField(user.email) || email, | ||
| email_action_type: verifyType(textField(emailData.email_action_type)), | ||
| factor_type: textField(emailData.factor_type), | ||
| new_email: textField(user.new_email) || textField(emailData.new_email), | ||
| old_email: textField(emailData.old_email), | ||
| redirect_to: textField(emailData.redirect_to), | ||
| site_url: textField(emailData.site_url), | ||
| token, | ||
| token_hash: tokenHash, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export function authEmailDeliveriesFromGoTrueEvent(event: GoTrueSendEmailEvent): AuthEmailDelivery[] { | ||
| const emailData = event.email_data ?? {} | ||
| const user = event.user ?? {} | ||
| const actionType = textField(emailData.email_action_type) | ||
| const currentEmail = textField(user.email) | ||
| const newEmail = textField(user.new_email) || textField(emailData.new_email) | ||
| const oldEmail = textField(emailData.old_email) | ||
| const token = textField(emailData.token) | ||
| const tokenNew = textField(emailData.token_new) | ||
| const tokenHash = textField(emailData.token_hash) | ||
| const tokenHashNew = textField(emailData.token_hash_new) | ||
|
|
||
| if (actionType === 'email_change' && tokenHash && tokenHashNew) { | ||
| return [ | ||
| delivery(currentEmail, event, token, tokenHashNew), | ||
| delivery(newEmail || currentEmail, event, tokenNew, tokenHash), | ||
| ].filter(item => item.email) | ||
| } | ||
|
|
||
| if (actionType === 'email_change_current') | ||
| return [delivery(currentEmail, event, token, tokenHashNew || tokenHash)].filter(item => item.email) | ||
|
|
||
| if (actionType === 'email_change_new' || actionType === 'email_change') { | ||
| return [delivery( | ||
| newEmail || currentEmail, | ||
| event, | ||
| tokenNew || token, | ||
| tokenHash, | ||
| )].filter(item => item.email) | ||
| } | ||
|
|
||
| if (actionType === 'email_changed_notification') | ||
| return [delivery(oldEmail || currentEmail, event, token, tokenHash)].filter(item => item.email) | ||
|
|
||
| return [delivery(currentEmail, event, token, tokenHash)].filter(item => item.email) | ||
| } | ||
|
|
||
| export function getAuthEmailBentoEvent(emailActionType: string): string { | ||
| const actionType = textField(emailActionType) | ||
| if (!actionType) | ||
| return `${AUTH_EMAIL_EVENT_PREFIX}unknown` | ||
|
|
||
| return AUTH_EMAIL_EVENTS[actionType as keyof typeof AUTH_EMAIL_EVENTS] | ||
| ?? AUTH_EMAIL_EVENTS[verifyType(actionType) as keyof typeof AUTH_EMAIL_EVENTS] | ||
| ?? `${AUTH_EMAIL_EVENT_PREFIX}${actionType}` | ||
| } | ||
|
|
||
| export function buildAuthConfirmationUrl( | ||
| supabaseUrl: string, | ||
| tokenHash: string, | ||
| emailActionType: string, | ||
| redirectTo: string, | ||
| ): string { | ||
| const baseUrl = trimTrailingSlashes(textField(supabaseUrl)) | ||
| const hash = textField(tokenHash) | ||
| const actionType = verifyType(textField(emailActionType)) | ||
| if (!baseUrl || !hash || !actionType) | ||
| return '' | ||
|
|
||
| const params = new URLSearchParams({ | ||
| token: hash, | ||
| type: actionType, | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| }) | ||
| const redirect = textField(redirectTo) | ||
| if (redirect) | ||
| params.set('redirect_to', redirect) | ||
|
|
||
| return `${baseUrl}/auth/v1/verify?${params.toString()}` | ||
| } | ||
|
|
||
| export function buildAuthEmailBentoDetails( | ||
| payload: AuthEmailPayload, | ||
| supabaseUrl: string, | ||
| webappUrl: string, | ||
| ): AuthEmailBentoDetails { | ||
| const siteUrl = trimTrailingSlashes(textField(webappUrl)) || trimTrailingSlashes(textField(payload.site_url)) | ||
| const confirmationUrl = buildAuthConfirmationUrl( | ||
| supabaseUrl, | ||
| payload.token_hash ?? '', | ||
| payload.email_action_type, | ||
| payload.redirect_to ?? '', | ||
| ) | ||
|
|
||
| return { | ||
| confirmation_link: siteUrl && confirmationUrl | ||
| ? `${siteUrl}/confirm-signup?confirmation_url=${encodeURIComponent(confirmationUrl)}` | ||
| : '', | ||
| confirmation_url: confirmationUrl, | ||
| email: textField(payload.email), | ||
| factor_type: textField(payload.factor_type), | ||
| new_email: textField(payload.new_email), | ||
| old_email: textField(payload.old_email), | ||
| site_url: siteUrl, | ||
| token: textField(payload.token), | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.