laravel集成github登录

发布时间:2021-01-01 20:24:04 阅读:1305次

现在很网站都集成了常见的登录,比较方便

比如qq登录,微信扫码登录,以及github登录

今天介绍下如何github登录

先自己注册一个github账号,登陆成功后点击右上角的头像选择其中的settings,然后打开页面,再点击Applications,接着再点击Developer settings,再选择OAuth Apps
点击Register a new application,根据要求填入数据,点击下方Register application即可生成我们想要的Client Id 和Client Secret

执行composer require "overtrue/laravel-socialite:~2.0"来安装扩展

将以下代码加入config/app.php中

'providers' => [
    // Other service providers...
    Overtrue\LaravelSocialite\ServiceProvider::class,
],

然后将对下代码加入config/services.php中

'github' => [
    'client_id' => '0f0447920f3882ff6d8a',
    'client_secret' => 'ff3f78396b635bf7b5154137***05b0d9e933dce',
    'redirect' => 'http://test.api-game.shzhanmeng.com/api/github/login',
],

添加路由

Route::get('/github', 'API\TestController@index');
Route::get('/github/login', 'API\TestController@handleProviderCallback');

创建controller

<?php namespace App\Http\Controllers\API;

use Socialite;
use App\User;
use Illuminate\Routing\Controller;

class TestController extends Controller{
    /**
    ¦* 将用户重定向到GitHub认证页面
    ¦*/
    public function index()
    {
    ¦   return Socialite::driver('github')->redirect();
    }

    /**
    ¦* 从GitHub获取认证用户信息
    ¦*/
    public function handleProviderCallback()
    {
    ¦   $user = Socialite::driver('github')->user();
    ¦   dd($user);
    }
}

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询
上一篇:腾讯ip查询

转载请注明:laravel集成github登录 出自老鄢博客 | 欢迎分享