我刚刚创建了php Web服务器并将其连接到Firebase。当我尝试身份验证时,“注册”工作正常。但问题出在登录中。它不断出现此错误:
致命错误:未捕获错误:调用/Applications/XAMPP/xamppfiles/htdocs/firebase_series/authActions.php:24中未定义的方法Kreait \ Firebase \ Auth :: signInWithEmailAndPassword() }在第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();
?>
答案 0 :(得分:2)
?我是您使用的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。