skip to Main Content

I have googled a lot. Most of the posts says, set csrf,
1 meta tag, name="csrf-token", content="{{ csrf_token() }}"
2 @csrf inside form tag
3 exclude the route

Another workround, to extend the time.

Route::get('refresh-token', function(){
    return csrf_token();
});

Then use javascript write setInterval to get new csrf…

These I have did. But I want better and more simple solution:

If 419 happens, redirect to login page.
Frontend to http://mysite/login
Admin to http://mysite/admin/login

How to capture 419 and the current section (admin or not) then redirect??

ChatGPT tells me to use AppExceptionsHandler. It says render(), but there is register() in laravel10. I think the code is the same inside the function.

It use:

public function render($request, Exception $exception)

My laravel was

public function register()

I change to

public function register(Request $request)

It says

'AppExceptionsHandler::register()' is not compatible with method 'IlluminateFoundationExceptionsHandler::register()

I use

public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });

    $request = new Request;
    $segment = $request->segment();
    echo '<pre>', print_r($segment, 1), "</pre>"; exit;

    $this->renderable(function (Exception $e) {
        if ($e->getPrevious() instanceof IlluminateSessionTokenMismatchException) {
            return redirect()->route('lang.admin.login');
        };
    });
}

It give me white page.


I think I found the solution. Add $request inside renderable():

public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });

    $this->renderable(function (Exception $e, $request) {
        $segments = $request->segments();

        if ($e->getPrevious() instanceof IlluminateSessionTokenMismatchException) {
            if($segments[1] == 'admin'){
                return redirect()->route('admin.login');
            }else{
                return redirect()->route('login');
            }
        };
    });
}

2

Answers


  1. Chosen as BEST ANSWER

    How to capture 419: use appExceptionsHandler.php
    How to get current url segment: use $request

    appExceptionsHandler.php

    use IlluminateHttpRequest;
    ...
    
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    
        $this->renderable(function (Exception $e, $request) {
            $segments = $request->segments();
    
            if ($e->getPrevious() instanceof IlluminateSessionTokenMismatchException) {
                if($segments[1] == 'admin'){
                    return redirect()->route('admin.login');
                }else{
                    return redirect()->route('login');
                }
            };
        });
    }
    

    We don't need to define $request, just put it inside renderable(function (Exception $e, $request))


  2. Try the TokenMismatchException error in renderable callback like this

    $this->renderable(function (TokenMismatchException $e, $request) {
        $segments = $request->segments();
        if(isset($segments[1]) && $segments[1] == 'admin'){
            return redirect()->route('admin.login');
        }else{
            return redirect()->route('login');
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search