15

Suppose I want to display certain content only if I know the user coming to my website has a valid Google Account and it's logged into that account.

Is there any way to do this in Javascript? I know that the Facebook API provides ways to tell the status of a user (logged in Facebook) and I'm sure I've seen sites doing this with Google Accounts as well, but searching for the relevant terms in Google leads me to nowhere as the search terms are poorly focused.

Thank you for any help.

Cristiano Paris
  • 1,786
  • 2
  • 14
  • 21

2 Answers2

11

This blog claims to have done it, via checking for image return values linked to the social platforms provided by G+ / twitter / etc

http://www.tomanthony.co.uk/blog/detect-visitor-social-networks/

<img style="display:none;"
onload="show_login_status('Google', true)"
onerror="show_login_status('Google', false)"
src="https://accounts.google.com/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Faccounts_logo.png&followup=https%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Faccounts_logo.png&chtml=LoginDoneHtml&checkedDomains=youtube&checkConnection=youtube%3A291%3A1"
/>
Kevin
  • 4,225
  • 2
  • 37
  • 40
4
<script type="text/javascript">
function show_login_status(network, status){

    if(status == false){
        alert('NOT LOGGED IN');
    }
    if(status == true){
        alert('Logged In');
    }


}


</script>

<img style="display:none;"
onload="show_login_status('Google', true)"
onerror="show_login_status('Google', false)"
src="https://accounts.google.com/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Faccounts_logo.png&followup=https%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Faccounts_logo.png&chtml=LoginDoneHtml&checkedDomains=youtube&checkConnection=youtube%3A291%3A1"
/>

This will work.

  • Does not work for me - shows not logged in when I am. – Don't Panic Jun 21 '16 at 09:51
  • Just tested on mine and worked like a charm still. Not sure what the difference would be. – Jesse Kemper Jun 21 '16 at 20:45
  • I tried visiting my Google account page and found that Google was asking me to verify phone etc. After confirming that, this test now works! Obviously it will fail if and when those kind of account verification checks pop up, I do get them periodically. – Don't Panic Jun 23 '16 at 09:49
  • Yeah, I figured it had to be something to do with that. Glad you figured it out. – Jesse Kemper Jun 23 '16 at 12:16