Created
March 12, 2019 10:26
-
-
Save sagormax/ac288bfe178a24fa1e62e03413a40583 to your computer and use it in GitHub Desktop.
BasicAuthMiddleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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