PHP code example of sakmobile / moph-auth

1. Go to this page and download the library: Download sakmobile/moph-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sakmobile / moph-auth example snippets


return [
    'health_url' => 'https://moph.id.th',
    'client_id' => 'YOUR_CLIENT_ID',
    'client_secret' => 'YOUR_CLIENT_SECRET',
    'redirect_uri' => 'http://localhost/callback.php',

    'provider_url' => 'https://provider.id.th',
    'provider_client_id' => 'YOUR_PROVIDER_CLIENT_ID',
    'provider_secret' => 'YOUR_PROVIDER_SECRET',
];

use sakmobile\MophAuth\MophAuth;

$config = = $auth->getLoginUrl();

header("Location: $loginUrl");

use sakmobile\MophAuth\MophAuth;

$auth = new MophAuth($config);

$healthToken = $auth->getHealthToken($_GET['code']);
$providerData = $auth->getProviderToken($healthToken);

$providerToken = $providerData['data']['access_token'];

$profile = $auth->getProfile($providerToken);

print_r($profile);

Array
(
    [hash_cid] => xxxxxxxxxxxxxxxxx
    [name_th] => สมชาย ใจดี
    [email] => [email protected]
    [organization] => Array(...)
    [is_director] => false
    [is_hr_admin] => true
)

$position = $profile['organization'][0]['position'] ?? null;

function normalizeProfile($p)
{
    return [
        'cid' => $p['hash_cid'] ?? null,
        'name' => $p['name_th'] ?? null,
        'position' => $p['organization'][0]['position'] ?? null,
        'is_director' => $p['is_director'] ?? 0,
        'is_hr_admin' => $p['is_hr_admin'] ?? 0,
    ];
}