auth class in laravel code example
Example 1: laravel 7 auth
composer require laravel/ui
php artisan ui vue --auth
Example 2: laravel check auth
use Illuminate\Support\Facades\Auth;
if (Auth::check()) {
}
Example 3: create new authentication middleware laravel
php artisan make:middleware BasicAuth //In console.
//BasicAuth.php file is created:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class AdminAuth {
public function handle($request, Closure $next) {
return $next($request);
}
}
public function handle($request, Closure $next) {
$isAuthenticatedAdmin = (Auth::check() && Auth::user()->admin == 1);
if (! $isAuthenticatedAdmin)
return redirect('/login')->with('message', 'Authentication Error.');
return $next($request);
}
protected $routeMiddleware = [
'adminAuth' => \App\Http\Middleware\AdminAuth::class
];
Route::get('/adminsite', function () {
return view('adminsite');
})->middleware('adminAuth');
Example 4: laravel auth namespace
use Illuminate\Support\Facades\Auth;
Example 5: laravel setup auth
php artisan make:auth
Example 6: laravel auth
composer require laravel/ui:^2.4
php artisan ui vue --auth