|Implementing web site sign-up using popular social networks login in PHP|
Majority of web sites use sign-up with social networks as alternate for traditional registration/login. Buttons “Login with: Facebook, Twitter, Google,... ” are present together with a login form very often now.
Using social networks for login simplifies a user experience and saves his time.
The author used social login on different web sites as well. Based on experience it was decided to build a PHP class/package that summarise the social login process on an abstract level and allows to implement the process with minimum coding.
Social login in common
For a user a social login looks like identical process in different social networks. Usually, it looks like: a user licks on a “login with ...” button. Is redirected to a social network website (http://facebook.com etc.) , clicks “Allow” on that site and, finally, is redirected back to an original website where his social login is processes and a session for a user is created.
However, on a back-end there is some difference.
Some social networks APIs don't need to connect to an api endpoint before to redirect a user a login page. Other has to connect to an API to init a login process, to get some special redirect url, to check some settings etc.
Next difference is that some APIs will require saving a specific data (request token etc) between 2 steps of an auth process – get a redirect url and complete login. For others no need to save something.
All such differences must be taken into account during a social login process.
Introduction gelembjuk/auth PHP package to unify the social login process.
The package is available in Composer. It allows to login with Facebook, Twitter, Google, LinkedIn with unified process and minimum coding. Additionally, the package can be easy extended with new social networks and same process will be used.
It is just normal installation with Composer
Start the login processes
Prepare a redirect url. In some APIs this url must be also registered in an application settings. It must be url to your social login complete script.
Get social login auth page url. You will need to redirect a user to this page.
Before redirect remember social login object state somewhere (usually in a $_SESSION) to restore it later.
And redirect a user to a social netork login page
Complete a social login process
Next step is to read an input arguments pasted from a social network.
This is required because classes from the package don't have access to $_GET,$_REQUEST etc arrays directly (because of attempt to create a nice arhitecture).
Complete login and get a user profile from a social network
The profile is an array with keys `userid`,`name`,`email`,`imageurl`.
On practice, you rather will find DB record for such user by $profile['userid'] + $socialnetwork in your users table and remember user's internal ID in a session. If no user in the DB then he did his login for first time and you have to add him to the table.
|Last Updated on Thursday, 30 July 2015 04:38|