如何在React中使用@react-oauth/google实现自动登录。

huangapple go评论69阅读模式
英文:

How to auto login in react using @react-oauth/google

问题

I'm trying to implement a google SSO login in my react.js application. Signing in part is working as expected and I'm getting a JWT in the response callback, but as soon as I refresh the page I have to login again to get the JWT. I was previously using react-google-login npm package that used to sign in the user automatically and return the user object on page refresh, but that isn't happening with the new @react-oauth/google package.

This is how the button element looks like:

<GoogleLogin
theme="filled_blue"
text="continue_with"
auto_select={true}
useOneTap={true}
state_cookie_domain='single_host_origin'
onSuccess={(credentialResponse) => {
console.log(credentialResponse);
}}
onError={() => {
console.log("Login Failed");
}}
/>

Please help me out, how can I persist a user login automatically?

英文:

I'm trying to implement a google SSO login in my react.js application. Signing in part is working as expected and I'm getting a JWT in the response callback, but as soon as I refresh the page I have to login again to get the JWT. I was previously using react-google-login npm package that used to sign in the user automatically and return the user object on page refresh, but that isn't happening with the new @react-oauth/google package.

This is how the button element looks like

	&lt;GoogleLogin
        theme=&quot;filled_blue&quot;
        text=&quot;continue_with&quot;
        auto_select={true}
        useOneTap={true}
        state_cookie_domain=&#39;single_host_origin&#39;
        onSuccess={(credentialResponse) =&gt; {
          console.log(credentialResponse);
        }}
        onError={() =&gt; {
          console.log(&quot;Login Failed&quot;);
        }}
      /&gt;

Please help me out, how can I persist a user login automatically?

答案1

得分: 1

You have to store the login token in a cookie or on the local storage.
Cookies

英文:

You have to store the login token in a cookie or on the local storage.
Cookies

huangapple
  • 本文由 发表于 2023年5月10日 19:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定