Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transferring react client-side google sign-in to ASP.NET server-side

I am using react-google-login library to let users sign in my application on the client-side through Google, which works fine and returns token and profile information.

  <GoogleLogin
      clientId="XXXX.apps.googleusercontent.com"
      buttonText="Login"
      onSuccess={responseGoogle}
      onFailure={responseGoogle}
      cookiePolicy={'single_host_origin'}
  />

 const responseGoogle = (response) => {
     console.log(response);
 }

Below is the configuration on the server to enable Google Authentication:

services.AddAuthentication()
    .AddGoogle(options =>
    {
        IConfigurationSection googleAuthNSection = 
            Configuration.GetSection("Authentication:Google");

        options.ClientId = googleAuthNSection["ClientId"];
        options.ClientSecret = googleAuthNSection["ClientSecret"];
    });

But, I do not how to transfer this successful login to the asp.net server-side. How can I use the token sent by google to sign in the user on the server side? I would like to do this for two cases: (a) the email in the system does not match the gmail, (b) the email in the system matches the gmail. I appreciate any help.

like image 827
renakre Avatar asked Oct 18 '25 00:10

renakre


1 Answers

Small misunderstanding of the auth process. You can do it in a two ways:

  • client side OAuth (react-google-login)
  • server side OAuth (Microsoft.AspNetCore.Authentication.Google)

But you're trying to mix them in one, it's possible but not very useful, because it's harder to handle token expiration and also scopes for client side are limited in security reasons.

You need to go with next instructions to implement server side OAuth, and you don't need to use react-google-login:

  1. Facebook, Google, and external provider authentication in ASP.NET Core
  2. Google external login setup in ASP.NET Core
like image 168
Alexandr Tovmach Avatar answered Oct 20 '25 16:10

Alexandr Tovmach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!