[英]Unable to redirect to login page if user is not logged in
I have a function that gets executed when a web service is called. 我有一个在调用Web服务时执行的函数。 If the user is not logged in I want to redirect to login page. 如果用户未登录,我想重定向到登录页面。 The front end I'm using is AngularJS. 我正在使用的前端是AngularJS。
The redirection is in the else
part. 重定向在else
部分。
This is the function: 这是功能:
public function postCustomerGetAction(Request $request) {
$tokenValidationObj = new LoginController();
$isValid = $tokenValidationObj->postValidateUserTokenAction($request);
// print_r($this->customer_id);exit;
// print_r($this->association_id);
// print_r($isValid);exit;
if($isValid == 1){
$log = new LogController();
$log->getLogJsonBody($this->get('logger'),$request);
$display = new Result();
try {
$schema = 'customerGet.json';
$validation = new InputValidation();
$validationObj = $validation->dataValidation($request, $schema);
$getKeyMap = array (
"_customer_id" => "customerId",
"_customer_name" => "customerName"
);
$getFunction = 'svk_apt_get_customer';
$conObj = new ConnectionController();
$conObj->beginTransaction();
$dbres = $conObj->buildQuery($getFunction, $getKeyMap, $validationObj);
$result = $display->getJsonResult($dbres);
$conObj->dbCommitTransaction();
} catch ( Exception $ex ) {
$log->getExceptionLog($this->get('logger'),$ex);
$result = $display->getJsonException($ex);
}
return $result;
}else{
$result = $this->container->getParameter('email_template_url');
// print_r($result);exit;
return new RedirectResponse($result);
// header('Location: https://stackoverflow.com');exit;
// return $this->redirect('https://stackoverflow.com');
}
} }
In the response, I see the following html but it does not redirect to the page I am specifying. 在响应中,我看到以下html,但是它没有重定向到我指定的页面。 The html in response: 响应中的html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url=https://stackoverflow.com" />
<title>Redirecting to https://stackoverflow.com</title>
</head>
<body>
Redirecting to <a href="https://stackoverflow.com">https://stackoverflow.com</a>.
</body>
</html>
How do I redirect when the web service is called and user is not logged in? 调用Web服务且用户未登录时如何重定向?
The web service / API should not be redirecting the frontend to another page. Web服务/ API不应将前端重定向到另一个页面。 If you are using Angular to interact with this web service, you should just return a "not logged in" status/flag, and then make Angular do the redirection. 如果您正在使用Angular与该Web服务进行交互,则应仅返回“未登录”状态/标志,然后使Angular进行重定向。
For a creating a custom response, you can refer to: Symfony security return 401 response instead of redirect 对于创建自定义响应,您可以参考: Symfony安全返回401响应而不是重定向
The HTTP Status 401 should be enough. HTTP状态401应该足够了。 More details about this status here: https://httpstatuses.com/401 此处有关此状态的更多详细信息: https : //httpstatuses.com/401
Angular should detect that, and then redirect. Angular应该检测到该错误,然后重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.