title: redirect unauthorized users to another page spatie laravel
body: <p>I am working on spatie laravel i want to redirecct unthorized users to a specific view on that view it will have a simple button to redirect to back i have tried like this but its giving me error like</p>
  
  <pre><code>Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
  Call to a member function setCookie() on null
  </code></pre>
  
  <p>this is my code in app/exception/handler.php</p>
  
  <pre><code> public function render($request, Exception $exception)
  {
  
       if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
      // Code here ...
        return view('404');
  }
      return parent::render($request, $exception);
  }
   /**
   * Convert an authentication exception into an unauthenticated response.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Illuminate\Auth\AuthenticationException  $exception
   * @return \Illuminate\Http\Response
   */
  protected function unauthenticated($request, AuthenticationException $exception)
  {
      if ($request-&gt;expectsJson()) {
          return response()-&gt;json(['error' =&gt; 'Unauthenticated.'], 401);
      }
      $guard = array_get($exception-&gt;guards(), 0);
      switch ($guard) {
        case 'admin':
          $login = 'admin.login';
          break;
        default:
          $login = 'login';
          break;
      }
      return redirect()-&gt;guest(route($login));
  }
  </code></pre>
  
  <p>in 404.blade.php</p>
  
  <pre><code> @extends('web.layouts.default')
  @section('title', '404')
  @section('content')
  &lt;div class="container"&gt;
      &lt;div class="row justify-content-center"&gt;
          &lt;div class="col-md-8"&gt;
              &lt;div class="card"&gt;
                  &lt;div class="card-header"&gt;You are not authorized&lt;/div&gt;
  
                  &lt;div class="card-body"&gt;
  
                      &lt;a href="{{ back() }}" class="btn btn-primary"&gt; Click here to go back&lt;/a&gt;
                   &lt;/div&gt; &lt;br&gt;    &lt;br&gt;
  
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>i have gievn view homepage permission to user when this permission is there you can view profile page in profileController i have written like</p>
  
  <pre><code> &lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use Auth;
  use Spatie\Permission\Models\Permission;
  class ProfileController extends Controller
  {
       public function __construct()
      {
          //$this-&gt;middleware('auth',['except' =&gt; 'otp']);
         // return redirect(route('logout'));
           $this-&gt;middleware(['permission:view homepage','auth']);
      }
  
      /**
       * Show the application dashboard.
       *
       * @return \Illuminate\Http\Response
       */
      public function index()
      {
  
          //$per=Auth::user()-&gt;getAllPermissions();
          return view('profile');
  
      }
  }
  </code></pre>
  
  <p>when i type /profile to user who dont have view homepage permission i am getting above mentioned error</p>
  
  <p>please let me know any inputs you want from me</p>