Fix P5: Outlook inbox sync via Microsoft Graph (read-only)#107
Open
bmoss2015 wants to merge 3 commits into
Open
Fix P5: Outlook inbox sync via Microsoft Graph (read-only)#107bmoss2015 wants to merge 3 commits into
bmoss2015 wants to merge 3 commits into
Conversation
Mirrors the Gmail integration so users can connect a Microsoft 365 /
Outlook.com mailbox and have it sync into the inbox alongside Gmail.
New files
- src/lib/email/microsoft-oauth.ts: OAuth helpers (authorize URL,
token exchange, refresh, /me userinfo). Tenant=common so personal
and work/school accounts both work. Reads MICROSOFT_OAUTH_CLIENT_ID
and MICROSOFT_OAUTH_CLIENT_SECRET.
- src/lib/email/outlook.ts: thin Graph REST client with token-refresh
retry. Uses the delta endpoint for both backfill and incremental
pulls and exposes attachment list/content helpers.
- src/app/api/oauth/microsoft/start/route.ts and callback/route.ts:
signed-in-only OAuth kickoff and callback. Stores tokens encrypted
in channel_accounts (provider='outlook'), then awaits the initial
sync so the inbox lands populated.
Changes
- src/lib/email/sync.ts: adds syncOutlookAccount + a generic
upsertConversationGeneric helper. The Outlook sync uses the Graph
/me/mailFolders('inbox')/messages/delta endpoint. The deltaLink is
persisted as sync_cursor; subsequent runs just GET that URL for the
incremental diff. 410 Gone (expired delta) triggers a clean
full-resync next pass.
syncAllActiveAccounts now sweeps both providers, so the existing
/api/cron/email-sync route picks up Outlook accounts with no
changes needed.
- EmailAccountsSection: empty state surfaces both Connect Gmail and
Connect Outlook buttons, and the per-account list has the matching
secondary links.
Schema
The channel_accounts.provider enum already includes 'outlook' from
the 0100_email_module migration, so no DB changes ship in this PR.
Scope cuts to land this safely
- Sending mail through Outlook is not wired yet. The Mail.Send scope
is requested at OAuth time so it's available, but the existing
email/send action only knows about Gmail. Adding Outlook send is a
follow-up.
- No UI differentiation between Gmail and Outlook inside the inbox
conversation lists; channel='outlook' is stored on messages and
conversations and the inbox query already treats both as 'email'.
Required env vars (please add in Vercel)
- MICROSOFT_OAUTH_CLIENT_ID (staging + prod)
- MICROSOFT_OAUTH_CLIENT_SECRET (staging + prod)
Redirect URI in Azure must be {origin}/api/oauth/microsoft/callback.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
This pull request has been ignored for the connected project Preview Branches by Supabase. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Bree already set MICROSOFT_CLIENT_ID and MICROSOFT_CLIENT_SECRET in the Vercel preview env. My code was reading the longer MICROSOFT_OAUTH_CLIENT_ID names which would have thrown 'MICROSOFT_OAUTH_CLIENT_ID is not set' on every OAuth attempt. Renamed to match the actual env values. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Mirrors the Gmail integration so users can connect a Microsoft 365 / Outlook.com mailbox and have it sync into the inbox alongside Gmail.
New files
src/lib/email/microsoft-oauth.ts— OAuth helpers (authorize URL, token exchange, refresh, /me userinfo).tenant=commonso both personal and work/school accounts work.src/lib/email/outlook.ts— Graph REST client with token-refresh retry, plus delta-endpoint listing and attachment download.src/app/api/oauth/microsoft/start/route.tsandcallback/route.ts— OAuth kickoff and callback mirroring the Google flow. Stores tokens encrypted inchannel_accounts(provider='outlook'), then awaits the initial sync so the inbox lands populated.Changes
src/lib/email/sync.ts— addssyncOutlookAccount+ a sharedupsertConversationGenerichelper. Uses the Graph/me/mailFolders('inbox')/messages/deltaendpoint; the@odata.deltaLinkis persisted assync_cursorfor incremental pulls. A 410 (expired delta) triggers a clean full-resync next pass.syncAllActiveAccountsnow sweeps both providers so the existing cron route picks up Outlook with no changes.EmailAccountsSection.tsx— empty state surfaces both Connect Gmail and Connect Outlook buttons; per-account list adds matching secondary links.Schema — no migration.
channel_accounts.provideralready includes'outlook'.Scope cuts (intentional)
channel='outlook'is stored on rows so we can color or icon them later.Required env vars (please add in Vercel)
MICROSOFT_OAUTH_CLIENT_ID— staging + prodMICROSOFT_OAUTH_CLIENT_SECRET— staging + prodRedirect URI in the Azure app registration must be:
https://<staging-preview-or-branch-domain>/api/oauth/microsoft/callbackhttps://app.nextsurplus.com/api/oauth/microsoft/callbackVerify on preview
Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com