search
LoginSignup
19

More than 5 years have passed since last update.

Organization

[ Laravel ]facebook Loginを実装する

Laravelを使う機会がきたので、実装しながら理解した経緯を記す。

◆ 今日やること

◆ バージョン

 $ php artisan --version
Laravel Framework version 5.3.28

 $ php --version
PHP 7.1.0 (cli) (built: Dec  2 2016 03:30:24) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies

◆ 実装編

> FacebookのアプリIDとapp_secretを取得する

facebook_dev.png

> laravel/socialiteのインストール

laravel/socialiteはFacebook, Twitter, Google, LinkedIn, GitHub and BitbucketのOauthログインインターフェイスを提供します

composer require laravel/socialite

> configurationの追加

  • config/app.php
'providers' => [
    // Other service providers...

    Laravel\Socialite\SocialiteServiceProvider::class,
],
  • aliases array in your app configuration file:
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
  • .env & config/services.php
#Social Login
FACEBOOK_APP_ID=XXXXXXX
FACEBOOK_APP_SECRET=XXXXXXXXXXXXXXXXXX
FACEBOOK_CALLBACK_URL=http://localhost:8000/auth/facebook/callback
    'facebook' => [
        'client_id' =>  env('FACEBOOK_APP_ID'),
        'client_secret' => env('FACEBOOK_APP_SECRET'),
        'redirect' =>  env('FACEBOOK_CALLBACK_URL'),
    ],

> Router & Controller

  • routes/web.php
Route::get('auth/login', 'Auth\SocialController@viewLogin');
Route::get('auth/login/facebook', 'Auth\SocialController@redirectToFacebookProvider');
Route::get('auth/facebook/callback', 'Auth\SocialController@handleFacebookProviderCallback');
  • Http/Controllers/Auth/SocialController.php
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Socialite;

class SocialController extends Controller
{
        /**
     * Redirect the user to the GitHub authentication page.
     *
     * @return Response
     */
    public function viewLogin()
    {
        return view('auth.login');
    }

    /**
     * Redirect the user to the GitHub authentication page.
     *
     * @return Response
     */
    public function redirectToFacebookProvider()
    {
        return Socialite::driver('facebook')->redirect();
    }

    /**
     * Obtain the user information from GitHub.
     *
     * @return Response
     */
    public function handleFacebookProviderCallback()
    {
        try{
            $user = Socialite::driver('facebook')->user();

            if($user){
                dd($user);
                // OAuth Two Providers
                $token = $user->token;
                $refreshToken = $user->refreshToken; // not always provided
                $expiresIn = $user->expiresIn;

                // All Providers
                $user->getId();
                $user->getNickname();
                $user->getName();
                $user->getEmail();
                $user->getAvatar();

            }
        }catch(Exception $e){
            return redirect("/");
        }

        // $user->token;
    }
}
  • resources/view/auth/login.blade.php
<a href="facebook">Sign in with Facebook</a>

> facebookから取得したユーザー情報

User {#178 ▼
  +token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  +refreshToken: null
  +expiresIn: "5183964"
  +id: "XXXXXXXXXXX"
  +nickname: null
  +name: "XXXXXX XXXXX"
  +email: "XXXXXX@gmail.com"
  +avatar: "https://graph.facebook.com/v2.8/XXXXXXXXX/picture?type=normal"
  +user: array:6 [▼
    "name" => "XXXXX XXXXXX"
    "email" => "XXXXXX@gmail.com"
    "gender" => "female"
    "verified" => true
    "link" => "https://www.facebook.com/app_scoped_user_id/XXXXXXX/"
    "id" => "XXXXXXXX"
  ]
  +"avatar_original": "https://graph.facebook.com/v2.8/XXXXXX/picture?width=1920"
  +"profileUrl": "https://www.facebook.com/app_scoped_user_id/XXXXXXX/"
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
19