PHP code example of venca-x / social-login
1. Go to this page and download the library: Download venca-x/social-login library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
venca-x / social-login example snippets
use VencaX;
/** @var VencaX\SocialLogin */
private $socialLogin;
public function injectSocialLogin( VencaX\SocialLogin $socialLogin )
{
$this->socialLogin = $socialLogin;
//set scope
$this->socialLogin->facebook->setScope( ['email'] );
$this->socialLogin->google->setScope( array( "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email" ) );
}
public function renderIn() {
//$facebookLoginUrl = $this->socialLogin->facebook->getLoginUrl();
//$googleLoginUrl = $this->socialLogin->google->getLoginUrl();
//$twitterLoginUrl = $this->socialLogin->twitter->getLoginUrl();
//dump( $this->socialLogin->getSocialLoginCookie() );
//$this->template->facebookLastLogin = $this->socialLogin->facebook->isThisServiceLastLogin();
//$this->template->googleLastLogin = $this->socialLogin->google->isThisServiceLastLogin();
//$this->template->twitterLastLogin = $this->socialLogin->twitter->isThisServiceLastLogin();
//...
}
public function actionFacebookLogin()
{
try
{
$me = $this->socialLogin->facebook->getMe( array( FacebookLogin::ID, FacebookLogin::EMAIL, FacebookLogin::NAME, FacebookLogin::FIRST_NAME, FacebookLogin::LAST_NAME ) );
dump( $me );
exit;
}
catch( Exception $e )
{
$this->flashMessage( $e->getMessage(), "alert-danger" );
$this->redirect("Homepage:default");
}
}
public function actionGoogleLogin( $code )
{
try
{
$me = $this->socialLogin->google->getMe( $code );
dump( $me );
exit;
}
catch( Exception $e )
{
$this->flashMessage( $e->getMessage(), "alert-danger" );
$this->redirect("Homepage:default");
}
}
//...
public function actionTwitterLogin($oauth_token, $oauth_verifier)
{
try {
$me = $this->socialLogin->twitter->getMe($oauth_token, $oauth_verifier);
//$me = $this->socialLogin->twitter->getMe($oauth_token, $oauth_verifier, true);//when zou want user's email
dump($me);
exit;
} catch (Exception $e) {
$this->flashMessage($e->getMessage(), 'alert-danger');
$this->redirect('Homepage:default');
}
}
private $backlink = null;
//render where are links to social networks
public function renderIn() {
if ($this->backlink) {
$this->socialLogin->facebook->setState($this->backlink);
$this->socialLogin->google->setState($this->backlink);
}
//$facebookLoginUrl = $this->socialLogin->facebook->getLoginUrl();
//$googleLoginUrl = $this->socialLogin->google->getLoginUrl();
//$twitterLoginUrl = $this->socialLogin->twitter->getLoginUrl();
//dump( $this->socialLogin->getSocialLoginCookie() );
//$this->template->facebookLastLogin = $this->socialLogin->facebook->isThisServiceLastLogin();
//$this->template->googleLastLogin = $this->socialLogin->google->isThisServiceLastLogin();
//$this->template->twitterLastLogin = $this->socialLogin->twitter->isThisServiceLastLogin();
}
public function actionFacebookLogin($state = NULL)
{
try
{
if ($state) $this->backlink = $state;
$me = $this->socialLogin->facebook->getMe();
//dump( $me );
//exit();
if($this->backlink != null) {
$this->redirect($this->backlink);
}
}
catch( Exception $e )
{
$this->flashMessage( $e->getMessage(), "alert-danger" );
$this->redirect("Homepage:default");
}
}
public function actionGoogleLogin( $code, $state = NULL )
{
try
{
if ($state) $this->backlink = $state;
$me = $this->socialLogin->google->getMe( $code );
//dump( $me );
//exit();
if($this->backlink != null) {
$this->redirect($this->backlink);
}
}
catch( Exception $e )
{
$this->flashMessage( $e->getMessage(), "alert-danger" );
$this->redirect("Homepage:default");
}
}
...