Login enters directly via google

Asked

Viewed 81 times

1

Good morning! I used the google login API on a website page. It takes the information and works correctly, however, even without clicking the button, it already runs the js of google directly, without making it possible somehow to do the "singOut" of the system because whenever I go to the login page, it already automatically picks up the information and goes to the redirect page. Follow the code from the login page

<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark" style="width: 350px"></div>

                <script>
                    function onSignIn(googleUser) {
                        var profile = googleUser.getBasicProfile();
                        var userID = profile.getId();
                        var userName = profile.getName();
                        var userPicture = profile.getImageUrl();
                        var userEmail = profile.getEmail();
                        var userToken = googleUser.getAuthResponse().id_token;

                        //document.getElementById('msg').innerHTML = userEmail;
                        if(userEmail !== ''){
                            var dados = {
                                userID:userID,
                                userName:userName,
                                userPicture:userPicture,
                                userEmail:userEmail
                            };
                            $.post('validarLoginGoogle.php', dados, function(retorna){
                                    if(retorna=="1"){
                                            window.location.href = "http://calangoeventos.eu5.net/telas/telaInicial.php";
                                    }
                                    }else{
                                            window.location.href = "http://calangoeventos.eu5.net/telas/complementoCadastro.php";
                                    }
                            });
                        }
                    }
</script>

The purpose of the above code is to redirect the user to the home page if he has already accessed before and if not, redirect to the registration add-on screen And the code of my validarLoginGoogle.php

<?php
session_start();

include_once("../funcoes/integracaoBanco.php");
$email = filter_input(INPUT_POST, 'userEmail', FILTER_SANITIZE_STRING);
$name = filter_input(INPUT_POST, 'userName', FILTER_SANITIZE_STRING);

$result_usuario = "SELECT * FROM Usuario WHERE email='$email'";
$resultado_usuario = mysqli_query($conexao, $result_usuario);

//Econtrado usuario com esse e-mail
if(($resultado_usuario) AND ($resultado_usuario->num_rows != 0)){
        $user = mysqli_fetch_assoc($resultado_usuario);
        $_SESSION['usuarioLogado'] = $user;
        $result = 1;
    echo(json_encode($result));
}else{//Nenhum usuário encontrado
    $result = 2;
    echo(json_encode($result));
}
  • and if you include a button or link to make the Sign out?

  • My Sign out only destroys user session after logged in, searched Sign out on api but found nothing related.

  • but in fact the problem is not exactly Sign out but rather control so that it just Logs on the system when click on the Google in Sign button and not automatically picking up the information that had been generated previously.

1 answer

0


Here you are doing a validation that if Google data is validated and returns 1 goes to the

telaInicial.php( window.location.href = "http://calangoeventos.eu5.net/telas/telaInicial.php")

Do the validation by returning to the place you need, it’s quite simple

$.post('validarLoginGoogle.php', dados, function(retorna){
 if(retorna=="1"){
     window.location.href = "http://calangoeventos.eu5.net/telas/telaInicial.php";
 }else{
    window.location.href = "http://calangoeventos.eu5.net/telas/complementoCadastro.php";
   }
});

I also saw an error here, which may hinder its validation

$.post('validarLoginGoogle.php', dados, function(retorna){
                                    if(retorna=="1"){
                                            window.location.href = "http://calangoeventos.eu5.net/telas/telaInicial.php";
                                    }
                                    }else{
                                            window.location.href = "http://calangoeventos.eu5.net/telas/complementoCadastro.php";
                                    }
                            });

On line 4 there is a closure that in the case closes the if validation, not making it possible to pass the validation pro else

  • I didn’t understand very well, the validation with redirection I’m already doing, in the case of the second observation, it was just a typo because I deleted an Else if from the code.

  • "because every time I go to the login page, it already automatically picks up the information and goes to the redirect page", this is happening because you passed in your code that if the validation of Google is validates redirects to the home page, do not know if you know, but the api of Google has cookie, so when you enter the validation is done automatically.

  • I get it, thank you very much!

  • But wouldn’t it be possible to control the use of cookies? because the only way to stop accessing automatically is with Sign out and I’d like you to log in only when the user clicks the button.

  • if(retorna=="1"){&#xA; $("#botaodelogin").click(function(){&#xA; window.location.href = "http://calangoeventos.eu5.net/telas/telaInicial.php";&#xA; })&#xA; } This way will do the validation, if it is correct it will wait the click on the button you want to login

  • 1

    It worked :D Rigadão!!

  • It was nothing, I’m here if you need me

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.