PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] redirect unauthorized users to another page spatie laravel

 March 04, 2022     authentication, laravel, laravel-5, laravel-permission, php     No comments   

Issue

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>


Answered By - AddWeb Solution Pvt Ltd
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing