Sign up with Facebook and Gmail account

Hello,

How to sign up with Facebook and Gmail account in my web site ? for the user can sign up with his facebook or gmail account and not need to regester mandatory in my web for login.

Thanks

Hi, here’s a piece of code I’m using in my application to allow users to login using FB account.

Can’t help you with Google auth…

please note, in the config params section you need to provide:




'FB_APP_ID'=> '<id of your FB AP>', 

'FB_SECRET_KEY' => '<secret key>',

'FB_API_KEY' => '<api key>',

// url of user-controller / handshake action. including your domain etc. full valid url.

'FB_HANDSHAKE_URL' => "http://mypenisinyourcoffee.com/fbuser/handshake",



  1. Controller



<?php

/**

 * @author Michal migajek Gajek

 * @copyright 2011 Michal Gajek

 * @package mg.core.user

 */

class FbUserController extends Controller 

{	

	public function actionIndex()

	{

		$this->redirect(

			"https://graph.facebook.com/oauth/authorize" 

			."?type=web_server"

			."&redirect_uri=".Yii::app()->params['FB_HANDSHAKE_URL']

			."&client_id=".Yii::app()->params['FB_APP_ID']

		);

	}

	

	/**

	 * action called by Facebook, after the user accepted the application

	 */

	public function actionHandshake()

	{

		if (!isset($_REQUEST['code']))

		{

			if (isset($_REQUEST['error_description']))

				$error = $_REQUEST['error_description'];

			else

				$error = "Auth error";

			throw new CHttpException(403, $error);

		}

			

			

		$identity = new FbUserIdentity($_REQUEST['code'], Yii::app()->params['FB_HANDSHAKE_URL']);

		if ($identity->authenticate()) 

		{

			Yii::app()->user->login($identity);

			$this->redirect('/');

		}

	}

	

}



2.FbUserIdentity class




<?php

/**

 * based on FB docs and 

 * http://amirrajan.net/Blog/asp-mvc-and-facebook-single-sign-on

 * @author Michal migajek Gajek

 * @copyright 2011 Michal Gajek

 * @package mg.identity

 */

class FbUserIdentity extends CBaseUserIdentity

{

	private $handshake_code = null;

	private $return_url = null;

	

	private $access_token = null;	

	private $fb_data = array();

	

	public $fb_id;

	public $fb_name;	

	

	/**

	 * creates an instance. Requires handshake code

	 * provided from Facebook at handshake initialization

	 */

	public function __construct($handshakeCode, $returnUrl)

	{

		$this->handshake_code = $handshakeCode;		

		$this->return_url = $returnUrl;

	}

	

	public function getId()

	{

		return $this->fb_id;	

	}

	

	public function getName()

	{

		return $this->fb_name;	

	}

	

	public function authenticate()

	{	

		// needed for logout	

		Yii::app()->user->setState("__fb_handshake_code", $this->handshake_code);

		

		$url = "https://graph.facebook.com/oauth/access_token"

		."?client_id=".Yii::app()->params['FB_APP_ID']

		."&redirect_uri={$this->return_url}"

		."&client_secret=".Yii::app()->params['FB_SECRET_KEY']

		."&code=".$this->handshake_code;

		

		$response = file_get_contents($url);

		if (empty($response))			

			throw new CHttpException(500, 'Problem z autoryzacjÄ…!');

		$response = str_replace("access_token=","", $response);

		

		$this->access_token = $response;

		

		

		// retrieve user ID

		$url = "https://graph.facebook.com/me?access_token=".$response;

		$response = file_get_contents($url);

		if (empty($response))			

			throw new CHttpException(500, 'Problem z pobraniem danych profilu!');

			

		$data = json_decode($response);

		$this->fb_id = $data->id;		

		$this->fb_data = $data;

		$this->fb_name = $data->name;

		$this->setState('fbname', $data->name);				

		

		if (!empty($this->fb_id) && ($this->fb_id > 0))

		{			 		

			$this->errorCode=self::ERROR_NONE;			

		}

        else

            $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;

        

		    

        

		return !$this->errorCode;

	}

}

Thank you migajek,

I need also for SDK or no ?

you’re welcome :)

nope, you don’t need SDK here. this code is standalone.

you’ll however need to setup Facebook Application to have your app id, api key and secret key.

// you’ll need to translate error messages to english ;)

Thanks, but please give me the code which i do in layout of in views site

thank you friend help me.

??

it does not require any code in views at all.

just navigate to "index" action of FbUserController.

i got errur

may be i have the errur in : ‘FB_HANDSHAKE_URL’

FB_HANDSHAKE_URL must be like this code? :

‘FB_HANDSHAKE_URL’ => “http://mysite.com/fbuser/handshake”,

yup, it should be like this.

first of all make sure the address is correct.

also, did you configured Facebook Application ( https://www.facebook.com/developers/#!/developers/apps.php ) correctly? You need to provide your website domain (path).

Thank you migajek, now it’s good but is still one problem is :

Unable to resolve the request "fbuser/handshake".

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

how to find : fbuser/handshake ???

my url is like this : http://mysite.com/fbuser/handshake

but i dont understood what is mean : fbuser/handshake ???

Thank you for your help

Please can you help me , i have error :

[b]Internal Server Error

file_get_contents() [<a href=‘function.file-get-contents’>function.file-get-contents</a>]: SSL: fatal protocol error

[/b]

someone can help me in this code :


public function authenticate()

        {

 

                Yii::app()->user->setState("__fb_handshake_code", $this->handshake_code);


                $url = "https://graph.facebook.com/oauth/access_token"

                ."?client_id=".Yii::app()->params['FB_APP_ID']

                ."&redirect_uri={$this->return_url}"

                ."&client_secret=".Yii::app()->params['FB_SECRET_KEY']

                ."&code=".$this->handshake_code;


                $response = file_get_contents($url);

                if (empty($response))

                        throw new CHttpException(500, 'Problem z autoryzacjÄ…!');

                $response = str_replace("access_token=","", $response);


                $this->access_token = $response;




                // retrieve user ID

                $url = "https://graph.facebook.com/me?access_token=".$response;

                $response = file_get_contents($url);

                if (empty($response))

                        throw new CHttpException(500, 'Problem z pobraniem danych profilu!');


                $data = json_decode($response);

                $this->fb_id = $data->id;

                $this->fb_data = $data;

                $this->fb_name = $data->name;

                $this->setState('fbname', $data->name);


                if (!empty($this->fb_id) && ($this->fb_id > 0))

                {

                        $this->errorCode=self::ERROR_NONE;

                }

        else

            $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;





                return !$this->errorCode;

        }

?? :)

no reply ?

Maybe this is your problem (from php.net/file-get-contents)

really i don’t know how change it !

ther’s no one can help me to sign up with facebook ?

can i know how to signup with facebook?

this extension provides facebook, google, in a same way. transparent for your application.

https://github.com/christiansalazar/crugeconnector.

       It worlks to login with google and facebook. But the facebook id or username are not adding to database.

Check once if u got solution, then send me. thnx.