LoginSignup
2
2

More than 5 years have passed since last update.

Laravel の Basic 認証で email 以外をユーザーIDとして利用する

Posted at

はい、というわけでまたまた Laravel ネタです。

Laravel の認証では Basic 認証を使うことができるようになっています。
参考

しかしながら標準機能では email / password の入力で固定になっており、ユーザー名で認証することができません。

そこでちょちょいと手を入れて email 以外のパラメーターでIDを使えるようにしてみましょう。

元々のファイル

標準ドキュメントを見ると


Route::get('profile', function() {
    // 認証済みのユーザーのみが入れる
})->middleware('auth.basic');

こんなふうに書くように指定があります。
route の中にmiddleware で指定するためには App/http/Kernel.php の中で指定が必要です。

App\Http\Kernel.php
protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

ちゃんと auth.basic がありますね。ここを見ると使われているミドルウェアのクラスが
Illuminate\Auth\Middleware\AuthenticateWithBasicAuth であることもわかります。

ミドルウェアなので handler の部分を確認してみましょう。

Illuminate\Auth\Middleware\AuthenticateWithBasicAuth

public function handle($request, Closure $next, $guard = null)
{
    return $this->auth->guard($guard)->basic() ?: $next($request);
}

ここで basic() という関数がでてきましたね。
guard()が登場しているので、Illuminate\Auth\SessionGuard を確認して見ます。

Illuminate\Auth\SessionGuard.php

public function basic($field = 'email', $extraConditions = [])
{
    if ($this->check()) {
        return;
    }

    if ($this->attemptBasic($this->getRequest(), $field, $extraConditions)) {
        return;
    }

    return $this->failedBasicResponse();
}

はい、いました basic() さん。
引数を見ると、第一引数で email と書いてあるのが確認できますね。
ここで別パラメータを渡してあげると良さそうです。

対応

basic() を呼んでいるのはミドルウェアを少し変更してあげればよさそうです。
ということで


php artisan make:middleware BasicAuthUsername

と新規にミドルウェアを作成します。
そして上記のベースのミドルウェアを継承して変更部分を足しましょう。

App\Http\Middleware\BasicAuthUsername

namespace App\Http\Middleware;

use Closure;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;

class BasicAuthUsername extends AuthenticateWithBasicAuth
{
    public function handle($request, Closure $next, $guard = null)
    {
        return $this->auth->guard($guard)->basic('username') ?: $next($request);
    }
}

user テーブルで username というカラムを使いたい場合はこのように記載してあげるだけですね。
あとは route の中で利用できるように kernel.php へ追記します。

App\Http\Kernel.php
protected $routeMiddleware = [
    ()
    'auth.basic_username' => \App\Http\Middleware\BasicAuthUsername::class,
];

最後は Route ファイルへ記載しましょう。


Route::get('/hoge/', function(){
    return view('hoge');
})->middleware('auth.basic_username');

以上です。

2
2
0

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
  3. You can use dark theme
What you can do with signing up
2
2