0

i'm doing a single function to login with Firebase in laravel, it works correctly but i'm trying to capture the error message into a session variable then redirect to the login view but instead it shows the laravel error view. How could i avoid this?. Thanks!

enter image description here

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');
        }
    }
3
  • It's throwing InvalidArgumentException, but you are only catching the Kreait\Firebase\Exception\Auth\InvalidPassword exception. Commented Nov 22, 2018 at 17:50
  • What is the PHP version you are using ? Commented Nov 22, 2018 at 17:51
  • i'd tried catching InvalidArgumentException too, but even when i try with an invalid password i get the same error page. Im using php 7.1.12 Commented Nov 22, 2018 at 18:10

1 Answer 1

3

You need to catch Kreait\Firebase\Exception\InvalidArgumentException as

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');
    } catch (\Kreait\Firebase\Auth\SignIn\FailedToSignIn $e) {
        $message = $e->getMessage();
        Session::put('message', $message);
        return redirect('login');
    }   
}

If you are using 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 | \Kreait\Firebase\Auth\SignIn\FailedToSignIn $e) {
        $message = $e->getMessage();
        Session::put('message', $message);
        return redirect('login');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.