Social Network Authentication in php: Setup

View: 326    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Php&mySql   Fields: Other

19 point/3 review File has been tested

Almost every website which contains a log in option, also contains ways to log in through different social networks. In this series of articles, we will take a look at how we can make sure that our visitor can log in through Google+ and eventually other networks like Twitter and Facebook.

Introduction

Almost every website which contains a log in option, also contains ways to log in through different social networks. In this series of articles, we will take a look at how we can make sure that our visitor can log in through Google+ and eventually other networks like Twitter and Facebook. In the final article, we will have a close look at how we can make sure users don’t have different accounts due to the fact that they used different social networks.

We will create a framework agnostic package which can easily handle users from different social networks. In this part, we will have a look at our basic setup.

You can view the code for all the articles on this Github page.

Creating your package

We start by creating our package. For that, we just start a fresh project without any content. We first need to define what our vendor name is. In general, people use their (user)name or company name. I will useSitePoint as my vendor name.

Next to that, we also need a package name. This name normally describes your package. Since we are using social networks to log in with, we will use SocialLogin as our package name.

It’s time to create our directory structure. In general, it’s recommended to store your classes in the srcdirectory. So let’s start by creating the directory src/SitePoint/SocialLogin.

We are going to create a composer.json file, to easily maintain our dependencies.

{
    "name": "sitepoint/social-login",
    "type": "library",
    "description": "Social login functionality",
    "homepage": "https://www.peternijssen.nl",
    "license": "MIT",
    "authors": [
        {
            "name":     "Peter Nijssen",
            "homepage": "https://www.peternijssen.nl"
        }
    ],
    "require": {
        "php":              ">=5.3.0",
        "lusitanian/oauth": "0.3.*"
    },
    "autoload": {
        "psr-0": { "SitePoint\\SocialLogin": "src" }
    },
    "target-dir": "SitePoint/SocialLogin"
}

To make sure we didn’t make any mistakes, run composer validate against the composer.json file.

Creating interfaces

We are going to create some interfaces before starting to work with the social networks. For that, we create the directory src\SitePoint\SocialLogin\Interfaces. Within this directory, we are going to create two interfaces.

We start off with the social network interface. This interface will be implemented by every class that handles the log in with a social network.

<?php

namespace SitePoint\SocialLogin\Interfaces;

interface SocialLoginInterface {

    /**
     * Initializes our service
     */
    public function init();
    
    /**
     * Returns the login url for the social network
     *
     * @return string
     */
    public function getLoginUrl();

    /**
     * Handles the login callback from the social network
     *
     * @param string $accessToken
     *
     * @return SocialUserInterface
     */
    public function loginCallback($accessToken);
}

The init method will initialize our social network login configuration. The getLoginUrl will return the URL to redirect the user to, when he logs in with the social network. The LoginCallback method needs to be called whenever you are receiving an answer from a social network. This method will return a user.

The next interface is the social user interface. From every social network, we will receive a lot of data back regarding the user. By creating a class which normalizes this data, we can easily read it and process it.

namespace SitePoint\SocialLogin\Interfaces;

interface SocialUserInterface {

    /**
     * Get the provider name
     *
     * @return string
     */
    public function getProvider();

    /**
     * Get the UID of the user
     *
     * @return string
     */
    public function getUid();

    /**
     * Get the first name of the user
     *
     * @return string
     */
    public function getFirstname();

    /**
     * Get the last name of the user
     *
     * @return string
     */
    public function getLastname();
    
    /**
     * Get the username
     *
     * @return string
     */
    public function getUsername();

    /**
     * Get the emailaddress
     *
     * @return string
     */
    public function getEmailAddress();

    /**
     * Get the city
     *
     * @return string
     */
    public function getCity();

    /**
     * Get the birthdate
     *
     * @return string
     */
    public function getBirthDate();

    /**
     * Get the gender
     *
     * @return string
     */
    public function getGender();
}

Required packages

All communication with the social networks will go through the so called OAuth protocol. Instead of developing that part ourselves, we will be using an already existing library.

Run composer require lusitanian/oauth:0.3.* on the command line to install this library

Social Network Authentication in php: Setup

Social Network Authentication in php: Setup Posted on 29-03-2016  Almost every website which contains a log in option, also contains ways to log in through different social networks. In this series of articles, we will take a look at how we can make sure that our visitor can log in through Google+ and eventually other networks like Twitter and Facebook. 6.3333333333333/10 326

Comment:

To comment you must be logged in members.

Files with category

  • How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    View: 3939    Download: 0   Comment: 0   Author: none  

    How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    Category: Php&mySql
    Fields: Other

    2.5/2 review
    With the explosion of machine learning services in recent years, it has become easier than ever for developers to create “smart apps”. In this article, I’ll introduce you to Microsoft’s offering for providing machine-learning capabilities to apps.

  • How to MySqli Tutorial PHP MySqli Extension

    View: 364    Download: 0   Comment: 0   Author: none  

    How to MySqli Tutorial PHP MySqli Extension

    Category: Php&mySql
    Fields: Other

    0/0 review
    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    View: 338    Download: 0   Comment: 0   Author: none  

    Make Laravel Artisan Commands

    Category: Php&mySql
    Fields: Other

    0/0 review
    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow.

  • Check if a Number is a Power of 2

    View: 318    Download: 0   Comment: 0   Author: none  

    Check if a Number is a Power of 2

    Category: Php&mySql
    Fields: Other

    1.5/3 review
    How to check if a number is a power of 2. To understand this question, let’s take some example.

  • Concatenate columns in MySql

    View: 381    Download: 0   Comment: 0   Author: none  

    Concatenate columns in MySql

    Category: Php&mySql
    Fields: Other

    0/2 review
    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow

  • How to Query NULL Value in MySql

    View: 317    Download: 0   Comment: 0   Author: none  

    How to Query NULL Value in MySql

    Category: Php&mySql
    Fields: Other

    5/1 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • How to Abstract Class in PHP

    View: 354    Download: 0   Comment: 0   Author: none  

    How to Abstract Class in PHP

    Category: Php&mySql
    Fields: Other

    0/0 review
    What is an abstract class in PHP and when to use an abstract class in your application. In this tutorial, we’ll learn about abstract class and their implementation.

  • Use Enums in Rails for Mapped Values

    View: 316    Download: 0   Comment: 0   Author: none  

    Use Enums in Rails for Mapped Values

    Category: Php&mySql
    Fields: Other

    2.5/2 review
    When I worked in a call center, we used to mark cases with different statuses. This allowed upper management to get a handle on where cases stood, what the bottlenecks were and flow of calls. Thankfully it has been a long time since I worked in a...

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd