(req, res)
| 1 | // File Path: frontend/api/auth/google/callback.js |
| 2 | export default async function handler(req, res) { |
| 3 | const { code } = req.query; |
| 4 | |
| 5 | if (!code) { |
| 6 | return res.status(400).send('Error: No authorization code provided.'); |
| 7 | } |
| 8 | |
| 9 | // ✅ Use the relative path. Vercel's proxy will handle it. |
| 10 | const backendUrl = `https://coding-platform-uyo1.vercel.app/api/login/oauth2/code/google`; |
| 11 | |
| 12 | try { |
| 13 | // Make the request to the Vercel URL, which gets proxied to your backend |
| 14 | const backendResponse = await fetch(`${backendUrl}?code=${code}`, { |
| 15 | method: 'GET', |
| 16 | redirect: 'manual' |
| 17 | }); |
| 18 | |
| 19 | const locationHeader = backendResponse.headers.get('location'); |
| 20 | |
| 21 | // If the backend sends a redirect, forward the user's browser to that new location |
| 22 | if (backendResponse.status === 302 && locationHeader) { |
| 23 | // The locationHeader from your backend should already contain the JWT token |
| 24 | // We just need to construct the full URL for the client-side redirect |
| 25 | const finalRedirectUrl = new URL(locationHeader, 'https://coding-platform-uyo1.vercel.app').toString(); |
| 26 | res.redirect(302, finalRedirectUrl); |
| 27 | } else { |
| 28 | // If something went wrong on the backend |
| 29 | const errorText = await backendResponse.text(); |
| 30 | res.status(backendResponse.status).send(`Error communicating with backend: ${errorText}`); |
| 31 | } |
| 32 | } catch (error) { |
| 33 | console.error('Error in Google callback handler:', error); |
| 34 | res.status(500).send('Internal Server Error'); |
| 35 | } |
| 36 | } |
nothing calls this directly
no outgoing calls
no test coverage detected