Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with useGoogleLogin: Google OAuth components must be used within GoogleOAuthProvider #25

Closed
Infinitay opened this issue Jun 2, 2022 · 1 comment

Comments

@Infinitay
Copy link

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

const CLIENT_ID = process.env.REACT_APP_GOOGLE_OAUTH_CLIENT_ID || "";

interface props {
	setAccessToken: React.Dispatch<React.SetStateAction<string>>;
}

const Authorize: React.FC<props> = ({ setAccessToken }) => {
	const login = useGoogleLogin({
		onSuccess: (tokenResponse) => console.log(tokenResponse),
	});

	return (
		<GoogleOAuthProvider clientId={CLIENT_ID}>
			<div id="authorizeButton">
				<button onClick={() => login()} />
			</div>
		</GoogleOAuthProvider>
	);
};

export default Authorize;

image

Uncaught Error: Google OAuth components must be used within GoogleOAuthProvider

Getting the following error above with the respective code. I am using useGoogleLogin so I can manually define the scope's I require of the user. I didn't have any issuing using GoogleLogin within GoogleOAuthProvider. I'm encountering this error only when using useGoogleLogin.

Is this a bug or am I not properly implementing the auth?

@Infinitay
Copy link
Author

Found the problem, it was as the stacktrace was saying. I can't define login as I did in the example above having it outside the scope of GoogleOAuthProvider.

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

I resolved this by explicitly following the example and creating a custom component for useGoogleLogin.

import React from "react";
import { GoogleOAuthProvider, GoogleLogin, useGoogleLogin, TokenResponse, CodeResponse } from "@react-oauth/google";
import GoogleLoginButton from "./GoogleLoginButton";

const CLIENT_ID = process.env.REACT_APP_GOOGLE_OAUTH_CLIENT_ID || "";

interface props {
	setAccessToken: React.Dispatch<React.SetStateAction<string>>;
}

const Authorize: React.FC<props> = ({ setAccessToken }) => {
	return (
		<div id="authorizeButton">
			<GoogleOAuthProvider clientId={CLIENT_ID}>
				<GoogleLoginButton />
			</GoogleOAuthProvider>
		</div>
	);
};

export default Authorize;
import React from "react";
import { useGoogleLogin } from "@react-oauth/google";

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

	return (
		<div>
			<button onClick={() => login()}>Login with google✌</button>
		</div>
	);
};

export default GoogleLoginButton;

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

No branches or pull requests

1 participant