【问题标题】:Uncaught Error: Call to undefined method Kreait未捕获的错误:调用未定义的方法 Kreait
【发布时间】:2020-02-25 14:35:46
【问题描述】:

我刚刚创建了 php web 服务器并将其连接到 firebase。当我尝试身份验证时,注册工作正常。但问题出在登录中。它不断收到此错误:

致命错误:未捕获错误:调用 /Applications/XAMPP/xamppfiles/htdocs/firebase_series/authActions.php:24 中未定义的方法 Kreait\Firebase\Auth::signInWithEmailAndPassword() 堆栈跟踪:#0 {main } 在第 24 行的 /Applications/XAMPP/xamppfiles/htdocs/firebase_series/authActions.php 中抛出

这里是我的验证码:

<?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");
    }


}

?>

这是我的数据库连接代码:


<?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();


?>

【问题讨论】:

  • 这不会修复您的代码,但您使用了两次if($user)。您需要删除其中之一。
  • 是的,这只是一个错字。
  • 你是如何在第一个 sn-p 中初始化 $firebase 变量的?
  • in index.php ``` ```
  • $email$pass 未在该 else 分支中设置

标签: php firebase firebase-realtime-database firebase-authentication xampp


【解决方案1】:

? 我是您正在使用的 SDK (kreait/firebase-php) 的维护者 :)

你的错误说

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

但我实际上并没有在您的代码中看到此方法。一个名为signInWithEmailAndPassword() 的方法也不存在,并且您正在使用已被弃用一段时间的方法来初始化 SDK - 请确保使用最新版本的 SDK(当时为 4.40此评论)。

一旦你有了,你就可以访问Auth::verifyPassword($email, $password)方法。

您的代码可能如下所示:

<?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";

如果您对 SDK 有其他疑问,我想邀请您Discord community dedicated to the SDK

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2017-06-11
    • 2017-12-26
    相关资源
    最近更新 更多