未捕获错误:调用未定义的方法Kreait [英] Uncaught Error: Call to undefined method Kreait

查看:76
本文介绍了未捕获错误:调用未定义的方法Kreait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了php Web服务器并将其连接到Firebase。当我尝试身份验证时,注册工作得很好。但问题出在登录上。它一直收到此错误:

致命错误:未捕获错误:在KreaitFirebaseAuth::signInWithEmailAndPassword()堆栈跟踪中调用未定义的方法/Applications/XAMPP/xamppfiles/htdocs/firebase_series/authActions.php:24:#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 KreaitFirebaseFactory;
use KreaitFirebaseServiceAccount;
use KreaitFirebaseAuth;

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


?>

推荐答案

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

您的错误显示

Call to undefined method KreaitFirebaseAuth::signInWithEmailAndPassword()

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

完成后,您将可以访问Auth::verifyPassword($email, $password)方法。

然后您的代码可能如下所示:

<?php
// includes/db.php

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

use KreaitFirebaseFactory;

$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

这篇关于未捕获错误:调用未定义的方法Kreait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆