kreait / firebase-php

Unofficial Firebase Admin SDK for PHP
https://firebase-php.readthedocs.io/
MIT License
2.24k stars 427 forks source link

Create a New User Issue #304

Closed ansyori28 closed 5 years ago

ansyori28 commented 5 years ago

I'm having an issue while trying to create a new user with this code:

$request = \Kreait\Auth\Request\CreateUser::new()
        ->withUnverifiedEmail($email)
        ->withClearTextPassword($password)
        ->withDisplayName($nama);
$createUser = $auth->createUser($request);

I have configured the $serviceAccount initialization and $auth correctly with this code below:

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

The problem

the $createUser variable return this error:

Fatal error: Uncaught Error: Class 'Kreait\Auth\Request\CreateUser' not found in \save_user.php:14 Stack trace: #0 {main} thrown in \save_user.php on line 14

Environment

Details

Please let me know if I need to add more details.

Code to reproduce issue

Not found yet ~

jeromegamez commented 5 years ago

The class should be \Kreait\Firebase\Request\CreateUser instead of \Kreait\Auth\Request\CreateUser 😅

ansyori28 commented 5 years ago

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.

ansyori28 commented 5 years ago

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

jeromegamez commented 5 years ago

Unfortunately, I can't explain this. If you look at

https://github.com/kreait/firebase-php/blob/7a0647d324cafbd5f10cf9262ac25f26ebaf4bb4/src/Firebase.php#L52-L55

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
ansyori28 commented 5 years ago

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?

ansyori28 commented 5 years ago

I recreated the JSON from IAM in Google Cloud Platform. I think the problem does not lie in a JSON file.

jeromegamez commented 5 years ago

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());
ansyori28 commented 5 years ago

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.

ansyori28 commented 5 years ago

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?

jeromegamez commented 5 years ago

$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

lock[bot] commented 4 years ago

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.