From fc657354116d3b513c2d2270600fd495330ef9f2 Mon Sep 17 00:00:00 2001 From: nnamdiarimah <10407499+nnamdiarimah@users.noreply.github.com> Date: Tue, 2 Dec 2025 22:21:42 -0500 Subject: [PATCH] wip --- src/routes/gmail.ts | 96 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 21 deletions(-) diff --git a/src/routes/gmail.ts b/src/routes/gmail.ts index 1eab0eb..088118c 100644 --- a/src/routes/gmail.ts +++ b/src/routes/gmail.ts @@ -58,29 +58,83 @@ router.get('/oauth/callback', asyncHandler(async (req: Request, res: Response) = console.log('📱 Received OAuth callback:', { code: !!code, state, error }); - // Build the deep link to redirect back to the app - const deepLink = `snacktrack://oauth/callback?${new URLSearchParams({ - ...(code && { code: code as string }), - ...(state && { state: state as string }), - ...(error && { error: error as string }), - }).toString()}`; + if (error) { + // OAuth error - redirect back with error + const deepLink = `snacktrack://oauth/callback?error=${encodeURIComponent(error as string)}`; + return res.send(` + + + + Authorization Failed + + + +

Authorization failed. Redirecting back to app...

+

If you're not redirected, click here.

+ + + `); + } - // For web, show a simple HTML page that redirects - const html = ` - - - - Redirecting... - - - -

Redirecting back to app...

-

If you're not redirected, click here.

- - - `; + if (!code) { + return res.status(400).send('Missing authorization code'); + } - res.send(html); + try { + // Exchange code for tokens + const oAuth2Client = getOAuth2Client(); + const { tokens } = await oAuth2Client.getToken(code as string); + + console.log('✅ Exchanged code for tokens'); + + // Redirect back to app with access token + const deepLink = `snacktrack://oauth/callback?${new URLSearchParams({ + access_token: tokens.access_token!, + ...(state && { state: state as string }), + }).toString()}`; + + // Show success page with auto-redirect + res.send(` + + + + Success! + + + +

✅ Authorization Successful!

+

Redirecting back to SnackTrack...

+

Click here if you're not redirected automatically

+

You can close this window and return to the app.

+ + + `); + } catch (error) { + console.error('❌ Error exchanging code for tokens:', error); + const deepLink = `snacktrack://oauth/callback?error=${encodeURIComponent('token_exchange_failed')}`; + res.send(` + + + + Error + + + +

Failed to complete authorization. Redirecting back to app...

+

If you're not redirected, click here.

+ + + `); + } })); /**