Getting Error: Google OAuth components must be used within GoogleOAuthProvider
See original GitHub issueHello. I’m trying to get auth code from google and send it to server side to get tokens. But when I wrapped my custom button to the GoogleOAuthProvider component I got the error like on the screenshot:
I’m doing everything like in documentation, but don’t understand where I could make a mistake…
this is my component’s code:
/* eslint-disable react/button-has-type */
import { GoogleOAuthProvider, useGoogleLogin } from '@react-oauth/google';
import { Button, message } from 'antd';
import { setApiHeader } from 'api/axios';
import { userAuth } from 'api/requests/userAuth';
import React from 'react';
import { useHistory } from 'react-router-dom';
import { localStorageKeys } from 'resources/localStorageKeys';
import { saveState } from 'utils/localStorage';
const GoogleAuth = () => {
const clientId = process.env.REACT_APP_GOOGLE_CLIENT_ID || '';
const history = useHistory();
const login = useGoogleLogin({
onSuccess: async (res: any) => {
try {
const { data } = await userAuth(res);
saveState(data, localStorageKeys.radiatorUser);
setApiHeader('Authorization', `Bearer ${data?.token}`);
history.push('/');
if (data) void message.success('Success');
} catch (error) {
void message.error(error.message);
}
},
flow: 'auth-code',
});
return (
<GoogleOAuthProvider clientId={clientId}>
<Button onClick={() => login()}>Sign in with Google 🚀 </Button>
</GoogleOAuthProvider>
);
};
export default GoogleAuth;
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
reactjs - Google OAuth components must be used within ...
this is layout.js and in _app.js I have all the components wrapped in GoogleOAuthProvider import { GoogleLogin } from '@react-oauth/google'; ...
Read more >Authorization Errors | Device Access - Google Developers
During the authorization process, Google OAuth may return an error. Use this guide to troubleshoot the most common errors during this ...
Read more >Google OAuth2 using the new Google Identity Services SDK ...
Your web application, complete either the OAuth 2.0 implicit flow, or to initiate the authorization code flow which then finishes on your ...
Read more >@react-oauth/google - npm
Start using @react-oauth/google in your project by running `npm i ... Users get a secure, token-based, passwordless account on your site, ...
Read more >Google OAuth 2.0 Login for React in 5 minutes - YouTube
Enjoying my videos? Sign up for FREE content here: https://www.thecoopercodes.com/UPDATE: This implementation is no longer supported by ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m using mobile right now, so I can’t write some code
But you can do it inside your react entry point like this
https://github.com/MomenSherif/react-oauth/blob/master/apps/playground/src/index.tsx
Or wrap your GoogleAuth component with the provider directly both will work
@MomenSherif It worked for me, thank you so much! =) I can close the question