PHPackages                             sakmobile/moph-auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. sakmobile/moph-auth

ActiveLibrary

sakmobile/moph-auth
===================

v1.0.0(yesterday)01↑2900%MITPHPPHP &gt;=7.4

Since Apr 3Pushed todayCompare

[ Source](https://github.com/sakmobile/moph-auth)[ Packagist](https://packagist.org/packages/sakmobile/moph-auth)[ RSS](/packages/sakmobile-moph-auth/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

MOPH Auth PHP Library
=====================

[](#moph-auth-php-library)

ไลบรารีสำหรับเชื่อมต่อ **Provider ID (MOPH Account Center)** ด้วย OAuth2 รองรับการ Login และดึงข้อมูลผู้ใช้งาน (Profile) สำหรับระบบโรงพยาบาล / หน่วยงานสาธารณสุข

---

✨ Features
----------

[](#-features)

- 🔐 Login ด้วย Health ID (OAuth2)
- 🎫 รับ Access Token (Health + Provider)
- 👤 ดึงข้อมูลผู้ใช้งาน (Profile)
- ⚡ ใช้งานง่าย (Pure PHP / รองรับทุก Framework)
- 📦 ใช้ผ่าน Composer ได้

---

📦 Installation
--------------

[](#-installation)

ติดตั้งผ่าน Composer:

```
composer require sakmobile/moph-auth
```

---

⚙️ Configuration
----------------

[](#️-configuration)

สร้างไฟล์ config:

```
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',
];
```

---

🚀 Usage
-------

[](#-usage)

### 1. สร้าง Login URL

[](#1-สร้าง-login-url)

```
use sakmobile\MophAuth\MophAuth;

$config = require 'config.php';

$auth = new MophAuth($config);

$loginUrl = $auth->getLoginUrl();

header("Location: $loginUrl");
```

---

### 2. Callback (รับ code และแลก token)

[](#2-callback-รับ-code-และแลก-token)

```
use sakmobile\MophAuth\MophAuth;

$auth = new MophAuth($config);

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

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

---

### 3. ดึงข้อมูล Profile

[](#3-ดึงข้อมูล-profile)

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

print_r($profile);
```

---

📊 ตัวอย่างข้อมูลที่ได้
----------------------

[](#-ตัวอย่างข้อมูลที่ได้)

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

---

🔐 หมายเหตุด้านความปลอดภัย
-------------------------

[](#-หมายเหตุด้านความปลอดภัย)

- `hash_cid` เป็น SHA256 ของเลขบัตรประชาชน (ไม่สามารถ decode ได้)
- ควรใช้ `hash_cid` เป็น User ID ในระบบ
- หลีกเลี่ยงการเก็บ CID จริง (PDPA)

---

🧠 Tips
------

[](#-tips)

### ดึงตำแหน่งจาก array ซ้อน

[](#ดึงตำแหน่งจาก-array-ซ้อน)

```
$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,
    ];
}
```

---

🔄 Flow การทำงาน
---------------

[](#-flow-การทำงาน)

```
Login → code
   ↓
Health Token
   ↓
Provider Token
   ↓
Profile

```

---

🛠 Requirements
--------------

[](#-requirements)

- PHP &gt;= 7.4
- cURL extension

---

📄 License
---------

[](#-license)

MIT License

---

👨‍💻 Author
----------

[](#‍-author)

**Sakwerachai**

---

⭐ Contributing
--------------

[](#-contributing)

Pull requests ยินดีต้อนรับ 🙌 หากพบปัญหา สามารถเปิด issue ได้เลย

---

---

❤️ สนับสนุนค่ากาแฟ
------------------

[](#️-สนับสนุนค่ากาแฟ)

หาก Library นี้ช่วยให้งานของคุณง่ายขึ้น ☕ สามารถสนับสนุนผู้พัฒนาเพื่อเป็นกำลังใจในการพัฒนาต่อได้ครับ

> 🙏 ทุกการสนับสนุนมีความหมายมาก

**ช่องทางสนับสนุน**

- 💸 PromptPay: `0991013326`
- 📱 หรือช่องทางอื่น ๆ ตามสะดวก

---

🛒 บริการ / ติดต่อพัฒนาเพิ่มเติม
-------------------------------

[](#-บริการ--ติดต่อพัฒนาเพิ่มเติม)

สามารถติดต่อได้ที่:

- 🌐 GitHub:
- 📧 Email:
- 💬 Line: yourlineid

---

🤝 สำหรับพี่น้อง IT โรงพยาบาล
----------------------------

[](#-สำหรับพี่น้อง-it-โรงพยาบาล)

โปรเจคนี้จัดทำขึ้นเพื่อ:

> 🎯 **แบ่งปันความรู้ และช่วยลดภาระงานของเพื่อน ๆ พี่ ๆ IT โรงพยาบาล**

สามารถ:

- ✅ นำไปใช้งานได้ฟรี
- ✅ แก้ไข / ปรับปรุงได้
- ✅ ต่อยอดได้เต็มที่

ภายใต้ MIT License 🎉

---

📢 หมายเหตุเพิ่มเติม
-------------------

[](#-หมายเหตุเพิ่มเติม)

- Library นี้เป็น **Open Source**
- ไม่มีค่าใช้จ่ายในการใช้งาน
- หากพบปัญหา หรืออยากให้เพิ่ม feature สามารถเปิด Issue ได้เลย

---

⭐ ฝากกด Star ให้กำลังใจ
-----------------------

[](#-ฝากกด-star-ให้กำลังใจ)

หากโปรเจคนี้มีประโยชน์ ฝากกด ⭐ บน GitHub เพื่อเป็นกำลังใจด้วยนะครับ 🙏

👉

---

###  Health Score

36

—

LowBetter than 81% of packages

Maintenance100

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16369242?v=4)[Werachai](/maintainers/sakmobile)[@sakmobile](https://github.com/sakmobile)

---

Top Contributors

[![sakmobile](https://avatars.githubusercontent.com/u/16369242?v=4)](https://github.com/sakmobile "sakmobile (8 commits)")

### Embed Badge

![Health badge](/badges/sakmobile-moph-auth/health.svg)

```
[![Health](https://phpackages.com/badges/sakmobile-moph-auth/health.svg)](https://phpackages.com/packages/sakmobile-moph-auth)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
