venca-x/social-login

Nette 扩展。使用社交网络登录(Facebook、Google、Twitter)

1.2.15 2022-03-15 18:55 UTC

README

Build Status Coverage Status Latest Stable Version Latest Unstable Version Total Downloads License

Nette 扩展,用于社交网络登录

所有 Facebook 字段的权限

安装

安装 dev-master 版本以用于 Nette 3.0

composer require venca-x/social-login:dev-master

安装 1.2.x 版本以用于 Nette 3.0(Nette\SmartObject)

composer require venca-x/social-login:^1.2.0

安装 1.1.x 版本以用于 Nette 2.4(Nette\SmartObject)

composer require venca-x/social-login:^1.1.0

安装 1.0.x 版本以用于 Nette 2.4Nette 2.3(Nette\Object)

composer require venca-x/social-login:^1.0.0

配置

config.neon

	parameters:
		facebook:
			appId: '123456789'
			appSecret: '987654321'
			callbackURL: 'http://www.muj-web.cz/homepage/facebook-login'
			defaultFbGraphVersion: 'v8.0'
		google:
			clientId: '123456789'
			clientSecret: '987654321'
			callbackURL: 'http://www.muj-web.cz/homepage/google-login'
		twitter:
			consumerKey: '123456789'
			consumerSecret: '987654321'
			callbackURL: 'http://www.muj-web.cz/homepage/twitter-login'

	nette:
		session:
			autoStart: true  # default is smart	

    services:
        ...
        - VencaX\SocialLogin({ facebook: %facebook%, google: %google%, twitter: %twitter% }, 'domain-social-login' )

将 'domain-social-login' 替换为您的唯一标识符(它是登录后最后使用服务的 cookie 名称)

BasePresenter.php

    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/plus.me", "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();
        //...
    }

in.latte 的布局

    <a rel="nofollow" href="{$facebookLoginUrl}" {if $facebookLastLogin}class="last-login"{/if}><i class="fa fa-facebook-square fa-lg"></i></a>
    <a rel="nofollow" href="{$googleLoginUrl}" {if $googleLastLogin}class="last-login"{/if}><i class="fa fa-google-plus-square fa-lg"></i></a><br/>
    <a rel="nofollow" href="{plink User:twitterLogin}" {if $twitterLastLogin}class="last-login"{/if}><i class="fa fa-twitter-square fa-lg"></i></a><br/>
    <a rel="nofollow" href="{plink User:registration}"><i class="fa fa-plus-square fa-lg"></i> Zaregistrovat</a>

简单登录

HomepagePresenter.php

    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");
        }
    }
    //...

使用 Twitter 简单登录

    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');
        }
    }

带链接的登录

当您希望在成功登录后重定向到特定 URL 时使用它

HomepagePresenter.php

    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");
        }
    }
    ...

注册

Facebook

Facebook 开发者 - 创建新的网站应用。完整:设置 -> 网页 -> 网站URL : http://www.mypage.com

Google

API 控制台 - Google Code - 创建新项目,添加 Google+ API:API & auth -> API -> Google+ API 设置为 ON 凭据:API & auth -> 凭据 -> 创建新 Client ID -> 网络应用

Twitter

在 dev.twitter.com/apps/ 上注册一个新应用