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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题