×ðÁú¿­Ê±

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel SocialiteʵÏÖGithubµÇ¼£¿

ÔÚµ±½ñ»¥ÁªÍøʱ´ú£¬Óû§µÄµÇ¼ÒѾ­³ÉΪÁ˾ø´ó²¿·ÖwebÓ¦ÓñØÐèҪ˼Á¿µÄÊÂÇé¡£ÕâÒ²Òâζ×ÅwebÓ¦ÓÃÐèҪΪÓû§Ìṩ¿ÉÑ¡ÔñµÄ¶àÖصǼ·½·¨£¬À´½µµÍÓû§µÄµÇ¼ÄÑÌâ¶ÈºÍÓÅ»¯Óû§ÌåÑé¡£

×÷ΪĿ½ñ×îÊܽӴýµÄPHP¿ò¼ÜÖ®Ò»£¬LaravelÌṩÁËһЩºÜÊÇÀû±ãµÄ·½·¨À´ÊµÏÖÓû§µÄÉí·ÝÑéÖ¤£¬ÆäÖÐLaravel SocialiteÊÇÒ»¸öºÜÊÇÇ¿Ê¢µÄÓû§Éí·ÝÑéÖ¤½â¾ö¼Æ»®£¬ËüÔÊÐíÓ¦ÓÃʹÓõÚÈý·½Æ½Ì¨×÷ΪÉí·ÝÑéÖ¤ÌṩÕß¡£

ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃLaravel SocialiteÀ´ÊµÏÖGithubµÇ¼µÄ¹¦Ð§¡£ÏÂÃæÎÒÃÇÀ´Öð²½ÊµÏÖÕâ¸öÄ¿µÄ¡£

°ì·¨1£º½¨ÉèGithubÓ¦ÓÃ

ÔÚ×îÏȱ¾ÎÄ֮ǰ£¬Ê×ÏÈÐèҪע²áºÍ½¨ÉèÒ»¸öGithubÕ˺Å£¬²¢ÇÒÔÚGithubÖн¨ÉèÒ»¸öOAuthÓ¦ÓóÌÐò¡£½¨ÉèÓ¦ÓõÄÀú³ÌÏà¶Ô¼òÆÓ£¬Ö»Ðè×ñÕÕÒÔϼ¸¸ö°ì·¨£º

ÉÏ°¶GithubÕ˺Å£¬ÔÚ Github Settings -> Developer settings -> OAuth AppsÒ³ÃæÖн¨ÉèеÄOAuth App

ÔÚµÚÒ»²¿·ÖÌîдAppÃû³Æ¡¢Homepage URLºÍCallback URL

Callback URL ¼´ÉÏ°¶Àֳɺ󷵻صÄWebsiteµØµã£¬ÐèÒªÔÚLaravelÍøÕ¾ºǫ́½ç˵

Ìá½»±íµ¥ºó·µ»ØµÄÒ³ÃæÖлáÏÔʾGithubÓ¦ÓóÌÐòµÄclient_idºÍclient_secret¡£client_id ½«ÓÃÓÚÉèÖÃLaravel SocialiteµÄGithubÇý¶¯³ÌÐò¡£

°ì·¨2£º×°Öà Laravel Socialite

×°Öà Laravel Socialite ¿ÉÒÔͨ¹ýComposerÇáËÉÍê³É¡£Ö»ÐèÔÚÏîĿĿ¼ÏµÄÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÖ¸Áî¼´¿É£º

composer require laravel/socialite

µÇ¼ºó¸´ÖÆ

°ì·¨3£ºÉèÖÃЧÀÍÌṩÕßºÍ Facade ÒýÓÃ

·­¿ª config/app.php ÉèÖÃÎļþ£¬ÕÒµ½ providers Êý×éºÍ aliases Êý×飬½« Socialite ЧÀÍÌṩÕßºÍ Facade ÒýÓÃÌí¼Óµ½¶ÔÓ¦µÄÊý×éÖС£

'providers' => [
    // Other service providers...

    LaravelSocialiteSocialiteServiceProvider::class,
],

'aliases' => [
    // Other aliases...

    'Socialite' => LaravelSocialiteFacadesSocialite::class,
],

µÇ¼ºó¸´ÖÆ

°ì·¨4£º½ç˵GithubÇý¶¯ÉèÖÃ

ÔÚ config/services.php ÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÉèÖãº

'github' => [
    'client_id' => env('GITHUB_CLIENT_ID'),
    'client_secret' => env('GITHUB_CLIENT_SECRET'),
    'redirect' => env('GITHUB_REDIRECT'),
],

µÇ¼ºó¸´ÖÆ

°ì·¨5£º½ç˵GithubµÇ¼Á´½Ó

·­¿ªµÇ¼ҳÃæµÄ blade Ä£°åÎļþ£¬Ìí¼ÓÒÔÏ´úÂ룺

<a href="{{ url('auth/github') }}" class="btn btn-github"><i class="fa fa-github"></i> Github 怬</a>

µÇ¼ºó¸´ÖÆ

Õ⽫½¨ÉèÒ»¸öÁ´½Ó£¬Í¨¹ý»á¼û /auth/github£¬½«»áÌøתµ½GithubµÄµÇ¼ҳÃæ¡£

°ì·¨6£º½ç˵Github»Øµ÷º¯Êý

·­¿ªÎÒÃÇÔÚGithubÖÐ×¢²áʱµÄ Callback URL Ò³Ãæ²¢»ñÈ¡Á´½ÓÖеÄcode²ÎÊý£¬²¢ÔÚLaravel·ÓÉÖÐÌí¼ÓÒÔÏ´úÂ룺

Route::get('auth/github/callback', function () {
    $user = Socialite::driver('github')->user();

    // ÈôÊÇÔÚÍâµØûÓÐÕÒµ½¸ÃÓû§£¬Ôò½¨ÉèÐÂÓû§
    $existingUser = User::where('email', $user->getEmail())->first();
    if ($existingUser) {
        auth()->login($existingUser, true);
    } else {
        $newUser                  = new User;
        $newUser->name            = $user->getNickname();
        $newUser->email           = $user->getEmail();
        $newUser->github_id       = $user->getId();
        $newUser->avatar          = $user->getAvatar();
        $newUser->provider        = 'github';
        $newUser->save();
        auth()->login($newUser, true);
    }

    return redirect()->to('/home');
});

µÇ¼ºó¸´ÖÆ

ÕâÀïÐèҪעÖصÄÊÇ£¬ÎÒÃÇÐèÒª½«github_idÉúÑÄÔÚÊý¾Ý¿âÖÐÒÔ±ãÐèҪʱ¾ÙÐеǼÑéÖ¤£¬Í¬Ê±Ò²ÉúÑÄÁËÓû§êdzƺÍÍ·ÏñÀû±ãչʾ¡£

°ì·¨7£º½¨Éè.envÎļþ

·­¿ªÏîÄ¿¸ùĿ¼Ï嵀 .env Îļþ£¬Ìí¼ÓÒÔÏ Github Ïà¹ØÉèÖãº

GITHUB_CLIENT_ID=your-github-client-id-here
GITHUB_CLIENT_SECRET=your-github-client-secret-here
GITHUB_REDIRECT=http://localhost:8000/auth/github/callback

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÖУ¬ÄãÐèÒª½« your-github-client-id-here ºÍ your-github-client-secret-here Ì滻ΪÄã×Ô¼ºÔÚGithub»ñÈ¡µ½µÄclient_idºÍclient_secret¡£GITHUB_REDIRECTµÄÖµÓ¦¸ÃÊÇGithubÉÏ×¢²áÓ¦ÓóÌÐòʱµÄÖض¨ÏòURI²ÎÊý¡£

ºÃÁË£¬ÏÖÔÚÄã¿ÉÒÔʵÑéÓà Github µÇ¼À´²âÊÔÄãµÄÓ¦ÓóÌÐòÁË¡£µ±Äã»á¼û /auth/github ʱ£¬ÄãÓ¦¸Ã±»Öض¨Ïòµ½ Github µÄµÇ¼ҳÃæ¡£ÊäÈëÄãµÄ Github Õ˺źó£¬ÄãÓ¦¸Ã»á±»Öض¨Ïò»Øµ½Äãͨ¹ý Callback URI Ö¸¶¨µÄµØµã¡£

ʹÓà Laravel Socialite ȷʵÊÇʵÏÖÉí·ÝÑéÖ¤µÄÒ»ÖÖ¿ìËÙ¡¢ÇáÓ¯µÄÒªÁ죬Ëü»¹Ö§³ÖÆäËûÐí¶àÕ˺ÅÈÏ֤ЧÀÍÌṩÉÌ£¬ÈçFacebook¡¢Twitter¡¢GoogleµÈµÈ¡£Ìñ¾²ÇÒ¸ßЧµÄµÚÈý·½µÇ¼·½·¨Äܹ»¼«´óµØÌáÉýÓû§µÄÌåÑ飬ΪWebÓ¦ÓÃÌí¼Ó¶àÖصǼ·½·¨Ò²Öð½¥³ÉΪÁËÒ»ÖÖÐëÒª¡£Ïà±ÈÆð×Ô¼º×ÔÁ¦ÊµÏÖÒ»Ì×ÈÏ֤ϵͳ£¬Ê¹Óà Laravel Socialite Ôò¿ÉÒÔʹÓÃÏÖÓеÄÓÅÒìµÄÈÏ֤ЧÀÍÌṩÉÌÀ´¼ò»¯Éí·ÝÑéÖ¤¼Æ»®µÄʵÏÖ¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel SocialiteʵÏÖGithubµÇ¼£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿