laravel-实现第三方github登录
1.laravel官方提供的第三方登录
1.2第三方composer
登录参考资料
链接 | 链接 | 链接 | 链接 | 中文文档 |
---|---|---|---|---|
packagist | 参考博客 | 参考博客 | 参考博客 | 中文文档 |
1.3github设置
找到
OAuth Apps
2.使用composer
安装依赖
composer require overtrue/socialite
在config/services.php
中加入配置项
#需要什么加什么这个扩展包支持好多家的登陆
'github' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',#登陆成功后要跳转的地址
],
'weibo' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
'qq' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
路由设计
Route::any('github','Admin\GithubController@login');#增加github登录的功能
Route::any('github/index','Admin\GithubController@index');#增加github登录的功能
controller代码
protected $config = [
'github' => [
'client_id' => 'e32739d55fb6c4fa880a',
'client_secret' => '7a1e5f5380ea915ed2274dda20e3f24f30c8b24c',
'redirect' => 'http://127.0.0.1:8000/github/index',
],
];
public function login()
{
$socialite = new SocialiteManager(config['github']);
$response = $socialite->driver('github')->redirect();
return $response;// or $response->send();
}
public function index()
{
$socialite = new SocialiteManager(config['github']);
$user = $socialite->driver('github')->user();
// $user->getId(); // 1472352
// $user->getNickname(); // "overtrue"
// $user->getUsername(); // "overtrue"
// $user->getName(); // "安正超"
// $user->getEmail(); // "anzhengchao@gmail.com"
// $user->getProviderName(); // GitHub
$is_null = User::where('github_email', $user->getEmail())->first();
if (empty($is_null)) {
$user = User::create(['github_email' => $user->getEmail()]);
session()->put('user', $is_null);
return \redirect('admin/index');
} else {
session()->put('user', $is_null);
return \redirect('admin/index');
}
}
curl解决方案
https://curl.haxx.se/docs/caextract.html
2.带补充现有未解决的bug