Skip to content

Instantly share code, notes, and snippets.

@mahmudinm
Forked from FlorianWeigang/filters.php
Created May 16, 2020 13:43
Show Gist options
  • Save mahmudinm/18fb0ba776fd15f7d7717fb50ddc1fa0 to your computer and use it in GitHub Desktop.
Save mahmudinm/18fb0ba776fd15f7d7717fb50ddc1fa0 to your computer and use it in GitHub Desktop.
static laravel basic.auth with username and password in config file
<?php
/**
* change this definition in filters.php.
*
* the code checks if the auth parameters matches the credentials in your config file, if not
* a WWW-Authenticate Header will be send to the client.
*/
Route::filter('auth.basic', function () {
$login = false;
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
// check credentials from config.
if (
$_SERVER['PHP_AUTH_USER'] === Config::get('app.username') &&
$_SERVER['PHP_AUTH_PW'] === Config::get('app.password')
) {
$login = true;
}
}
if ($login === false) {
return Response::make('Invalid credentials.', 401, ['WWW-Authenticate' => 'Basic']]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment