CakePHP CakeDC用户插件社交登录

时间:2016-03-23 10:44:37

标签: cakephp cakephp-3.0 cakedc

其实我正在开发CakePHP3项目,我想添加社交登录:Facebook,Google ......

我找到了做完所有这些任务的CakeDC插件..

我正确安装了插件并对其进行了配置,简单的登录工作,但我遇到了Facebook和谷歌登录的麻烦..

我在Facebook和谷歌游戏机上创建了项目,我给他们这个网址:

http://projettest.com/auth/facebook

http://projettest.com/auth/google

我将此添加到我的bootstrap.php

Configure::write('OAuth.providers.facebook.options.clientId', '866098280167491');
Configure::write('OAuth.providers.facebook.options.clientSecret', 'secret');

Configure::write('OAuth.providers.google.options.clientId', '772409145076-birdmh5a3t48b8mg9knq0lonup6aeomk.apps.googleusercontent.com');
Configure::write('OAuth.providers.google.options.clientSecret', 'secret');

当我尝试使用这两种方式登录时,我接受使用我的Facebook和Google帐户登录,但之后会显示一条消息:

  

您无权访问该位置。

如果检查请求,我看到Google和Facebook提交的回复代码参数 例如 : http://projettest.com/auth/google?state=xxx&code=4/xxx&session_state=xxx&prompt=consent#

这是permission.php

的内容
return [
    'Users.SimpleRbac.permissions' => [
        [
            'role' => '*',
            'plugin' => 'CakeDC/Users',
            'controller' => '*',
            'action' => '*',
        ],
        [
            'role' => 'user',
            'plugin' => 'CakeDC/Users',
            'controller' => 'Users',
            'action' => ['register', 'edit', 'view'],
        ],
        [
            'role' => 'user',
            'plugin' => 'CakeDC/Users',
            'controller' => 'Users',
            'action' => '*',
            'allowed' => true,
        ],
        [
            'role' => ['user'],
            'controller' => ['Pages'],
            'action' => ['other', 'display'],
            'allowed' => true,
        ],
        ]];

这是users.php

的内容
$config = [
    'Users' => [
        //Table used to manage users
        'table' => 'CakeDC/Users.Users',
        //configure Auth component
        'auth' => true,
        //Password Hasher
        'passwordHasher' => '\Cake\Auth\DefaultPasswordHasher',
        //token expiration, 1 hour
        'Token' => ['expiration' => 3600],
        'Email' => [
            //determines if the user should include email
            'required' => true,
            //determines if registration workflow includes email validation
            'validate' => true,
        ],
        'Registration' => [
            //determines if the register is enabled
            'active' => true,
            //determines if the reCaptcha is enabled for registration
            'reCaptcha' => true,
        ],
        'Tos' => [
            //determines if the user should include tos accepted
            'required' => true,
        ],
        'Social' => [
            //enable social login
            'login' => false,
        ],
        'Profile' => [
            //Allow view other users profiles
            'viewOthers' => true,
            'route' => ['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile'],
        ],
        'Key' => [
            'Session' => [
                //session key to store the social auth data
                'social' => 'Users.social',
                //userId key used in reset password workflow
                'resetPasswordUserId' => 'Users.resetPasswordUserId',
            ],
            //form key to store the social auth data
            'Form' => [
                'social' => 'social'
            ],
            'Data' => [
                //data key to store the users email
                'email' => 'email',
                //data key to store email coming from social networks
                'socialEmail' => 'info.email',
                //data key to check if the remember me option is enabled
                'rememberMe' => 'remember_me',
            ],
        ],
        //Avatar placeholder
        'Avatar' => ['placeholder' => 'CakeDC/Users.avatar_placeholder.png'],
        'RememberMe' => [
            //configure Remember Me component
            'active' => true,
            'Cookie' => [
                'name' => 'remember_me',
                'Config' => [
                    'expires' => '1 month',
                    'httpOnly' => true,
                ]
            ]
        ],
    ],
//default configuration used to auto-load the Auth Component, override to change the way Auth works
    'Auth' => [
        'loginAction' => [
            'plugin' => 'CakeDC/Users',
            'controller' => 'Users',
            'action' => 'login',
            'prefix' => false
        ],
        'authenticate' => [
            'all' => [
                'scope' => ['active' => 1]
            ],
            'CakeDC/Users.ApiKey',
            'CakeDC/Users.RememberMe',
            'Form',
        ],
        'authorize' => [
            'CakeDC/Users.Superuser',
            'CakeDC/Users.SimpleRbac',
        ],
    ],
    'OAuth' => [
        'path' => ['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'socialLogin', 'prefix' => false],
        'providers' => [
            'facebook' => [
                'className' => 'League\OAuth2\Client\Provider\Facebook',
                'options' => [
                    'graphApiVersion' => 'v2.5',
                    'redirectUri' => Router::url('/auth/facebook', true)
                ]
            ],
            'twitter' => [
                'options' => [
                    'redirectUri' => Router::url('/auth/twitter', true)
                ]
            ],
            'linkedIn' => [
                'className' => 'League\OAuth2\Client\Provider\LinkedIn',
                'options' => [
                    'redirectUri' => Router::url('/auth/linkedIn', true)
                ]
            ],
            'instagram' => [
                'className' => 'League\OAuth2\Client\Provider\Instagram',
                'options' => [
                    'redirectUri' => Router::url('/auth/instagram', true)
                ]
            ],
            'google' => [
                'className' => 'League\OAuth2\Client\Provider\Google',
                'options' => [
                    'userFields' => ['url', 'aboutMe'],
                    'redirectUri' => Router::url('/auth/google', true)
                ]
            ],
        ],
    ]
];

return $config;

请帮帮我。

1 个答案:

答案 0 :(得分:0)

如果cURL调用出错,SocialAuthenticate _authenticate函数将返回false并重定向到登录页面。检查您的服务器SSL设置。检查Auth / SocialAuthenticate.php @第180行有一个例外,在$ e-> getMessage()中你会发现错误。 问题:https://github.com/CakeDC/users/issues/348