One simple way to password protect a page without user registration is to use HTTP basic authentication. This involves adding a username and password to the HTTP headers of the request. Here's an example of how to implement this in Laravel:
- Create a new middleware using the following command:
php artisan make:middleware Authenticate
- In the
handlemethod of the middleware, check if the request has the correct username and password:
public function handle($request, Closure $next)
{
$username = 'myusername';
$password = 'mypassword';
if ($request->getUser() != $username || $request->getPassword() != $password) {
$headers = array('WWW-Authenticate' => 'Basic');
return response('Unauthorized', 401, $headers);
}
return $next($request);
}
- Apply the middleware to the route you want to protect:
Route::get('/protected', function () {
return 'This page is protected.';
})->middleware('auth.basic');
- When a user visits the protected page, they will be prompted to enter a username and password. If they enter the correct credentials, they will be able to access the page.
Note that HTTP basic authentication is not the most secure method of protecting a page, as the username and password are sent in plain text and can be intercepted by a third party. However, it may be sufficient for a small website with low security requirements.