尊龙凯时人生就是搏

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel SocialiteºÍFacebookʵÏÖÉç½»µÇ¼£¿

laravelÊÇÒ»¸öÊ¢ÐеÄphp webÓ¦ÓóÌÐò¿ª·¢¿ò¼Ü£¬ËüÌṩÁ˾«Á·ºÍÓÅÑŵÄÓï·¨£¬¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¸ü¿ìËٵع¹½¨Ó¦ÓóÌÐò¡£laravel socialiteÊÇlaravelµÄÒ»¸ö²å¼þ£¬Ëü¿ÉÒÔ×ÊÖúÄãʹÓÃfacebook¡¢twitter¡¢googleµÈÉ罻ýÌåƽ̨ÌṩµÄapiʵÏÖÉç½»µÇ¼¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃlaravel socialiteºÍfacebookʵÏÖÉç½»µÇ¼¡£

×°ÖÃLaravel Socialite

ÒªÔÚLaravelÖÐʹÓÃLaravel Socialite£¬ÄúÐèÒªÏÈ×°ÖÃËü¡£Äã¿ÉÒÔʹÓÃComposerÔÚLaravelÏîÄ¿ÖÐ×°ÖÃLaravel Socialite¡£ÔÚÖÕ¶ËÖУ¬µ¼º½µ½ÏîĿĿ¼Ï²¢Ö´ÐÐÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº

composer require laravel/socialite

µÇ¼ºó¸´ÖÆ

½¨ÉèFacebookÓ¦ÓóÌÐò²¢»ñÈ¡Ó¦ÓóÌÐòƾ֤

ÔÚʹÓÃFacebookµÇ¼֮ǰ£¬ÄúÐèÒª½¨ÉèÒ»¸öFacebookÓ¦ÓóÌÐò¡£Ê×ÏÈ£¬µÇ¼Facebook¿ª·¢ÕßÍøÕ¾https://developers.facebook.com£¬Ê¹ÓÃÄúµÄFacebookƾ֤½¨ÉèÒ»¸öеÄÓ¦ÓóÌÐò¡£

½¨ÉèÓ¦ÓóÌÐòºó£¬»ñÈ¡Ó¦ÓóÌÐòƾ֤£¬µã»÷ ¡°ÉèÖá± -> ¡°»ù±¾¡± ²¢½«ÄúµÄÓ¦ÓóÌÐòIDºÍÓ¦ÓóÌÐòÉñÃØ£¨Secret Key£©¸´ÖƵ½ÈκÎÎı¾±à¼­Æ÷ÖУ¬ÎÒÃÇÉÔºó½«ÐèÒªËüÃÇ¡£

ÐÞ¸ÄenvÎļþ

ÔÚÄúµÄLaravelÏîÄ¿¸ùĿ¼ÖÐÕÒµ½¡°.env¡±Îļþ£¬½«ÒÔÏÂÄÚÈÝÌí¼Óµ½Îļþµ×²¿£º

FACEBOOK_ID=your_facebook_app_id_here
FACEBOOK_SECRET=your_facebook_app_secret_here
FACEBOOK_CALLBACK_URL=http://your_website_url_here/auth/facebook/callback

µÇ¼ºó¸´ÖÆ

ÇëÓÃÄú´ÓFacebook¿ª·¢Ö°Ô±ÍøÕ¾»ñÈ¡µÄ¡°Ó¦ÓóÌÐòID¡±ºÍ¡°ÉñÒþÃØÔ¿¡±Ìæ»»¡°your_facebook_app_id_here¡±ºÍ¡°your_facebook_app_secret_here¡±ÖеÄÎı¾¡£ ÇëÈ·±£ÔÚFACEBOOK_CALLBACK_URLÖµµÄĩβÌí¼Ó/auth/facebook/callbackÒÔ±ãÔÚFacebookµÇ¼ÀֳɺóÖض¨Ïòµ½ÄúµÄÓ¦ÓóÌÐòµÄ»Øµ÷URL¡£

½¨Éè·ÓɺͿØÖÆÆ÷

ΪʵÏÖFacebookµÇ¼£¬ÎÒÃÇÐèÒª½¨ÉèÁ½¸ö·ÓɺÍÒ»¸ö¿ØÖÆÆ÷¡£ ·Óɽ«Ö¸µ¼Óû§Ç°ÍùFacebookºÍFacebookµÇ¼»Øµ÷¡£

//Ö¸µ¼Óû§Ç°Íù Facebook µÇ¼ҳÃæ
Route::get('facebook', function () {
    return Socialite::driver('facebook')->redirect();
});

//Facebook µÇ¼»Øµ÷
Route::get('auth/facebook/callback', 'AuthFacebookController@handleCallback');

µÇ¼ºó¸´ÖÆ

¿ØÖÆÆ÷ÐèÒª´¦ÀíÀ´×ÔFacebookµÄÏìÓ¦ºÍÔÚÄúµÄÓ¦ÓóÌÐòÖн¨Éè»ò¸üÐÂÓû§¡£Ê¹ÓÃÒÔÏÂÏÂÁÉèFacebookController:

php artisan make:controller Auth/FacebookController

µÇ¼ºó¸´ÖÆ

ÔÚFacebookControllerÖУ¬ÎÒÃÇÐèÒª±àдhandleCallbackÒªÁìÀ´´¦ÀíFacebookÏìÓ¦²¢ÔÚÊý¾Ý¿âÖн¨Éè»ò¸üÐÂÓû§¡£

<?php
namespace AppHttpControllersAuth;
use AppUser;
use IlluminateHttpRequest;
use AppHttpControllersController;
use LaravelSocialiteFacadesSocialite;
class FacebookController extends Controller {
    public function handleCallback(Request $request) {
        try {
            // ´Ó Facebook »ñÈ¡Óû§ÐÅÏ¢
            $user = Socialite::driver('facebook')->user();
        } catch (Exception $e) {
            // ÈôÊÇ Facebook Ñé֤ʧ°Ü£¬Öض¨Ïòµ½µÇ¼ҳ
            return redirect('login');
        }
        // ²éÕÒÓû§
        $authUser = $this->findOrCreateUser($user);
        // µÇÈÎÃü»§
        auth()->login($authUser, true);
        // Öض¨Ïòµ½Ó¦ÓóÌÐòÊ×Ò³
        return redirect()->route('home');
    }
    private function findOrCreateUser($facebookUser) {
        // ƾ֤ Facebook Óû§ ID ºÍЧÀÍÌṩÉÌÃû³Æ²éÕÒÓû§
        $authUser = User::where('facebook_id', $facebookUser->getId())
            ->where('provider', 'facebook')
            ->first();
        if ($authUser) {
            return $authUser;
        }
        // ÈôÊÇδÕÒµ½¸ÃÓû§£¬½¨ÉèÒ»¸öÐÂÓû§
        return User::create([
            'name' => $facebookUser->getName(),
            'email' => $facebookUser->getEmail(),
            'facebook_id' => $facebookUser->getId(),
            'provider' => 'facebook',
            'avatar' => $facebookUser->getAvatar(),
            'password' => md5(rand(1,10000)),
        ]);
    }
} 

µÇ¼ºó¸´ÖÆ

×¢ÖØ£¬ÕâÀïÎÒÃÇʹÓÃmd5£¨rand£¨1,10000£©£©À´ÉèÖÃÓû§µÄËæÉñÃØÂë¡£

ÖÁ´Ë£¬Íê³ÉÁËÉç½»µÇ¼µÄʵÏÖ¡£

ÏÖÔÚÄú¿ÉÒÔ»á¼ûÓ¦ÓóÌÐòµÄ¡°/ facebook¡±Â·ÓÉ£¬²¢¿ÉÒÔͨ¹ýµ¥»÷FacebookµÇ¼À´Íê³ÉµÇ¼Àú³Ì¡£

ʹÓÃLaravel SocialiteºÍFacebook£¬ÊµÏÖÉç½»µÇ¼±äµÃºÜÊǼòÆÓ£¬¿ÉÒÔ´ó´óïÔÌ­ÄúµÄÊÂÇéÁ¿¡£LaravelºÍÆäËûһЩPHP¿ò¼ÜΪ¿ª·¢Ö°Ô±ÌṩÁËÐí¶àÒ×ÓÚʹÓõŤ¾ßÀ´¹¹½¨ÓÅÒìµÄWebÓ¦ÓóÌÐò£¬ÀýÈçÕâÀïµÄÉç½»µÇ¼¡£

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

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