Skip to content

Instantly share code, notes, and snippets.

@sagormax
Created March 12, 2019 10:26
Show Gist options
  • Save sagormax/ac288bfe178a24fa1e62e03413a40583 to your computer and use it in GitHub Desktop.
Save sagormax/ac288bfe178a24fa1e62e03413a40583 to your computer and use it in GitHub Desktop.
BasicAuthMiddleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Response;
/**
* Basic Auth / Basic認証の処理
*/
class BasicAuthMiddleware
{
const AUTH_USER = "user";
const PASSWORD = "password";
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Basic認証はテストサーバーのみ行う
if (!env('APP_DEBUG')) {
return $next($request);
}
// 入力されたIDとパス
$user = $request->getUser();
$pass = $request->getPassword();
// Basic認証のログイン情報とパスワード
if($user == self::AUTH_USER && $pass == self::PASSWORD){
return $next($request);
}
// エラー時の処理
$headers = ['WWW-Authenticate' => 'Basic'];
return new Response('Invalid credentials.', 401, $headers);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment