question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting Error: Google OAuth components must be used within GoogleOAuthProvider

See original GitHub issue

Hello. 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:

image

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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
MomenSherifcommented, Jun 11, 2022

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

1reaction
TangoPJcommented, Jun 11, 2022

@MomenSherif It worked for me, thank you so much! =) I can close the question

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found