# Authing - PHP

The Authing PHP SDK consists of two parts: ManagementClient and AuthenticationClient. All operations performed in ManagementClient are performed as an administrator, including modules such as managing users, managing roles, managing authority policies, and managing user pool configuration. All operations in AuthenticationClient are performed as the current terminal user, including methods such as login, register, modify user information, and log out.

You should set the initialized ManagementClient instance to a global variable (initialize only once), and AuthenticationClient should be initialized one for every request.

# Installation

We recommend using composer for installation, it can work well with some module packaging tools.

# latest stable
$ composer require authing-sdk/php

# Using the management module

Initializing ManagementClient requires userPoolId (user pool ID) and secret (user pool key):

You can click here Learn how to get UserPoolId and Secret (opens new window).

use Authing\Mgmt\ManagementClient;

$management = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
// Get admin rights
$management->requestToken();

Now the managementClient instance is ready to use. For example, you can get the list of users in the user pool:

use Authing\Mgmt\ManagementClient;

$management = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
// Get admin rights
$management->requestToken();
$users = $management->users()->paginate();

# Use authentication module

Initialization of AuthenticationClient requires userPoolId (user pool ID):

You can learn how to get UserPoolId (opens new window) here.

use Authing\Auth\AuthenticationClient;

$authentication = new AuthenticationClient("AUTHING_USERPOOL_ID");

Then you can register and log in and other operations:

use Authing\Auth\AuthenticationClient;
use Authing\Types\LoginByEmailInput;

$authentication = new AuthenticationClient("AUTHING_USERPOOL_ID");
$user = $authentication->loginByEmail(new LoginByEmailInput("test@example.com", "123456"));

After logging in, methods such as update_profile that require users to log in are available:

use Authing\Auth\AuthenticationClient;
use Authing\Types\LoginByEmailInput;
use Authing\Types\UpdateUserInput;

$authentication = new AuthenticationClient("AUTHING_USERPOOL_ID");
$authentication->loginByEmail(new LoginByEmailInput("test@example.com", "123456"));

$user = $authentication->updateProfile((new UpdateUserInput())->withNickname("nickname"));

You can also set the AccessToken parameter after initialization, without calling the LoginByXXX method every time:

use Authing\Auth\AuthenticationClient;

$authentication = new AuthenticationClient("AUTHING_USERPOOL_ID");
$authentication->setAccessToken("ACCESS_TOKEN");

Execute the UpdateProfile method again and find that it is also successful:

use Authing\Auth\AuthenticationClient;
use Authing\Types\UpdateUserInput;

$authentication = new AuthenticationClient("AUTHING_USERPOOL_ID");
$authentication->setAccessToken("ACCESS_TOKEN");

$user = $authentication->updateProfile((new UpdateUserInput())->withNickname("nickname"));

# Error handling

Unified use of try catch processing:

use Authing\Auth\AuthenticationClient;
use Authing\Types\UpdateUserInput;

$authentication = new AuthenticationClient("AUTHING_USERPOOL_ID");
$authentication->setAccessToken("ACCESS_TOKEN");

try {
    $user = $authentication->updateProfile((new UpdateUserInput())->withNickname("nickname"));
} catch (Exception $e) {
    print_r($e);
}

# Get help

Join us on Gitter: #authing-chat (opens new window)

# Interface Index

Available Authentication methods

  • Get the user information of the current user: getCurrentUser
  • Register with email: registerByEmail
  • Register with username: registerByUsername
  • Register with mobile phone number verification code: registerByPhoneCode
  • Login with email: loginByEmail
  • Login with username: loginByUsername
  • Use mobile phone number verification code to log in to loginByPhoneCode
  • Use mobile phone number password to log in: loginByPhonePassword
  • Send email: sendEmail
  • Send SMS verification code: sendSmsCode
  • Check the valid status of the token: checkLoginStatus
  • Use phone number verification code to reset password: resetPasswordByPhoneCode
  • Use email verification code to reset password: resetPasswordByEmailCode
  • Update user profile: updateProfile
  • Update password: updatePassword
  • Update phone number: updatePhone
  • Update email: updateEmail
  • Refresh token: refreshToken
  • Bind mobile phone number: bindPhone
  • Unbind phone number: unbindPhone

For details, see:

Core Authentication Module

The management module contains the following sub-modules:

Manage users Manage Roles Manage Strategy Manage Authority & Access Control Manage custom field metadata