Implemented Custom Auth Framework

Rebuilt member authorization and session handling within Laravel's
envirnoment. Sticking with bcrypt encryption for passwords to make the
transistion simple.
about-updates
Ro 1 year ago
parent ba79c9924c
commit 14af284103
Signed by untrusted user: are0h
GPG Key ID: 29B551CDBD4D3B50

@ -0,0 +1,48 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
class AuthController extends Controller
{
public function showLogin(Request $request)
{
//$token = $request->session()->token();
//$token = csrf_token();
return view('front.login');
}
public function memberAuth(Request $request): Response
{
$token = csrf_token();
$credentials = $request->validate([
'handle' => ['required'],
'password' => ['required'],
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('den');
}
return back()->withErrors([
'error' => 'Nope. Check your crendtials, champ',
]);
}
public function leave(Request $request): Response
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->intended('login');
}
}

@ -0,0 +1,16 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class DenController extends Controller
{
//
public function start(Request $request)
{
$member = Auth::user();
return view('back.start', ['handle' => $member->handle]);
}
}

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Models\Location; use App\Models\Location;
class FrontIndexController extends Controller class FrontIndexController extends Controller
@ -11,6 +12,10 @@ class FrontIndexController extends Controller
$locations = Location::all(); $locations = Location::all();
$count = count($locations); $count = count($locations);
$terms = "no|agenda";
//$result = DB::select("SELECT * FROM searchlocations('$terms')");
return view('front.index', ['count' => $count]); return view('front.index', ['count' => $count]);
} }
} }

@ -36,11 +36,12 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class, \App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, \Illuminate\Routing\Middleware\SubstituteBindings::class,
], ],
'api' => [ 'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api', \Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class, \Illuminate\Routing\Middleware\SubstituteBindings::class,
], ],
]; ];
@ -64,5 +65,6 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class, 'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'member.check' => \App\Http\Middleware\MemberCheck::class,
]; ];
} }

@ -0,0 +1,24 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class MemberCheck
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
return $next($request);
} else {
return redirect('login');
}
}
}

@ -0,0 +1,14 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\User as Authenticatable;
class Member extends Authenticatable
{
use HasFactory;
protected $table = "member";
protected $fillable = ["uuid", "handle", "email", "password", "active", "role", "avatar", "pronoun", "gender"];
}

@ -15,7 +15,7 @@ return [
'defaults' => [ 'defaults' => [
'guard' => 'web', 'guard' => 'web',
'passwords' => 'users', 'passwords' => 'member',
], ],
/* /*
@ -38,7 +38,7 @@ return [
'guards' => [ 'guards' => [
'web' => [ 'web' => [
'driver' => 'session', 'driver' => 'session',
'provider' => 'users', 'provider' => 'member',
], ],
], ],
@ -60,9 +60,9 @@ return [
*/ */
'providers' => [ 'providers' => [
'users' => [ 'member' => [
'driver' => 'eloquent', 'driver' => 'eloquent',
'model' => App\Models\User::class, 'model' => App\Models\Member::class,
], ],
// 'users' => [ // 'users' => [
@ -91,8 +91,8 @@ return [
*/ */
'passwords' => [ 'passwords' => [
'users' => [ 'member' => [
'provider' => 'users', 'provider' => 'member',
'table' => 'password_reset_tokens', 'table' => 'password_reset_tokens',
'expire' => 60, 'expire' => 60,
'throttle' => 60, 'throttle' => 60,

@ -0,0 +1,11 @@
@extends('frame')
@section('title', 'Den|Start')
@section('main-content')
<div>
<h1>The Den</h1>
Hey {{$handle}}
</div>
@endsection

@ -0,0 +1,21 @@
@extends('frame')
@section('title', 'Login')
@section('main-content')
@parent
@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
<div>
<form action="/login" method="post" enctype="multipart/form-data">
@csrf
<label>Handle</label><br />
<input type="text" name="handle" value="" />
<br />
<label>Password</label><br />
<input type="password" name="password" value="" />
<input type="submit" value="Knock Knock" name="submit_button">
</form>
</div>
@endsection

@ -2,6 +2,8 @@
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FrontIndexController; use App\Http\Controllers\FrontIndexController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DenController;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -14,4 +16,13 @@ use App\Http\Controllers\FrontIndexController;
| |
*/ */
//index
Route::get("/", [FrontIndexController::class, 'start']); Route::get("/", [FrontIndexController::class, 'start']);
//auth
Route::get("/login", [AuthController::class, 'showLogin']);
Route::post("/login", [AuthController::class, 'memberAuth']);
//den
Route::get("/den", [DenController::class, 'start'])->middleware('member.check');
Route::get("/logout", [AuthController::class, 'leave']);

Loading…
Cancel
Save