英文:
Laravel How to capture 419 then redirect to different login page
问题
Here is the translated content:
我已经搜索了很多。大多数帖子都说,设置csrf,
1 meta标签,name="csrf-token",content="{{ csrf_token() }}"
2 @csrf在表单标签内
3 排除路由
另一个解决方法是延长时间。
Route::get('refresh-token', function(){
    return csrf_token();
});
然后使用JavaScript编写setInterval以获取新的csrf...
我已经尝试了这些。但我想要更好、更简单的解决方案:
**如果出现419错误,重定向到登录页面。**
前端跳转至 http://mysite/login
管理员跳转至 http://mysite/admin/login
**如何捕获419和当前部分(是否为管理员)然后重定向?**
ChatGPT建议我使用App\Exceptions\Handler。它说使用render(),但在Laravel 10中有register()。我认为函数内部的代码是相同的。
它使用:
public function render($request, Exception $exception)
我的Laravel是
public function register()
我更改为
public function register(Request $request)
它说
'App\Exceptions\Handler::register()' 与方法 'Illuminate\Foundation\Exceptions\Handler::register()' 不兼容
我使用
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 \Illuminate\Session\TokenMismatchException) {
            return redirect()->route('lang.admin.login');
        };
    });
}
我认为我找到了解决方案。在renderable()中添加$request:
public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });
    $this->renderable(function (\Exception $e, $request) {
        $segments = $request->segments();
        if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            if($segments[1] == 'admin'){
                return redirect()->route('admin.login');
            }else{
                return redirect()->route('login');
            }
        };
    });
}
Hope this helps! If you have any more questions or need further assistance, feel free to ask.
英文:
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 App\Exceptions\Handler. 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
'App\Exceptions\Handler::register()' is not compatible with method 'Illuminate\Foundation\Exceptions\Handler::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 \Illuminate\Session\TokenMismatchException) {
            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 \Illuminate\Session\TokenMismatchException) {
            if($segments[1] == 'admin'){
                return redirect()->route('admin.login');
            }else{
                return redirect()->route('login');
            }
        };
    });
}
答案1
得分: 1
如何捕获419错误:使用 app\Exceptions\Handler.php
如何获取当前URL段:使用 $request
app\Exceptions\Handler.php
use Illuminate\Http\Request;
...
public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });
    $this->renderable(function (\Exception $e, $request) {
        $segments = $request->segments();
        if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            if($segments[1] == 'admin'){
                return redirect()->route('admin.login');
            }else{
                return redirect()->route('login');
            }
        };
    });
}
无需定义 $request,只需将其放在 renderable(function (\Exception $e, $request)) 中。
英文:
How to capture 419: use app\Exceptions\Handler.php
How to get current url segment: use $request
app\Exceptions\Handler.php
use Illuminate\Http\Request;
...
public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });
    $this->renderable(function (\Exception $e, $request) {
        $segments = $request->segments();
        if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            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
得分: 0
尝试在renderable回调中处理TokenMismatchException错误,如下所示:
$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');
    }
});
如果需要进一步帮助,请随时提问。
英文:
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');
    }
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论