react-oauth icon indicating copy to clipboard operation
react-oauth copied to clipboard

GoogleLogin button is not responsive

Open nachodeh opened this issue 1 year ago • 10 comments

It's currently not possible to make the button take up 100% of the available width. It does initially when rendering but immediately updates to some smaller size. This makes it very hard to make the button fit into any website's style.

nachodeh avatar May 09 '24 17:05 nachodeh

Use Custom Google Button

import { useGoogleLogin } from '@react-oauth/google';

const login = useGoogleLogin({
  onSuccess: tokenResponse => console.log(tokenResponse),
});

<MyCustomButton onClick={() => login()}>Sign in with Google 🚀</MyCustomButton>;

vasanthmn1 avatar May 20 '24 04:05 vasanthmn1

Use Custom Google Button

import { useGoogleLogin } from '@react-oauth/google';

const login = useGoogleLogin({
  onSuccess: tokenResponse => console.log(tokenResponse),
});

<MyCustomButton onClick={() => login()}>Sign in with Google 🚀</MyCustomButton>;

Thanks, but this doesn't really work because useGoogleLogin provides a tokenResponse and I need a credentialResponse for my API

nachodeh avatar May 20 '24 17:05 nachodeh

@nachodeh Have you found a solution so far?

Nicolasbort avatar Oct 05 '24 16:10 Nicolasbort

@Nicolasbort yeah, I stopped using this button, created my own, and implemented token retrieval in my own API. In the end it's much better this way and gives me more flexibility.

nachodeh avatar Oct 05 '24 16:10 nachodeh

@nachodeh that's a good idea, thanks for the reply.

Nicolasbort avatar Oct 06 '24 00:10 Nicolasbort

Why does useGoogleLogin return an access token instead of a user credential like the GoogleLogin component does?

Thanks btw! I did get it working. It just feels odd because now I need to change the backend implementation simply because the button won't fill the width of the container.

nickcan avatar Nov 30 '24 19:11 nickcan

having the same issue.

ciccilleju avatar Jan 18 '25 18:01 ciccilleju

Why does useGoogleLogin return an access token instead of a user credential like the GoogleLogin component does?

Thanks btw! I did get it working. It just feels odd because now I need to change the backend implementation simply because the button won't fill the width of the container.

I have Same question

divya-nsh avatar Jan 22 '25 14:01 divya-nsh

It helped me

   const login = useGoogleLogin({
           onSuccess: async tokenResponse => {
               const token = tokenResponse.access_token;
             // fetching userinfo can be done on the client or the server
             console.log(token);
             const userInfo = await axios
               .get('https://www.googleapis.com/oauth2/v3/userinfo', 
                 { headers: { Authorization: `Bearer ${tokenResponse.access_token}` },
               })
             const result = userInfo.data;
             console.log(result);
            // contains name, email & googleId(sub)
           },
         });

<MyCustomButton onClick={login}>Sign in with Google 🚀</MyCustomButton>;

source: https://stackoverflow.com/questions/75590977/get-token-and-google-id-in-react-oauth-google

NuraiymMamatova avatar Feb 17 '25 17:02 NuraiymMamatova

Front End


import { useGoogleLogin } from "@react-oauth/google";

const googleLogin = useGoogleLogin({
  onSuccess: async (tokenResponse) => {

    // Send access token to your backend for verification
    const response = await apiClient.post("/auth/google", {
      accessToken: tokenResponse.access_token,
    });

    // Handle your authentication flow
    if (response.success) {
      // Store tokens, update state, etc.
    }
  },
  onError: () => {
    console.error("Google login failed");
  },
});

// Custom button with full styling control
<button
  onClick={() => googleLogin()}
  className="w-full bg-white text-gray-700 py-4 px-6 rounded-2xl font-medium border hover:bg-gray-50 flex items-center justify-center gap-3"
>
  <GoogleLogoSVG />
  <span>Continue with Google</span>
</button>

Back End

app.post("/auth/google", async (request, reply) => {
  const { accessToken: googleAccessToken } = request.body;

  // Verify token by fetching user info from Google
  const response = await fetch(
    "https://www.googleapis.com/oauth2/v3/userinfo",
    {
      headers: { Authorization: `Bearer ${googleAccessToken}` },
    }
  );

  if (!response.ok) {
    return reply.code(400).send({ 
      success: false, 
      message: "Invalid Google access token" 
    });
  }

  const payload = await response.json();
  // payload contains: email, name, picture, sub (Google ID)

  // Create/login user and return your JWT tokens
  const user = await findOrCreateUser(payload);
  const accessToken = generateJWT(user);
  
  return reply.send({ success: true, accessToken, user });
});

yashpatil02121 avatar Nov 06 '25 09:11 yashpatil02121