<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manwarerutuj on Medium]]></title>
        <description><![CDATA[Stories by Manwarerutuj on Medium]]></description>
        <link>https://medium.com/@manwarerutuj?source=rss-a998640274fd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*sCOTPeZaKm5XziMX</url>
            <title>Stories by Manwarerutuj on Medium</title>
            <link>https://medium.com/@manwarerutuj?source=rss-a998640274fd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:41:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@manwarerutuj/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[“react-google-login” deprecated???  Don’t worry “@react-oauth/google” will fix it .]]></title>
            <link>https://medium.com/@manwarerutuj/react-google-login-deprecated-dont-worry-react-oauth-google-will-fix-it-1b2141252952?source=rss-a998640274fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b2141252952</guid>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[login]]></category>
            <category><![CDATA[google-login]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Manwarerutuj]]></dc:creator>
            <pubDate>Sat, 16 Sep 2023 19:37:40 GMT</pubDate>
            <atom:updated>2023-09-16T19:37:40.091Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DhmWDKsTA3Ug1P19" /><figcaption>Photo by <a href="https://unsplash.com/@rahuulmiishra?utm_source=medium&amp;utm_medium=referral">Rahul Mishra</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hi everyone welcome to my blog, this was around July 2023 I was working on a project and was required to implement <strong>Google login functionality</strong> on the website.</p><p>This was a very easy task I thought like running the following command:</p><pre>npm i react-google-login</pre><p>All of a sudden, a warning message popped up stating that the package was no longer supported.</p><p>All my confidence was shattered but people, you have a chance to know me now, I don’t give up easily😎.</p><h3><strong><em>Let’s begin with our discussion on “@react-oauth/google” (The Solution Part)</em></strong></h3><h4>Installation:</h4><pre>&gt;npm i @react-oauth/google<br><br>//or<br><br>&gt;npm i @react-oauth/google@latest</pre><p>Enter the above-mentioned command in your terminal while located in your application directory.</p><p>Depending on your package manager, you may need to use yarn using the same command.</p><blockquote>I would like to skip this part of setting up credentials on Google Console to use login functionality, there are many resources available for setting up credentials you may click <a href="https://www.balbooa.com/gridbox-documentation/how-to-get-google-client-id-and-client-secret"><strong>here</strong></a> for one such resource.</blockquote><h3>Come Dive In VS Code With Me</h3><p>Wrap your App component inside <strong>“GoogleOAuthProvider”</strong> after importing it from <strong>“@react-oauth/google”.</strong></p><pre>//import statement<br>import { GoogleOAuthProvider } from &quot;@react-oauth/google&quot;;<br>//old way to render element<br>ReactDOM.render(<br>  &lt;Router&gt;<br>    &lt;GoogleOAuthProvider clientId={process.env.REACT_APP_GOOGLE_API_TOKEN}&gt;<br>      &lt;App /&gt;<br>    &lt;/GoogleOAuthProvider&gt;<br>  &lt;/Router&gt;,<br><br>  document.getElementById(&quot;root&quot;)<br>);</pre><p>In the above code,</p><p><strong><em>“clientId={process.env.REACT_APP_GOOGLE_API_TOKEN}” </em></strong>is where your API TOKEN will be resting. Basically, an API Token is sensitive data that you should not disclose publically so the actual API Token is safe inside the “<strong>.env”</strong> file. Click <a href="https://medium.com/how-to-react/using-env-file-in-react-js-b2714235e77e">here</a> to learn more about <strong>“.env”</strong>.</p><p>This is the old way to write <strong>ReactDOM.render()</strong>. You should use the new way discussed below:</p><pre>// Create a root.<br>const root = ReactDOMClient.createRoot(document.getElementById(&#39;root&#39;));<br><br>// Render an element to the root.<br>root.render(<br>  &lt;Router&gt;<br>    &lt;GoogleOAuthProvider clientId={process.env.REACT_APP_GOOGLE_API_TOKEN}&gt;<br>      &lt;App /&gt;<br>    &lt;/GoogleOAuthProvider&gt;<br>  &lt;/Router&gt;<br>);</pre><p>If you want to dig into the topic what is the difference between the old and the new way to render elements click <a href="https://github.com/reactwg/react-18/discussions/5"><strong>here</strong></a>.</p><blockquote>See one more piece of stuff added to the treasure, let’s keep moving who knows we could grab many on the way.</blockquote><p>Import “GoogleLogin” from “@react-oauth/google”.</p><h4>Use “GoogleLogin” as an element in your code it takes two parameters:</h4><p>1. <strong>onSuccess:</strong> if your login action is successfully executed then this will return you an object containing “JWT Token”. <br>2. <strong>onError:</strong> we can console log the error if occurs.</p><pre>import { GoogleLogin } from &#39;@react-oauth/google&#39;;<br><br>&lt;GoogleLogin<br>  onSuccess={credentialResponse =&gt; {<br>    console.log(credentialResponse);<br>  }}<br>  onError={() =&gt; {<br>    console.log(&#39;Login Failed&#39;);<br>  }}<br>/&gt;;</pre><p>In the above code “credentialResponse” is the object containing “JWT Token”.<br>which looks somewhat like:</p><pre>{<br>  &quot;credential&quot;: &quot;This is JWT Token&quot;,<br>  &quot;clientId&quot;: &quot;your client id&quot;,<br>  &quot;select_by&quot;: &quot;btn_confirm&quot;<br>}</pre><p>In this blog post, we will learn how to implement <strong>Google login using a custom login button</strong>. If you use a custom login button, you will receive a slightly different response than the default response.<br>To know more about <strong>jwt_decode</strong>, a package to decode information from <strong>JWT Token</strong> watch this <a href="https://www.youtube.com/watch?v=roxC8SMs7HU">video</a>.</p><h3>How to use a custom login Button</h3><ol><li>Import “useGoogleLogin” from “@react-oauth/google” instead of “GoogleLogin”.</li><li>Create a function that will be an <strong>onClick</strong> event for the custom login button (<strong>In this case const Login() is that function</strong>).</li></ol><pre>import { useGoogleLogin } from &quot;@react-oauth/google&quot;;<br><br>const Login = () =&gt; {<br> <br>  const login = useGoogleLogin({<br>    onSuccess: (codeResponse) =&gt; {<br>      setUser(codeResponse)<br>      console.log(codeResponse)<br>    },<br>    onError: (error) =&gt; console.log(&quot;Login Failed:&quot;, error)<br>  });<br>};<br><br>export default Login;<br></pre><p>Use “<strong>useGoogleLogin()</strong>” as displayed in the above code block to get the response.</p><p>you can see the response block below:</p><pre>{<br>  &quot;access_token&quot;: &quot;access token&quot;,<br>  &quot;token_type&quot;: &quot;Bearer&quot;,<br>  &quot;expires_in&quot;: 3599,<br>  &quot;scope&quot;: &quot;email profile https://www.googleapis.com/auth/userinfo.email openid https://www.googleapis.com/auth/userinfo.profile&quot;,<br>  &quot;authuser&quot;: &quot;0&quot;,<br>  &quot;prompt&quot;: &quot;none&quot;<br>}</pre><p>Now, you possess an &quot;<strong>access_token</strong>&quot; which can be utilized to obtain details regarding the user who attempted to log in.</p><h4>How to retrieve information using “access_token”</h4><p>The continued part of the same login function discussed above is written below.</p><p>create a <strong>useEffect</strong> hook and request `<a href="https://www.googleapis.com/oauth2/v1/userinfo?access_token">https://www.googleapis.com/oauth2/v1/userinfo?access_token</a>= “<strong>access_token</strong>” ` <br>⬆️This endpoint will provide you with the information of the user that you could use further in your application.</p><pre><br>import { FcGoogle } from &quot;react-icons/fc&quot;;<br><br>import { useGoogleLogin } from &quot;@react-oauth/google&quot;;<br><br>const Login = () =&gt; {<br>  <br>  const [user, setUser] = useState([]);<br>  <br>  const login = useGoogleLogin({<br>    onSuccess: (codeResponse) =&gt; {setUser(codeResponse)<br>      console.log(codeResponse)<br>    },<br>    onError: (error) =&gt; console.log(&quot;Login Failed:&quot;, error)<br>  });<br><br>  useEffect(() =&gt; {<br>    if (user) {<br>      axios<br>        .get(<br>          `https://www.googleapis.com/oauth2/v1/userinfo?access_token=${user.access_token}`,<br>          {<br>            headers: {<br>              Authorization: `Bearer ${user.access_token}`,<br>              Accept: &quot;application/json&quot;,<br>            },<br>          }<br>        )<br>        .then((res) =&gt; {<br>          setProfile(res.data);<br>        })<br>        .catch((err) =&gt; console.log(err));<br>    }<br>  }, [user]);<br><br>  return (<br>          &lt;div className=&quot;shadow-2xl&quot;&gt;<br>            &lt;button<br>              type=&quot;button&quot;<br>              className=&quot;bg-mainColor flex justify-center items-center p-3 rounded-lg cursor-pointer outline-none&quot;<br>              onClick={() =&gt; login()}<br>            &gt;<br>              &lt;FcGoogle className=&quot;mr-4&quot; /&gt;<br>              Sign in with Google<br>            &lt;/button&gt;<br>          &lt;/div&gt;<br>  );<br>};<br><br>export default Login;</pre><p>Response from the endpoint will look like:</p><pre>{<br>  &quot;id&quot;: &quot;id&quot;,<br>  &quot;email&quot;: &quot;email&quot;,<br>  &quot;verified_email&quot;: &quot;verification status&quot;,<br>  &quot;name&quot;: &quot;Name&quot;,<br>  &quot;given_name&quot;: &quot;given name&quot;,<br>  &quot;family_name&quot;: &quot;family name&quot;,<br>  &quot;picture&quot;: &quot;profile picture&quot;,<br>  &quot;locale&quot;: &quot;locality&quot;<br>}</pre><p>You can access the email by using “<strong>data.email</strong>” since the response is stored in const data.</p><h3>Conclusion</h3><p>We have learned how to implement Google login using “@react-oauth/google” and that too using a custom login button.</p><p>I hope you find this blog easy to read and understand and you will be able to easily implement Google login in your application ✌️.</p><p><strong><em>Connect with me on medium➡️ </em></strong><a href="https://medium.com/@manwarerutuj"><strong><em>https://medium.com/@manwarerutuj</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b2141252952" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>