Search code examples
phplaravelauthenticationlaravel-5laravel-permission

redirect unauthorized users to another page spatie laravel


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

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function setCookie() on null

this is my code in app/exception/handler.php

 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->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }
    $guard = array_get($exception->guards(), 0);
    switch ($guard) {
      case 'admin':
        $login = 'admin.login';
        break;
      default:
        $login = 'login';
        break;
    }
    return redirect()->guest(route($login));
}

in 404.blade.php

 @extends('web.layouts.default')
@section('title', '404')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">You are not authorized</div>

                <div class="card-body">

                    <a href="{{ back() }}" class="btn btn-primary"> Click here to go back</a>
                 </div> <br>    <br>

        </div>
    </div>
</div>
@endsection

i have gievn view homepage permission to user when this permission is there you can view profile page in profileController i have written like

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use Spatie\Permission\Models\Permission;
class ProfileController extends Controller
{
     public function __construct()
    {
        //$this->middleware('auth',['except' => 'otp']);
       // return redirect(route('logout'));
         $this->middleware(['permission:view homepage','auth']);
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        //$per=Auth::user()->getAllPermissions();
        return view('profile');

    }
}

when i type /profile to user who dont have view homepage permission i am getting above mentioned error

please let me know any inputs you want from me


Solution

  • You should try to below button use in your 404.blade.php file and it's working fine.

    <a href="{{ redirect()->back()->getTargetUrl() }}" class="btn btn-primary"> Click here to go back</a>