views:

105

answers:

3

I want my website to automatically detect if a user is logged into google. If they are, it checks the database to see if they are already registered. If they are registered, it logs them in to my site. Otherwise, it redirects them to the login page.

I think it might be the experimental x-has-session but I've no idea how to implement it. I'm using the JanRain OpenID PHP Library.

Responses appriciated!

+1  A: 

I have just written this exact thing using JanRain and Zend Framework, also done it with Facebook.

  1. You should create a normal registration system which uses a username, email and password. And implement some sort of Stay Logged In functionality on your log in form using a cookie to persist a token, as well as normal Session based login.
  2. Then create your Google log in link, which links to a script which talks to https://www.google.com/accounts/o8/id using JanRain OpenId.
  3. From the response you get from Google you can get their first name, last name and email, use this to populate the user table as you would with a normal registered user. But in this instance their username and password would be NULL. When this user is then in the table set up your Stay Logged In cookie.
  4. Now when the Google user returns they are already logged in using your normal Stay Logged In functionality.
  5. Your Log Out page should clear the session and remove the Stay Logged In cookie token.

You may want to consider what happens when someone logs in using Google, then logs out and then logs back in using Google at a later date. You will need to check for that user in the user table and match the user row up with the result from Google, rather than registering a new user each time.

jakenoble
A: 

I managed to figure it out. I'll respond with a blog article and a link to the live site when it's done :)

Rob
A: 

@rob, I've been googling this for most of they day, but haven't found the answer, any chance you could point me in the right direction?

Guy Van Sanden