Feat: Google Auth - #43
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Adds Google OAuth-based authentication across the Next.js frontend and Express backend, enabling users to sign in (and auto-register on first sign-in) using their Google account.
Changes:
- Frontend: adds Google OAuth provider wiring, invokes Google login from the Login/Register pages, and adds a
googleAuthaction in the Zustand auth store. - Backend: adds a new
POST /api/v1/users/google-authroute + controller that fetches Google profile data and issues app auth cookies. - Tooling: adds
@react-oauth/google(frontend) andgoogle-auth-library(backend) dependencies.
Reviewed changes
Copilot reviewed 9 out of 11 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| frontend/store/useAuthStore.ts | Adds googleAuth() action to send Google token to backend and update auth state. |
| frontend/package.json | Adds @react-oauth/google dependency. |
| frontend/package-lock.json | Locks @react-oauth/google dependency resolution. |
| frontend/components/GoogleAuthProvider.tsx | Introduces a wrapper component for GoogleOAuthProvider. |
| frontend/app/register/page.tsx | Adds Google sign-up flow via useGoogleLogin() + googleAuth(). |
| frontend/app/login/page.tsx | Adds Google sign-in flow via useGoogleLogin() + googleAuth(). |
| frontend/app/layout.tsx | Wraps app in GoogleAuthProvider so OAuth hooks can work. |
| backend/src/routes/user.routes.js | Adds /google-auth route mapping. |
| backend/src/controllers/user.controllers.js | Implements Google auth: userinfo fetch, auto-registration, cookie issuance. |
| backend/package.json | Adds google-auth-library dependency. |
| backend/package-lock.json | Locks google-auth-library dependency resolution. |
Files not reviewed (2)
- backend/package-lock.json: Generated file
- frontend/package-lock.json: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+850
to
+869
| const accessToken = user.generateAccessToken(); | ||
| const refreshToken = user.generateRefreshToken(); | ||
|
|
||
| user.refreshToken = refreshToken; | ||
| await user.save({ validateBeforeSave: false }); | ||
|
|
||
| const options = { | ||
| httpOnly: true, | ||
| secure: true, | ||
| sameSite: "none", | ||
| }; | ||
|
|
||
| return res | ||
| .status(200) | ||
| .cookie("accessToken", accessToken, options) | ||
| .cookie("refreshToken", refreshToken, options) | ||
| .json({ | ||
| message: "Google login successful", | ||
| user, | ||
| }); |
Comment on lines
+814
to
+831
| const payload = await response.json(); | ||
| const { sub: googleId, email, name: displayName, picture: avatar } = payload; | ||
|
|
||
| let user = await User.findOne({ email }); | ||
|
|
||
| if (user) { | ||
| // User exists, just log them in (add googleId if they didn't have it) | ||
| if (!user.googleId) { | ||
| user.googleId = googleId; | ||
| await user.save({ validateBeforeSave: false }); | ||
| } | ||
| } else { | ||
| // User doesn't exist, create them | ||
| // Generate a unique username based on their display name | ||
| const baseUsername = displayName.toLowerCase().replace(/[^a-z0-9]/g, ""); | ||
| const randomSuffix = Math.floor(1000 + Math.random() * 9000); | ||
| let username = `${baseUsername}_${randomSuffix}`; | ||
|
|
Comment on lines
+9
to
+11
| import { OAuth2Client } from "google-auth-library"; | ||
|
|
||
| const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID); |
| "cors": "^2.8.6", | ||
| "dotenv": "^17.4.2", | ||
| "express": "^5.2.1", | ||
| "google-auth-library": "^10.9.0", |
Comment on lines
+5
to
+11
| export default function GoogleAuthProvider({ children }: { children: React.ReactNode }) { | ||
| return ( | ||
| <GoogleOAuthProvider clientId={process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || ""}> | ||
| {children} | ||
| </GoogleOAuthProvider> | ||
| ); | ||
| } |
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.
closes #3
Summary
This PR introduces a complete, secure Google OAuth 2.0 authentication flow, allowing users to seamlessly log in or register using their Google accounts.