Google OAuth authentication has been successfully integrated into your ChatApp frontend. Users can now login and signup using their Google accounts.
- Handles Google OAuth flow
loginWithGoogle(): Redirects to backend Google OAuth endpoint (/api/auth/google)handleGoogleCallback(): Fetches user data after successful OAuth authentication
- Reusable button component with Google icon
- Styled to match your existing UI theme
- Can be customized with different text
- Handles the OAuth callback after Google authentication
- Shows a loading screen while processing authentication
- Redirects to home page on success, login page on failure
- Login Page: Added "Login with Google" button with OR divider
- SignUp Page: Added "Sign up with Google" button with OR divider
- Added route:
/auth/google/callbackfor handling OAuth callback
- User clicks "Login with Google" or "Sign up with Google"
- Frontend redirects to:
http://localhost:5000/api/auth/google - Backend handles Google OAuth and redirects back to:
http://localhost:3000/auth/google/callback - Frontend fetches user data from
/api/auth/me - User data is stored in localStorage and AuthContext
- User is redirected to the home page
The vite.config.js is configured to proxy /api requests to http://localhost:5000:
proxy: {
"/api": {
target: "http://localhost:5000",
changeOrigin: true,
secure: false,
},
}The vercel.json is configured to proxy API requests to your production backend:
{
"rewrites": [
{
"source": "/api/:path*",
"destination": "https://chatapp-backend-1-2k8y.onrender.com/api/:path*"
}
]
}Your backend must have the following endpoints:
-
GET /api/auth/google- Initiates Google OAuth flow
- Redirects to Google for authentication
- After Google auth, redirects back to frontend callback URL
-
GET /api/auth/me(or similar)- Returns authenticated user data
- Must include credentials/cookies
- Response format should match your existing auth structure
- Start your backend server on
http://localhost:5000 - Start your frontend:
npm run dev(runs onhttp://localhost:3000) - Click "Login with Google" button
- Complete Google authentication
- You should be redirected back and logged in
- Make sure your backend Google OAuth callback URL is set to redirect to:
http://localhost:3000/auth/google/callback(for development) - For production, the callback should be:
https://your-frontend-domain.vercel.app/auth/google/callback - Ensure CORS is properly configured on your backend to accept requests from your frontend domain
- The backend should set HTTP-only cookies or return user data that can be stored in localStorage
You can create a .env.local file to customize the API URL:
VITE_API_URL=http://localhost:5000For production deployment, set this in your Vercel environment variables.
- Check that your backend is correctly setting cookies/session
- Verify CORS settings allow credentials
- Ensure the
/api/auth/meendpoint returns the correct user data format
- Verify your backend is running on the correct port
- Check the proxy configuration in
vite.config.js - Ensure the
/api/auth/googleendpoint is accessible
- Check browser console for detailed error messages
- Verify the
/api/auth/meendpoint is working - Ensure cookies are being sent with credentials: "include"
If you need to customize the authentication flow further, you can:
- Modify the
useGoogleAuthhook to handle different response formats - Update the
GoogleCallbackpage to show more detailed error messages - Add additional OAuth providers (Facebook, GitHub, etc.) using the same pattern