我正在做一个功能来在laravel中使用Firebase登录,它可以正常工作,但是我试图将错误消息捕获到会话变量中,然后重定向到登录视图,但是显示了laravel错误视图。我如何避免这种情况?谢谢!
public function login(Request $data) {
$email = $data->input('email');
$password = $data->input('password');
try {
$user = $this->auth->verifyPassword($email, $password);
Session::put('user_uid', $user->uid);
Session::put('user_email', $user->email);
return redirect('/');
} catch (Kreait\Firebase\Exception\Auth\InvalidPassword $e) {
$message = $e->getMessage();
Session::put('message', $message);
return redirect('login');
}
}
答案 0 :(得分:0)
您需要将Kreait\Firebase\Exception\InvalidArgumentException
捕获为
public function login(Request $data) {
$email = $data->input('email');
$password = $data->input('password');
try {
$user = $this->auth->verifyPassword($email, $password);
Session::put('user_uid', $user->uid);
Session::put('user_email', $user->email);
return redirect('/');
} catch (\Kreait\Firebase\Exception\Auth\InvalidPassword $e) {
$message = $e->getMessage();
Session::put('message', $message);
return redirect('login');
} catch (\Kreait\Firebase\Exception\InvalidArgumentException $e) {
$message = $e->getMessage();
Session::put('message', $message);
return redirect('login');
}
}
如果您使用的是PHP 7.1 +
public function login(Request $data) {
$email = $data->input('email');
$password = $data->input('password');
try {
$user = $this->auth->verifyPassword($email, $password);
Session::put('user_uid', $user->uid);
Session::put('user_email', $user->email);
return redirect('/');
} catch (\Kreait\Firebase\Exception\Auth\InvalidPassword | \Kreait\Firebase\Exception\InvalidArgumentException $e) {
$message = $e->getMessage();
Session::put('message', $message);
return redirect('login');
}
}