From c77eeafb2c40e6231b72adda22cb86b81c5649ab Mon Sep 17 00:00:00 2001 From: ro Date: Sun, 3 Mar 2024 13:48:22 -0600 Subject: [PATCH] Login Auth Flow Added controller to handle the login process and session management that stores information about the person that has logged in so that info is available through out the app when logged in --- app/Http/Controllers/AuthController.php | 48 +++++++++++++++++++++++++ app/Http/Controllers/DashController.php | 5 +-- app/Services/AuthService.php | 18 ++++++---- resources/views/forms/login.blade.php | 13 ++++--- resources/views/frame.blade.php | 6 ---- routes/web.php | 5 +++ 6 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 app/Http/Controllers/AuthController.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php new file mode 100644 index 0000000..599b19f --- /dev/null +++ b/app/Http/Controllers/AuthController.php @@ -0,0 +1,48 @@ +auth = $authService; + } + + public function enter(Request $request): Response + { + $token = csrf_token(); + + $credentials = $request->validate([ + 'handle' => ['required'], + 'password' => ['required'], + ]); + + if ($credentials) { + $result = $this->auth->check($request); + if ($result['status']) { + //$request->session()->regenerate(); + return redirect()->intended('dashboard'); + } else { + return back()->withErrors([ + 'error' => $result['message'], + ]); + } + } else { + return back()->withErrors([ + 'error' => 'Nope. Check your crendtials, champ', + ]); + } + } + + public function exit(Request $request): Response + { + session()->flush(); + return redirect()->intended('dashboard'); + } +} diff --git a/app/Http/Controllers/DashController.php b/app/Http/Controllers/DashController.php index 5097087..82188fa 100644 --- a/app/Http/Controllers/DashController.php +++ b/app/Http/Controllers/DashController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Services\SettingsService; use App\Services\AuthService; +use Illuminate\Http\Request; class DashController extends Controller { @@ -20,10 +21,10 @@ class DashController extends Controller $this->auth = $authService; } - public function start() + public function start(Request $request) { return view('back.start', [ - "status" => false, + "status" => (session('handle') !== null ? true : false), "title" => "Fipamo Dash" ]); } diff --git a/app/Services/AuthService.php b/app/Services/AuthService.php index 5cc15ea..ffced9b 100644 --- a/app/Services/AuthService.php +++ b/app/Services/AuthService.php @@ -7,27 +7,31 @@ use function _\find; class AuthService { protected $config; + protected $request; public function __construct(SettingsService $config) { $this->config = $config; } - public function check($handle, $pass) + public function check($request) { $folks = $this->config->getFolks(); - $found = find($folks, ['handle' => $handle]); + $found = find($folks, ['handle' => $request->handle]); if ($found) { - if (password_verify($pass, $found['password'])) { - return "WELCOME"; + if (password_verify($request->password, $found['password'])) { + $request->session()->put('handle', $found['handle']); + $request->session()->put('email', $found['email']); + $request->session()->put('role', $found['role']); + $request->session()->put('avi', $found['avi']); + return ['status' => true, 'message' => 'HEY WELCOME BACK']; //DO SESSION STUFF } else { - return "NOPE"; + return ['status' => false, 'message' => 'CHECK THAT PASSWORD']; //RETURN ERROR } } else { - return "WHO ARE YOU?"; - //RETURN ERROR + return ['status' => false, 'message' => 'CHECK THAT HANDLE']; } } } diff --git a/resources/views/forms/login.blade.php b/resources/views/forms/login.blade.php index 0389d90..7c6506f 100644 --- a/resources/views/forms/login.blade.php +++ b/resources/views/forms/login.blade.php @@ -2,12 +2,17 @@
-
+ + @csrf - + @if($errors->any()) + + @else + + @endif + ? +
diff --git a/resources/views/frame.blade.php b/resources/views/frame.blade.php index 131d54b..4741345 100644 --- a/resources/views/frame.blade.php +++ b/resources/views/frame.blade.php @@ -30,12 +30,6 @@ @endif - - @if($errors->any()) -
- {{$errors->first()}} -
- @endif @if(session('message'))
{!! session('message') !!} diff --git a/routes/web.php b/routes/web.php index 978db55..cdb4aa0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,6 +2,7 @@ use Illuminate\Support\Facades\Route; use App\Http\Controllers\DashController; +use App\Http\Controllers\AuthController; /* |-------------------------------------------------------------------------- @@ -20,7 +21,11 @@ Route::get('/', function () { //DASHBOARD +//login stuff +Route::post("/login", [AuthController::class, 'enter']); + //back Route::group(['prefix' => 'dashboard'], function () { Route::get("/", [DashController::class, 'start']); + Route::get("/logout", [AuthController::class, 'exit']); });