Skip to content

Instantly share code, notes, and snippets.

@DarkGhostHunter
Created January 14, 2020 15:38
Show Gist options
  • Save DarkGhostHunter/8bd3be43129dcd1c517007e8c8710f76 to your computer and use it in GitHub Desktop.
Save DarkGhostHunter/8bd3be43129dcd1c517007e8c8710f76 to your computer and use it in GitHub Desktop.
Shares the Authenticated User in all views
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\Auth\Authenticatable;
class ShareAuthenticatedUser
{
/**
* The View Factory.
*
* @var \Illuminate\Contracts\View\Factory
*/
protected Factory $factory;
/**
* The Authenticated user, if any.
*
* @var \Illuminate\Contracts\Auth\Authenticatable|null
*/
protected Authenticatable $user;
/**
* Create a new Share Authenticated User instance.
*
* @param \Illuminate\Contracts\View\Factory $factory
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
*/
public function __construct(Factory $factory, Authenticatable $user = null)
{
$this->factory = $factory;
$this->user = $user;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->factory->share('authenticated', $this->user);
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment