Google Login API - how do I register someone with PHP?

On Google Turn on the Google login page, at the bottom there is a section that shows how to sign a user using Javascript:

<a href="#" onclick="signOut();">Sign out</a> <script> function signOut() { var auth2 = gapi.auth2.getAuthInstance(); auth2.signOut().then(function () { console.log('User signed out.'); }); } </script> 

I searched and I cannot find a way to sign the user like this using PHP.

I found how to completely sign up a user from Google, but I don't want this. I also know that I can remove the $_SESSION variable that contains the access code, but this is still not quite what I want.

Does anyone know how I can register someone from my Google application using PHP?

+8
source share
5 answers

This should work, I fixed the problem with Mark Guinn's code, which was due to the fact that the tasks of the gapi.auth2.init(); method gapi.auth2.init(); were not completed. .next() 'he solved the problem.

 <?php session_start(); session_unset(); session_destroy(); ?> <html> <head> <meta name="google-signin-client_id" content="YOUR_CLIENT_ID"> </head> <body> <script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script> <script> window.onLoadCallback = function(){ gapi.load('auth2', function() { gapi.auth2.init().then(function(){ var auth2 = gapi.auth2.getAuthInstance(); auth2.signOut().then(function () { document.location.href = 'login.php'; }); }); }); }; </script> </body> </html> 
+4
source

JavaScript is the only way to manipulate cookies for domains other than yours.

0
source

If you want to subscribe the user to auth2.signOut() on the server side, check this code (although this is python, you should get an idea).

 app.signOut = function() { // Get `GoogleAuth` instance var auth2 = gapi.auth2.getAuthInstance(); // Sign-Out fetch('/signout', { method: 'POST', credentials: 'include' }).then(function(resp) { if (resp.status === 200) { auth2.signOut() .then(changeProfile); } else { console.error("couldn't sign out"); } }).catch(function(error) { console.error(error); }); }; 

And this one

 @app.route('/signout', methods=['POST']) def signout(): # Terminate sessions session.pop('id', None) return make_response('', 200) 

It depends on how you structure the sessions, but you can send an ajax request to the server before signOut() .

0
source

Verify that this works for you.

 header('Location:https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://www.domain.com'); 
0
source

Why don't you just make your script icon look something like this:

 <?php session_start(); session_unset(); session_destroy(); ?> <html> <head> <meta name="google-signin-client_id" content="YOUR_CLIENT_ID"> </head> <body> <script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script> <script> window.onLoadCallback = function(){ gapi.load('auth2', function() { gapi.auth2.init(); var auth2 = gapi.auth2.getAuthInstance(); auth2.signOut().then(function () { document.location.href = 'login.php'; }); }); }; </script> </body> </html> 
-1
source

Source: https://habr.com/ru/post/1244712/


All Articles