2016-05-19 15:01:28 +01:00
|
|
|
<?php
|
|
|
|
|
2018-01-15 14:02:13 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-01-15 14:02:13 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2023-02-18 09:34:57 +00:00
|
|
|
use Illuminate\Http\Request;
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2020-08-09 15:54:10 +01:00
|
|
|
use Illuminate\View\View;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
2023-07-28 11:48:07 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress UnusedClass
|
|
|
|
*/
|
2016-05-19 15:01:28 +01:00
|
|
|
class AuthController extends Controller
|
|
|
|
{
|
2018-01-15 14:02:13 +00:00
|
|
|
/**
|
|
|
|
* Show the login form.
|
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
public function showLogin(): View|RedirectResponse
|
2017-03-03 14:23:56 +00:00
|
|
|
{
|
2019-03-21 19:46:38 +00:00
|
|
|
if (Auth::check()) {
|
|
|
|
return redirect('/');
|
|
|
|
}
|
|
|
|
|
2017-03-03 14:23:56 +00:00
|
|
|
return view('login');
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
/**
|
2023-02-18 09:34:57 +00:00
|
|
|
* Log in a user, set a session variable, check credentials against the `.env` file.
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
public function login(Request $request): RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
2023-02-18 09:34:57 +00:00
|
|
|
$credentials = $request->only('name', 'password');
|
2016-05-19 15:01:28 +01:00
|
|
|
|
2019-03-21 19:46:38 +00:00
|
|
|
if (Auth::attempt($credentials, true)) {
|
2023-10-27 20:22:40 +01:00
|
|
|
return redirect()->intended('/admin');
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('login');
|
|
|
|
}
|
2019-03-23 09:35:07 +00:00
|
|
|
|
|
|
|
/**
|
2023-02-18 09:34:57 +00:00
|
|
|
* Show the form to allow a user to log-out.
|
2019-03-23 09:35:07 +00:00
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
public function showLogout(): View|RedirectResponse
|
2019-03-23 09:35:07 +00:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public function logout(): RedirectResponse
|
|
|
|
{
|
|
|
|
Auth::logout();
|
|
|
|
|
|
|
|
return redirect('/');
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|