Skip to content

Feat: Google Auth - #43

Merged
Siddharth-732 merged 4 commits into
mainfrom
feat/google-auth
Jul 16, 2026
Merged

Feat: Google Auth#43
Siddharth-732 merged 4 commits into
mainfrom
feat/google-auth

Conversation

@Siddharth-732

Copy link
Copy Markdown
Owner

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.

  • Added a new googleAuth action to the global Zustand useAuthStore to securely pass the Google token to the backend.
  • Added a new POST /api/v1/users/google-auth endpoint.
  • Auto-Registration: If a user signs in with Google for the first time, the backend automatically generates a unique username (e.g., siddharth_7384) and saves them to MongoDB.

Copilot AI review requested due to automatic review settings July 16, 2026 17:06
@vercel

vercel Bot commented Jul 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
bind-app Ready Ready Preview, Comment Jul 16, 2026 5:06pm

@Siddharth-732 Siddharth-732 changed the title Feat/google auth Feat: Google Auth Jul 16, 2026
@Siddharth-732
Siddharth-732 merged commit 706650e into main Jul 16, 2026
4 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 googleAuth action in the Zustand auth store.
  • Backend: adds a new POST /api/v1/users/google-auth route + controller that fetches Google profile data and issues app auth cookies.
  • Tooling: adds @react-oauth/google (frontend) and google-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);
Comment thread backend/package.json
"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>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ISSUE 3 ! Google auth

2 participants