How to request permission access from user without asking the user to insert information Google Sign-Up

Viewed 137

I have implemented google sign-in for my web app, in which I've used this library: https://metacpan.org/pod/Google::RestApi::Auth::OAuth2Client to get the code value, the access token and after that the user's information (email, name, google-id).

My problem in this implementation is that the user is prompt for consent where he needs to insert his information (email and password) and I would like to redirect the user for consent where he is able to select the accounts, and not insert his information. I don't know if my implementation, where I make the GET request, is incorrect or if it is because of the library that I use.

In front-end, I have a form which calls a route that redirects to my back-end function 'on_google_login':

sub on_google_login {
  my $self = shift;
  my $redirect_name = $self->get_redirect_name();
  $self->session(redirect => $redirect_name);
  my $google = Google::RestApi::Auth::OAuth2Client->new(
    client_id     => $ENV{GOOGLE_CLIENT_ID},
    client_secret => $ENV{GOOGLE_SECRET},
    redirect_uri  => $ENV{GOOGLE_BASE_URL} . '/google_callback'
  );

  my $url = $google->authorize_url(
    display => 'page'
  );

  $self->redirect_to($url);
}

And this is my callback function, where I extract the 'code' and I request the user's information using the access token.

sub on_google_callback {
  my $self = shift;

  my $code = $self->req->param('code');
  my $google = Google::RestApi::Auth::OAuth2Client->new(
    client_id     => $ENV{GOOGLE_CLIENT_ID},
    client_secret => $ENV{GOOGLE_SECRET},
    redirect_uri  => $ENV{GOOGLE_BASE_URL} . '/google_callback'
  );

  if (not (defined $code)) {
    return $self->render(text => 'Did not connect to Google');
  }

  my $redirect_name = $self->session('redirect') // 'home';
  delete $self->session->{'redirect'};  
  my $access_token = $google->access_token($code)->access_token;
  my $url = $ENV{GOOGLE_ENDPOINT} . $access_token;
  my $request = HTTP::Request->new(GET => $url);
  my $ua = LWP::UserAgent->new();
  my $info = decode_json($ua->request($request)->content);
  my ($google_id, $name, $mail) = ($info->{sub}, $info->{name}, $info->{email});

  if (!defined $google_id) {
    return $self->render(
    template => 'validation/custom_error',
    title => 'Error logging in with Google',
    message => 'Sorry, something went wrong when attempting to log you in ' .
      'with Google. Please try again and contact us in the chat if this ' .
      'persists.',
    status => 400);
  }
  my $found_user = $self->db->resultset('User')->by_mail($mail);

  if ($found_user) {
    return unless validate_user_can_login($self, $found_user);
    return unless set_user_data_on_login($self, $found_user);
    $self->redirect_to($redirect_name);
  } else {
    $self->session(name => $name);
    $self->session(mail => $mail);
    $self->redirect_to('/register');
  }

  return;
}
0 Answers
Related