ãLaravelãGoogleã®OAuthèªè¨¼ã§ãã°ã¤ã³æ©è½ãå®è£ ãã(PHP)
Laravelã®Socialiteã«æåããã®ã§ã¡ã¢ã
ããããããã¨ã
Googleã®OAuthAPIãç¨ãã¦ãã°ã¤ã³æ©è½ãå®è£
ãã
â»ãã°ã¤ã³ã®ã¿ãGoogleã®APIãç¨ãã¦å¤å®ã
ã¦ã¼ã¶æ
å ±ã¯Google APIããåå¾ããã
ã¾ããä»åã¯userã®æ°è¦ä½æã¯å²æãã¾ãããã°ã¤ã³ã®ã¿ã
emailã§ç®¡çã¦ã¼ã¶ãå¦ãå¤å®ãã¾ãã
â»å¿
è¦ãªæ¹ã¯ããã°ã¤ã³æã«ã¦ã¼ã¶ãããªããã°createãã¦ãã ããã
ãç°å¢ã
Laravel5.5
PHP7.2
Google API
ãåæã
php artisan make:authãå®æ½æ¸(ãªããã°å©ãã¦ãã ãã)
Usersãã¼ãã«(ãããã¯authã«å¯¾å¿ãããã¼ãã«)ãåå¨ãã
DBã¯æ¥ç¶æ¸ã¿
ãã°ã¤ã³ã§ããGoogleã¢ã«ã¦ã³ãããã
ãã£ã¦ã¿ã¾ãã
Laravelã¯å
¬å¼ã§ãGoogleãFacebookãTwitterãªã©
æ§ã
ãªã½ã¼ã·ã£ã«èªè¨¼ã使ç¨ã§ããããã±ã¼ã¸ãæã£ã¦ãã¾ãã(ç¥)
Googleå´
OAuthæ å ±ãåå¾ã»è¨å®
ãã®è¾ºãã¯ãQiitaãè¨äºãããããããã¾ãã®ã§å²æã
Google developerã³ã³ã½ã¼ã«ã§ãããã¸ã§ã¯ãä½æãã¾ãã
Google OAuth 2.0 認証を使ったログインの実装 - Qiita
GoogleのOAuth2.0を使ってプロフィールを取得【PHP】 - FaMirror Project
ã»ã¯ã©ã¤ã¢ã³ãID(èªåçºè¡)
ã»ã¯ã©ã¤ã¢ã³ãã·ã¼ã¯ã¬ãã(èªåçºè¡)
ã»ãªãã¤ã¬ã¯ãå
(èªåã§è¨å®)Ex.https://samplesample.jp/login/google/callback
ãä»å¾å¿ è¦ã«ãªãã¾ãã
â»redirectã¯ããã°ã¤ã³å¾ã«ã¢ã¯ã»ã¹ããURLã«ãªãã¾ãããèªèº«ã§è¨å®ãã ããã
â»ä½¿ç¨ã§ããURLã«å¶éãããã¾ãï¼
.comã.jpãªã©ããããªãã¯ãã¡ã¤ã³ã®ã¿ä½¿ç¨å¯è½ã(ãã©ã¤ãã¼ãã¯localhostã®ã¿)
ããã§ããªããããã¾ããããã
apacheããã£ã¦ã.jpã«å¤æ´ãããgoogleã許ãã¦ããã¾ããã
ããgoogleãã not foundãªã©ã®ã¨ã©ã¼ãåãããå ´åãããã«åé¡ããå¯è½æ§ããã¾ãã
Googleå´ã®å¿
è¦æ
å ±ãåå¾ã§ãããã
ããããèªè¨¼ã§ãããããAPIãæå¹ã«ãã¦ããã¾ãã
APIã®æå¹å
Google ã®developerã³ã³ã½ã¼ã«ãå·¦å´ã¡ãã¥ã¼ã®ã©ã¤ãã©ãªãé¸æã
Google Cloud Platform
ã»Google + API
ã»Google Drive API
ã»Contacts API
ãã®3ã¤ãå¿ è¦ã«ãªãã¾ãã®ã§ãå ¨ã¦æå¹ã«ãã¦ãã ããã
ãããããã®ã§ããã
ããã§ãGoogleå´ã®è¨å®ã¯å®äºã§ãã
Laravelã®è¨å®
Socialiteããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«
ã¾ãã¯ãcomposerã§installãã¾ãããã
composer require laravel/socialite
Configã®è¨å®å¤æ´
ã³ã³ããã¼ã©ã§Socialiteã使ç¨ããããã
providerã¨aliasã«ç»é²ãã¦ããã¾ãã
config/app.php
<?php Laravel\Socialite\SocialiteServiceProvider::class, /.../ 'Socialite' => Laravel\Socialite\Facades\Socialite::class,
config/service.php
<?php 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => env('APP_URL').'/login/google/callback', ],
ã·ã¼ã¯ã¬ãããªã©é ãããã«ã.envãã¡ã¤ã«ã§è¨å®ãã¾ãã
.envãã¡ã¤ã«
<?php APP_URL=(èªåã®webã¢ããªURL(ä¸ã®æ¹ã«æ¢å)) ... GOOGLE_CLIENT_ID=(èªåã®ã¯ã©ã¤ã¢ã³ãID|æ°è¦ä½æ) GOOGLE_CLIENT_SECRET=(èªåã®ã¯ã©ã¤ã¢ã³ãã·ã¼ã¯ã¬ãã|æ°è¦ä½æ)
以ä¸ã§è¨å®ãå®äºãã¾ããã
Controller/Model/Viewãå®è£
ãã¾ãã
Laravelã§å®è£
Routeå®è£
ã¾ãã¯Controllerã«ã¢ã¯ã»ã¹ã§ãããããRouteãè¨å®ãã¾ãããã
<?php // ãã°ã¤ã³åã¢ã¯ã»ã¹å¯è½(Google OAuthèªè¨¼) Route::group(['prefix' => '/login/google', 'namespace' => 'Auth'], function ($router) { $router->get('/', 'LoginController@index')->name('login'); $router->get('/redirect', 'LoginController@getRedirect'); $router->get('/callback', 'LoginController@getCallback'); });
Controllerå®è£
Authã§Loginã³ã³ããã¼ã©ãããã¯ããªã®ã§ã
Loginã³ã³ããã¼ã©ãç·¨éãã¾ãã
app/Http/Controller/Auth/LoginController.php
<?php use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Support\Facades\Auth; use App\Models\User as UserModel; // ç§ã¯Modelãã£ã¬ã¯ããªé ä¸ã«UserModelãç½®ãã¦ãã®ã§ use Socialite; // Socialite使ç¨å¯è½ã«ãã class LoginController extends BaseController { use AuthenticatesUsers; private $user_model; public function __construct(UserModel $user_model) { $this->middleware('guest:admin')->except('logout'); $this->user_model = $user_model; //Usermodelãã¤ã³ã¹ã¿ã³ã¹å } /** * ã¨ã©ã¼ã®æç¡ã確èªãã¦ããã°ã¤ã³ãã¼ã¸ã«ãªãã¤ã¬ã¯ã * * @return response * @access public */ public function index() { Auth::logout(); return $this->render(); } /** * Googleèªè¨¼ãã¼ã¸ãã¦ã¼ã¶ã¼ããªãã¤ã¬ã¯ã * * @return \Illuminate\Http\Response * @access public */ public function getRedirect() { return Socialite::driver('google')->redirect(); } /** * Googleããã¦ã¼ã¶ã¼æ å ±ãåå¾ããã°ã¤ã³æ¨©éãããã°ãã°ã¤ã³ * * @return \Illuminate\Http\Response * @access public */ public function getCallback() { $email = Socialite::driver('google')->stateless()->user()->email; $user = $this->user_model->getAdminUserFromEmail($email); // ãã§ã«ç»é²ãã¦ããã¦ã¼ã¶ã¨emailãä¸è´ãããç¢ºèª if ($user === null) { return redirect() ->route('login') ->with('message', 'ãã°ã¤ã³æ¨©éãããã¾ããã'); } Auth::login($user, true); //å¼¾ãããªããã°$userãªãã¸ã§ã¯ããç¨ãã¦ãã°ã¤ã³ return redirect($this->redirectTo); //ãã°ã¤ã³å¾ãªãã¤ã¬ã¯ã } }
Modelå®è£
ããã¦ãã®UserModelã§emailããã¦ã¼ã¶ã®æç¡å¤å®ã
(ãã¹ã¯ã¼ãã¯ä½¿ç¨ããªãã®ã§ãæ¶ãã¦ãã¾ã£ã¦å¤§ä¸å¤«ã§ãã)
<?php /** * ç¹å®ã®Emailãã管çã¦ã¼ã¶ãåå¾ * * @param str $email * @return object * @access public */ public function getAdminUserFromEmail(string $email) { return $this->where('email', $email) ->first(); }
Viewã®å®è£
æå¾ã«ãViewãè¨å®ãã¦ããã
<a href="/login/google/redirect" class="btn btn-google">Sign in using Google+</a>
ãã°ã¤ã³ã§ããã°ãå®äºã§ãï¼
ãªããªãã¦ä¾¿å©ãªãã§ãããã»ã»ã»ï¼ï¼ï¼ï¼æ¶