从 Laravel 中的中间件重定向未按预期工作
Redirecting From Middleware In Laravel Is Not Working As Expected
我正在做一个 react-laravel 项目,我正在尝试使用中间件保护仪表板路由,这是来自中间件的代码
public function handle($request, Closure $next)
{
$apiToken = $request->bearerToken();
$isAuthenticated = User::where('api_token', $apiToken)
->where('is_admin', true)
->first();
if(!$isAuthenticated) {
return redirect('/');
}
return $next($request);
}
现在的问题是,如果用户未通过身份验证,中间件不会重定向到所需的路由,而是 returns html 页面作为响应中的文本,如下所示。Response in case of of not authenticated user
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="theme-color" content="#000000"/>
<meta name="description" content="Web site created using create-react-app"/>
<link rel="stylesheet" href="http://localhost/css/app.css">
<title>Some Title Here</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="http://localhost/js/app.js"></script>
</body>
</html>
我尝试使用
return redirect()->route('/');
但遗憾的是它也不起作用。
我还尝试使用
抛出 401 异常
abort(401)
并在异常处理程序中捕获它并从那里重定向(就像我对 404 和 500 异常所做的那样并且我被成功重定向)但这不起作用并且我得到了与页面相同的响应消息在响应中作为文本发送,所以问题可能出在从中间件重定向,请有人帮我找到问题吗?
return redirect('/');
会将用户重定向到项目的 public 目录。
将名称添加到所需的路线,例如:
Route::get('/route', 'API\Controller@index')->name('route name');
然后你可以重定向到指定的路由:
if(!$isAuthenticated) {
return route('route name');
}
所以作为一名前端工程师,我选择了一个捷径解决方案并使用 react 和 axios 重定向用户,所以在中间我只是抛出了一个状态为 401(未经授权)的 httperror 然后被捕获它通过 axios 如下:
我的中间件
if(!$isAuthenticated) {
return abort(401);
}
反应
const instance = axios.create({
baseURL: API_PREFIX
});
instance.interceptors.response.use(
response => {
return response;
},
function(error) {
if (error.response.status === 401) {
window.location.replace("/");
}
return Promise.reject(error.response);
}
);
对我有帮助的链接:github
我正在做一个 react-laravel 项目,我正在尝试使用中间件保护仪表板路由,这是来自中间件的代码
public function handle($request, Closure $next)
{
$apiToken = $request->bearerToken();
$isAuthenticated = User::where('api_token', $apiToken)
->where('is_admin', true)
->first();
if(!$isAuthenticated) {
return redirect('/');
}
return $next($request);
}
现在的问题是,如果用户未通过身份验证,中间件不会重定向到所需的路由,而是 returns html 页面作为响应中的文本,如下所示。Response in case of of not authenticated user
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="theme-color" content="#000000"/>
<meta name="description" content="Web site created using create-react-app"/>
<link rel="stylesheet" href="http://localhost/css/app.css">
<title>Some Title Here</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="http://localhost/js/app.js"></script>
</body>
</html>
我尝试使用
return redirect()->route('/');
但遗憾的是它也不起作用。
我还尝试使用
抛出 401 异常abort(401)
并在异常处理程序中捕获它并从那里重定向(就像我对 404 和 500 异常所做的那样并且我被成功重定向)但这不起作用并且我得到了与页面相同的响应消息在响应中作为文本发送,所以问题可能出在从中间件重定向,请有人帮我找到问题吗?
return redirect('/');
会将用户重定向到项目的 public 目录。
将名称添加到所需的路线,例如:
Route::get('/route', 'API\Controller@index')->name('route name');
然后你可以重定向到指定的路由:
if(!$isAuthenticated) {
return route('route name');
}
所以作为一名前端工程师,我选择了一个捷径解决方案并使用 react 和 axios 重定向用户,所以在中间我只是抛出了一个状态为 401(未经授权)的 httperror 然后被捕获它通过 axios 如下: 我的中间件
if(!$isAuthenticated) {
return abort(401);
}
反应
const instance = axios.create({
baseURL: API_PREFIX
});
instance.interceptors.response.use(
response => {
return response;
},
function(error) {
if (error.response.status === 401) {
window.location.replace("/");
}
return Promise.reject(error.response);
}
);
对我有帮助的链接:github