jonnybarnes.uk/app/Http/Controllers/AuthController.php
Jonny Barnes 2e79492b01 Protect admin routes with new eloquent sessions
When using Laravel’s own auth middleware an exception would then get
thrown which was being sent to Slack, hmmm.

So I modified the original MyAuthMiddleware to use the Auth facade
instead of a custom session key.

A logout page has also been added.
2019-03-23 09:35:07 +00:00

69 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;
class AuthController extends Controller
{
/**
* Show the login form.
*
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
*/
public function showLogin()
{
if (Auth::check()) {
return redirect('/');
}
return view('login');
}
/**
* Log in a user, set a session variable, check credentials against
* the .env file.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function login(): RedirectResponse
{
$credentials = request()->only('name', 'password');
if (Auth::attempt($credentials, true)) {
return redirect()->intended('/');
}
return redirect()->route('login');
}
/**
* Show the form to logout a user.
*
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
*/
public function showLogout()
{
if (Auth::check() === false) {
// The user is not logged in, just redirect them home
return redirect('/');
}
return view('logout');
}
/**
* Log the user out from their current session.
*
* @return \Illuminate\Http\RedirectResponse;
*/
public function logout(): RedirectResponse
{
Auth::logout();
return redirect('/');
}
}