duankui1532 2019-05-04 16:32
浏览 100

Wordpress页面 - 与firebase auth和firestore PHP结合使用

I've developed an mobile App with react native where I am using firebase as backend. Now I am developing a website with wordpress for that app and I need to use the firebase database (firestore) and authentification in my website.

So I have a registration form on my website and I am using an action hook to get the data, the user entered in. Now I want to use that data and authenticate the user with firebase auth.

I used the firebase functions in Javascript for now in my app but for that case I need PHP.

I think Firebase-Admin-SDK for PHP is the correct choose. https://firebase-php.readthedocs.io/en/latest/

Unfortunately I don't really know how to use that.

I created a wordpress plugin with composer dependencies and uploaded it via FTP to my wordpress site. The plugin is also activated.

With the following code I try to create a dummy user in firebase.

Unfortunately there isn't any new record in my firebase. I don't know why it doesn't work and I am pretty new to PHP and wordpress so I don't know how to solve this problem. I know that the hook is getting called.

<?php
    /**
     * Plugin Name: Own Functions
     * Plugin URI:
     * Description: PHP code
     * Author: xxx
     * Version: 2.1.91
     * Text Domain: insert-php
     * Domain Path: /languages/
     * Author URI:
     */

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

    use Kreait\Firebase\Factory;
    use Kreait\Firebase\ServiceAccount;

    $serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');

    $firebase = (new Factory)
    ->withServiceAccount($serviceAccount)
    ->create();

    $auth = $firebase->getAuth();


 add_action('swpm_front_end_registration_complete_user_data', 'registration_callback_firebase');
    function registration_callback_firebase ($member_info)
    {
        $userProperties = [
        'email' => 'user@example.com',
        'emailVerified' => false,
        'phoneNumber' => '+15555550100',
        'password' => 'secretPassword',
        'displayName' => 'John Doe',
        'photoUrl' => 'http://www.example.com/12345678/photo.png',
        'disabled' => false,
        ];

        $createdUser = $auth->createUser($userProperties);

        // This is equivalent to:

        $request = \Kreait\Auth\Request\CreateUser::new()
        ->withUnverifiedEmail('user@example.com')
        ->withPhoneNumber('+15555550100')
        ->withClearTextPassword('secretPassword')
        ->withDisplayName('John Doe')
        ->withPhotoUrl('http://www.example.com/12345678/photo.png');

        $createdUser = $auth->createUser($request);
}

Is this the correct attempt? Or is it possible to call a javascript function (and pass the entered data) in this PHP action hook and do everything with javascript?

I would appreciate any tips if that's the right way or how I can achieve my goals.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题