Uncaught Error: Call to undefined method Kreait

让人想犯罪 __ 提交于 2021-02-17 06:11:50

问题


I just created php web server and connected it to firebase. when I tried authentication, Sign up works just fine. but the problem is in Sign in. it keeps getting this error:

Fatal error: Uncaught Error: Call to undefined method Kreait\Firebase\Auth::signInWithEmailAndPassword() in /Applications/XAMPP/xamppfiles/htdocs/firebase_series/authActions.php:24 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/firebase_series/authActions.php on line 24

here my authentication code:

<?php
include("includes/db.php");


if(isset($_POST['signup']))
{

    $email = $_POST['emailSignup'];
    $pass = $_POST['passSignup'];

    $auth = $firebase->getAuth();
    $user = $auth->createUserWithEmailAndPassword($email,$pass);
    header("Location:index.php");
}

else
{

    $email = $_POST['emailSignin'];
    $pass = $_POST['passSignin'];

    $auth = $firebase->getAuth();
    $user = $auth->getUserWithEmailAndPassword($email,$pass);
    if($user)
    {
        session_start();
        $_SESSION['user'] = true;
        header("Location:home.php");
    }


}

?>

and here's my database connection code:


<?php

require __DIR__.'/vendor/autoload.php';

use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Auth;

// This assumes that you have placed the Firebase credentials in the same directory
// as this PHP file.
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');
$apiKey = 'AIzaSyCHULFKW6Kl7FXZc3ZUTYL8fq0f90-kAJ0';

$firebase = (new Factory)
    ->withServiceAccount($serviceAccount, $apiKey)
    // The following line is optional if the project id in your credentials file
    // is identical to the subdomain of your Firebase project. If you need it,
    // make sure to replace the URL with the URL of your project.
    ->withDatabaseUri('https://phpserver-f35e3.firebaseio.com/')
    ->create();

$database = $firebase->getDatabase();


?>

回答1:


👋 I'm the maintainer of the SDK (kreait/firebase-php) you're using :)

Your error says

Call to undefined method Kreait\Firebase\Auth::signInWithEmailAndPassword()

but I don't actually see this method called in your code. A method called signInWithEmailAndPassword() doesn't exist as well, and you're using methods to initialize the SDK that have been deprecated for quite some time now - please make sure to be on the latest release of the SDK (4.40 at the time of this comment).

Once you have, you will have access to the Auth::verifyPassword($email, $password) method.

Your code could then look like this:

<?php
// includes/db.php

require __DIR__.'/vendor/autoload.php';

use Kreait\Firebase\Factory;

$factory = (new Factory())->withServiceAccount(__DIR__.'/google-service-account.json');

$auth = $factory->createAuth();
// no closing "?>"
<?php
include("includes/db.php");

// Have a look at https://www.php.net/filter_input to filter user input

if (isset($_POST['signup'])) {
    $email = $_POST['emailSignup'];
    $pass = $_POST['passSignup'];

    $user = $auth->createUserWithEmailAndPassword($email,$pass);

    header("Location:index.php");
    exit;
}

$email = $_POST['emailSignin'];
$pass = $_POST['passSignin'];

if ($email && $pass && $user = $auth->verifyPassword($email, $pass)) {
    session_start();

    $_SESSION['firebase_user_id'] = $user->id;

    header("Location:home.php");
    exit;
}

echo "Authentication failed";

If you have further questions concerning the SDK, I'd like to invite you the Discord community dedicated to the SDK.



来源:https://stackoverflow.com/questions/60397030/uncaught-error-call-to-undefined-method-kreait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!