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

Viewed 1244

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.

1 Answers

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

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
Related