DEV Community

Cover image for New PHP package for implementing authorization via social media
alexdodonov
alexdodonov

Posted on • Edited on

New PHP package for implementing authorization via social media

Installation

Just type

composer require mezon/social-network
Enter fullscreen mode Exit fullscreen mode

All supported social networks

  • Facebook
  • VKontakte
  • Odnoklassniki

If you need support of the social media wich is not in the list please use issue creation form

Facebook auth code

First of all we need to create Facebook application

After that you will be able to use this application for sign in page on your site.

And somewhere in your code you need to create object:

$facebook = new \Mezon\SocialNetwork\Auth\Facebook([
    'client_id' => 'client id from the Facebook application',
    'client_secret' => 'client secret from the Facebook application',
    'redirect_uri' => 'URI of your web application on wih user will be redirected after authorization on Facebook'
]);
Enter fullscreen mode Exit fullscreen mode

Then output a button on your page wich will let users to go on the Facebook and use ther facebook account for signing in on your site:

print('<a href="'.$facebook->getLink().'"Sign In</a>a>');
Enter fullscreen mode Exit fullscreen mode

After clicking on that link you users will be redirected on the Facebook. There they will confirm that they want to use your application for signing in and grant access to their account's data. And then they will be redurected on the 'redirect_uri' wich you have specified when you created your application on Facebook and the same URL must be used wile $facebook object setup.

// your redirect_uri must process code

if($facebook->auth($_GET['code'])) {
    // authorization was successfull
}
else {
    // an error have occured
}
Enter fullscreen mode Exit fullscreen mode

If the method $facebook->auth() have returned true then everithing is OK and you can fetch user's data:

var_dump($facebook->userInfo);
// here:
// [
//  'id' => 'user id',
//  'first_name' => 'user first name',
//  'last_name' => 'user last name',
//  'email' => 'user email, but looks like Facebook has forbidden to fetch this info, so dont rely on this field',
//  'picture' => 'user avatar'
// ]
Enter fullscreen mode Exit fullscreen mode

Learn more

More information can be found here:

Twitter
Mezon Framework

It will be great if you will contribute something to this project. Documentation, sharing the project in your social media, bug fixing, refactoring, or even submitting issue with question or feature request. Thanks anyway )

Top comments (4)

Collapse
 
nathandaly profile image
Nathan Daly

This looks pretty simple and great for bespoke applications.

The below link will give you a list of all the socialite implementations and ideas for ones you can also implement :)

socialiteproviders.com/about/

Collapse
 
alexdodonov profile image
alexdodonov

Wow impressive, thanks )

Collapse
 
edydeyemi profile image
Edydeyemi

This looks great! Bookmarked already.
Thanks for sharing.

Collapse
 
alexdodonov profile image
alexdodonov

Glad to hear that. Hope it will be usefull for you. If you need support of other social medias wich are not implemented yet - feel free to submit issue on girhub.