laravel auth facade code example
Example 1: laravel make auth
Laravel's laravel/ui package provides a quick way to scaffold all of the routes and views you need for authentication using a few simple commands:
composer require laravel/ui
php artisan ui vue --auth
Example 2: laravel check auth
use Illuminate\Support\Facades\Auth;
if (Auth::check()) {
}
Example 3: how to find the name of login user in laravel
Auth::user()->name
Example 4: laravel authentication
composer require laravel/ui
php artisan ui vue --auth
Example 5: 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 6: laravel setup auth
php artisan make:auth