Add facebook user to existing login system

I want to add facebook login to my existing login system in order to remember me, I have an idea how it should work:

Short version

Check if the user is registered on facebook, the user gets the user ID and checks if he is connected to any account. If it receives some data and stores session vars, and the user is logged in.

Long version

How to register

  • The user clicks on registration, the user comes to the page with two registration options and registers with facebook.

  • If the user selects a Facebook subscription, then some information will be autofill (for example, email), and then the user will need to fill in the password and nickname. Then we check the information, if it is valid, we save the user id + facebook user. If the user chooses to register, we do not perform auto-completion and, of course, do not save the facebook user ID.

How to check if a user is logged in:

  • Check if session vars are installed if it is logged in. If not, go to step 2.

  • Check if cookie exists with code for me. If it exists, we check if the code is valid if it is logged in. If the cookie does not exist, go to step 3.

  • , facebook, , db, vars . , .

facebook, :

<?php
require_once("facebook.php");

$config = array();
$config['appId'] = 'app id';
$config['secret'] = 'secret app id';
$facebook = new Facebook($config);

$uid = $facebook->getUser();
?>

  • ?
  • , ?
  • , , ?
  • localhost, , 0 . ?

!

+5
2

: 1. , . , [native] FB - .

  1. , facebook . facebook. , facebook , URL-. , / Facebook

facebook api, $uid facebook [ ] null $uid = $facebook->getUser();

, , $uid [ null ]. , , , $user = $facebook->api('/me'). , facebook. , , , facebook, .

, api ('/me') , , try-catch . [, , . , app_id ]

$user null, URL [ facebook], $loginUrl = $facebook->getLoginUrl() URL-.

facebook api

:

<?php
require_once("facebook.php");

$config = array();
$config['appId'] = 'app id';
$config['secret'] = 'secret app id';
$facebook = new Facebook($config);

$uid = $facebook->getUser();

if( ! $uid){ // we have an id

    try{
        $user = $facebook->api('/me'); //get the current user FB public profile
        if($user){ // we have a valid user who is logged in to his facebook account
            $logoutURL = $facebook->getLogoutUrl(); // user 
         }
    }catch(FacebookApiException $e){
        $user = null;
        $loginURL = $facebook->getLoginUrl();
    }
}

PHP- facebook, . Facebook api , , .

, !

+5

1: , 2: php sdk, , fb. 3: , , . 4: , , facebook . URL- localhost. , , facebook, URL- (URL- ). :)

:)

+1

All Articles