Closed ansyori28 closed 5 years ago
The class should be \Kreait\Firebase\Request\CreateUser
instead of \Kreait\Auth\Request\CreateUser
😅
Well, I changed \Kreait\Auth\Request\CreateUser::new()
to \Kreait\Firebase\Request\CreateUser::new()
but still no luck ._.
$request = \Kreait\Firebase\Request\CreateUser::new()
->withUnverifiedEmail($email)
->withClearTextPassword($password)
->withDisplayName($nama);
$createUser = $auth->createUser($request);
Cmiiw. Thanks.
Here is my current code:
<?php
require 'firebase/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
$serviceAccount1 = ServiceAccount::fromJsonFile('json_configuration.json');
$firebase1 = (new Factory)->withServiceAccount($serviceAccount1)->create();
$auth = $firebase1->getAuth();
$email = $_POST['email'];
$password = $_POST['password'];
$nama = $_POST['nama'];
$request = \Kreait\Firebase\Request\CreateUser::new()
->withUnverifiedEmail($email)
->withClearTextPassword($password)
->withDisplayName($nama);
$createUser = $auth->createUser($request);
?>
Result:
Fatal error: Uncaught Error: Call to a member function createUser() on null in \save_user.php:18 Stack trace: #0 {main} \save_user.php on line 18
The tutorial linked to this error: https://firebase-php.readthedocs.io/en/latest/user-management.html#create-a-user
Unfortunately, I can't explain this. If you look at
it should be impossible that $firebase1->getAuth()
returns null
, the following error should be thrown instead
TypeError: Return value of Kreait\Firebase::getAuth() must be an instance of
Kreait\Firebase\Auth, null returned in /path/to/Firebase.php on line 54
I see. I think the class is now correct. I get another problem now, when I print out $auth with print_r($auth); it returns empty? it's not supposed to be empty right? am I doing something wrong in the authentication step?
I recreated the JSON from IAM in Google Cloud Platform. I think the problem does not lie in a JSON file.
If the problem was in the JSON file, then ServiceAccount::fromJsonFile('json_configuration.json')
would have failed already.
I get another problem now, when I print out $auth with print_r($auth); it returns empty? it's not supposed to be empty right?
No, it's not supposed to be empty… if you do a var_dump($firebase1->getAuth());
you should see something like
This is the script I tested this with
<?php
require 'vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/tests/_fixtures/test_credentials.json');
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->create();
var_dump($firebase->getAuth());
Alright. It's working now, I forgot that I have declared this code somewhere:
$serviceAccount = ServiceAccount::fromJsonFile('json_configuration.json');
$firebase = (new Factory)->withServiceAccount($serviceAccount)->withDatabaseUri('https://url.firebaseio.com')->create();
$database = $firebase->getDatabase();
I create another these code in the file I currently working on:
$serviceAccount1 = ServiceAccount::fromJsonFile('json_configuration.json');
$firebase1 = (new Factory)->withServiceAccount($serviceAccount1)->create();
$auth = $firebase1->getAuth();
So I think $serviceAccount and $firebase cannot be initialized more than once.
This final code is working for me on creating a new user:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require 'firebase/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
$serviceAccount1 = ServiceAccount::fromJsonFile('json_configuration.json');
$firebase1 = (new Factory)->withServiceAccount($serviceAccount1)->create();
$auth = $firebase1->getAuth();
$email = $_POST['email'];
$password = $_POST['password'];
$nama = $_POST['nama'];
$request = \Kreait\Firebase\Request\CreateUser::new()
->withUnverifiedEmail($email)
->withClearTextPassword($password)
->withDisplayName($nama);
$createUser = $auth->createUser($request);
if ($createUser) {
echo "<script>alert('New User has been Saved'); window.location ='index.php?data=user' </script>";
} else {
echo "<script>alert('Error'); window.location ='index.php?data=user' </script>";
}
}
?>
Thanks a lot, man.
Hi man. How do I get uid just after $auth->createUser($request);
executed? After the registration is done, I'd like to get the uid and put it into $_SESSION. How can I do that?
$auth->createUser()
returns a UserRecord which contains the UID, see https://github.com/kreait/firebase-php/blob/master/src/Firebase/Auth/UserRecord.php
For further usage help, please join the community chat at https://discord.gg/nbgVfty
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
I'm having an issue while trying to create a new user with this code:
I have configured the $serviceAccount initialization and $auth correctly with this code below:
The problem
the $createUser variable return this error:
Environment
7.3.6
):composer show
):Details
Please let me know if I need to add more details.
Code to reproduce issue
Not found yet ~