Next.js Discord

Discord Forum

Auth with Google Provider error

Answered
Atlantic mackerel posted this in #help-forum
Open in Discord
Atlantic mackerelOP
Hey guys, Im trying to add google provider to my application but it doesn't work
I can select my google accounts but after selected, the error has occurred.
Why This error has occurred?


I tried this

  <GoogleOAuthProvider clientId="secret">
            <div className="w-[120px]">
                <GoogleLogin type="icon" onSuccess={responseMessage} onError={errorMessage}/>
            </div>
</GoogleOAuthProvider>


Also I did this (seocnd photo)
Answered by Atlantic mackerel
OH I fixed it myself! thank you sorry for late
View full answer

87 Replies

Remvove /home from the approved redirect URI so it's just http://localhost:8888
Also click on check and see what comes up
@Jesse Also click on `check` and see what comes up
Atlantic mackerelOP
like this?
Yep but have two on Approved JavaScript Source and Approved Redirect URI.
The two you should have on each are http://localhost and http://localhost:8888
Atlantic mackerelOP
ahhh then I have to add http://localhost
I added this but the after click page just show the same
like this.
Atlantic mackerelOP
ahhh both are same
@Jesse like this.
Atlantic mackerelOP
Oh login work perfectly!! but
thenn did you know how to get that user information programmatically?
yes, but it depends what data you're getting
do you have a custom sign up button
actually nvm
I can see from your code
@Jesse do you have a custom sign up button
Atlantic mackerelOP
Yep I have this
import {GoogleLogin, GoogleOAuthProvider} from "@react-oauth/google";

    const responseMessage = (response) => {
        console.log(response);
    };
    const errorMessage = () => {

    };
                <GoogleLogin type="icon" onSuccess={responseMessage} onError={errorMessage}/>
I remember having to do this let me have a look at my code.
Atlantic mackerelOP
ahhh you did the same thing before me haha
what are you getting in response
Atlantic mackerelOP
This is response object
This should work.
const res = await fetch("https://www.googleapis.com/oauth2/v2/userinfo", {
  headers: { Authorization: `Bearer ${response.credential}` },
});
const data = await res.json();
const responseMessage = async (response) => {
  const res = await fetch("https://www.googleapis.com/oauth2/v2/userinfo", {
    headers: { Authorization: `Bearer ${response.credential}` },
  });
  const data = await res.json();
};
Atlantic mackerelOP
it returned this error
GET https://www.googleapis.com/oauth2/v2/userinfo 401 (Unauthorized)
Okay wait a sec, lemme find it
Atlantic mackerelOP
as I remember, I didn't put any client_scret key
ANd I guess it's gonna make problem
import{ OAuth2Client } from "google-auth-library";
const client = new OAuth2Client(
  "Your client ID",
);
const responseMessage = async (response) => {
  const ticket = (
      await client.verifyIdToken({
        idToken: response.credential,
        audience:
          "Your client ID",
      })
   ).getPayload();
  console.log(ticket)
};
This should work
Atlantic mackerelOP
ahh I'll try it
I tried this:

import {User} from "../types/user";
import {GoogleLogin, GoogleOAuthProvider} from "@react-oauth/google";
import{ OAuth2Client } from "google-auth-library";

const client = new OAuth2Client(
    "secret",
);
const Home = () => {
    const responseMessage = async (response) => {
        const ticket = (
            await client.verifyIdToken({
                idToken: response.credential,
                audience:
                    "Your client ID",
            })
        ).getPayload();
        console.log(ticket)
    };
    const errorMessage = () => {

    };

    return (
        <GoogleOAuthProvider clientId="919611793952-7muej33mh7touelci048cbjmingtko01.apps.googleusercontent.com" >
            <div className="w-[120px]">
                <GoogleLogin type="icon" onSuccess={responseMessage} onError={errorMessage}/>
            </div>
        </GoogleOAuthProvider>
    );
};

export default Home;


but the error said:

../node_modules/gcp-metadata/build/src/gcp-residency.js:19:0
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../node_modules/gcp-metadata/build/src/index.js
../node_modules/google-auth-library/build/src/index.js
./pages/home.tsx
I didn't had this google-auth-library libs so I download it via npm i google-auth-library
Run this in your server instead
Are you familiar with Next backend
@Jesse Are you familiar with Next backend
Atlantic mackerelOP
oh actually my backend is electron I use with nextron
oh
okay nvm
let me find another solution
Atlantic mackerelOP
yap
Use that component
convert it to jsx if needed
then replace your div with that
Atlantic mackerelOP
ahh got it
and it will work
Atlantic mackerelOP
I tried to add responseMessage but the error has occurred as you can see
oh I see
you're gonna have to edit the GoogleButton slightly
Atlantic mackerelOP
ahhh got it like add parameter?
wait a sec let me check you might have to edit responseMessage
Atlantic mackerelOP
I just did this


    const responseMessage = async (response) => {
        const res = await fetch("https://www.googleapis.com/oauth2/v2/userinfo", {
            headers: { Authorization: `Bearer ${response.credential}` },
        });
        const data = await res.json();
    };
    const errorMessage = () => {

    };

    return (
        <GoogleOAuthProvider clientId="" >
            <div className="w-[120px]">
                <GoogleButton googleFunction={responseMessage} text={"Sign in with Google"}></GoogleButton>
            </div>
        </GoogleOAuthProvider>
    );
that won't work this is the new code you'll need
Atlantic mackerelOP
ahhhh got it
Create this function where your response message is.
const login = useGoogleLogin({
    onSuccess: (codeResponse) => responseMessage(codeResponse.access_token),
  });
then pass it as googleFunction
Also import this.
import { useGoogleLogin } from "@react-oauth/google";
Atlantic mackerelOP
ahhh like this?
import {GoogleOAuthProvider, useGoogleLogin} from "@react-oauth/google";
import GoogleButton from "../components/google-button";



const Home = () => {
    const responseMessage = async (response) => {
        const login = useGoogleLogin({
            onSuccess: (codeResponse) => responseMessage(codeResponse.access_token),
        });
    };
    const errorMessage = () => {

    };

    return (
        <GoogleOAuthProvider clientId="" >
            <div className="w-[120px]">
                <GoogleButton googleFunction={responseMessage} text={"Sign in with Google"}></GoogleButton>
            </div>
        </GoogleOAuthProvider>
    );
};

export default Home;
I just got this errorrrr..:lolsob:
put the login outside the responseMessage
Atlantic mackerelOP
like this?

import {GoogleOAuthProvider, useGoogleLogin} from "@react-oauth/google";
import GoogleButton from "../components/google-button";

const Home = () => {

    const login = useGoogleLogin({
        onSuccess: (codeResponse) => responseMessage(codeResponse.access_token),
    });

    const responseMessage = async (response) => {


    };
    const errorMessage = () => {

    };

    return (
        <GoogleOAuthProvider clientId="">
            <div className="w-[120px]">
                <GoogleButton googleFunction={() => {


                }} text={"Sign in with Google"}></GoogleButton>
            </div>
        </GoogleOAuthProvider>
    );
};

export default Home;
Also change.
googleFunction={responseMessage}

To.
googleFunction={login}
Atlantic mackerelOP
And then set your response message function to.
const responseMessage = async (response) => {
  const res = await fetch("https://www.googleapis.com/oauth2/v2/userinfo", {
    headers: { Authorization: `Bearer ${response.credential}` },
  });
  const data = await res.json();
};
Atlantic mackerelOP
I guess the login and main page should be separated file
Also make the GoogleOAuthProvider wrap where the component is rendered instead of wrapping it in the component
Atlantic mackerelOP
ahhh so it couldn't be in same file
@Jesse Also make the GoogleOAuthProvider wrap where the component is rendered instead of wrapping it in the component
Atlantic mackerelOP
I just edit the google-button


const GoogleButton = ({
                          googleFunction,
                          text = "Sign in with Google",
                      }: {
    googleFunction: () => any;
    text?: string;
}) => {

    const login = useGoogleLogin({
        onSuccess: (codeResponse) => responseMessage(codeResponse.access_token),
    });

    const responseMessage = async (response) => {
        const res = await fetch("https://www.googleapis.com/oauth2/v2/userinfo", {
            headers: { Authorization: `Bearer ${response.credential}` },
        });
        const data = await res.json();
    };



    return (
    <button
                type="button"
                onClick={() => login}
                className="loginWithGoogleBtn"
            >
                {text}
            </button>

  )
}


but it didn't work
What error are you getting
Have you put the provider outside the component
@Jesse Have you put the provider outside the component
Atlantic mackerelOP
Oh I just turn off my pc to get sleep can I continue tomorrow?
@Atlantic mackerel Oh I just turn off my pc to get sleep can I continue tomorrow?
Yeah sure, tell me when you’re ready
@Jesse Yeah sure, tell me when you’re ready
Atlantic mackerelOP
Thanks!!
Is it fixed yet @Atlantic mackerel
@Jesse Is it fixed yet <@273686048121290763>
Atlantic mackerelOP
OH I fixed it myself! thank you sorry for late
Answer
Make sure to mark it as complete
Atlantic mackerelOP
where's the mark?
Original message was deleted
Do this
Atlantic mackerelOP
ahhhh
Perfect
nice
Atlantic mackerelOP
Thank uuu
no worries