hamuga-id-sdk
v0.1.11
Published
Hamuga ID SDK for React Native
Readme
hamuga-id-sdk
Hamuga ID SDK for React Native. Supports both New & Old Architecture.
English
Installation
npm install hamuga-id-sdkAfter installing the package, you need to install the native dependencies:
cd example/ios && pod installNote for iOS: This library requires SafariServices.framework. The podspec links it automatically, but if you encounter issues, manually link it in Xcode's "Link Binary With Libraries" build phase.
Usage
This library provides two ways to initiate the Hamuga ID authentication flow:
1. HamugaAuthButton
A customizable button component that opens the authentication screen.
import { HamugaAuthButton } from 'hamuga-id-sdk';
// ...
<HamugaAuthButton
baseUrl="https://your-auth-server.com"
client_id="your-client-id"
redirect_uri="your-app://callback"
scope={['openid', 'email']}
/>;2. hamugaIdOpen
A function to programmatically open the authentication screen.
import { hamugaIdOpen } from 'hamuga-id-sdk';
// ...
const handleLogin = () => {
hamugaIdOpen({
baseUrl: 'https://your-auth-server.com',
client_id: 'your-client-id',
redirect_uri: 'your-app://callback',
scope: ['openid', 'email'],
autoClose: true, // Optional: automatically closes browser on redirect match
});
};Utility Functions
buildAuthUrl
Constructs the authentication URL without opening the browser.
import { buildAuthUrl } from 'hamuga-id-sdk';
const url = buildAuthUrl({
baseUrl: '...',
client_id: '...',
redirect_uri: '...',
scope: ['openid'],
});getAuthCodeFromUrl
Extracts the code parameter from a redirect URL.
import { getAuthCodeFromUrl } from 'hamuga-id-sdk';
const code = getAuthCodeFromUrl(url);Props
AuthProps
Available for HamugaAuthButton, hamugaIdOpen, and buildAuthUrl.
| Prop | Type | Default | Description |
| --------------- | --------------------------------------------- | ------------ | ---------------------------------------- |
| baseUrl | string | | Base URL of your authentication server. |
| client_id | string | | Your client ID. |
| redirect_uri | string | | URI to redirect to after authentication. |
| scope | ('openid' \| 'email' \| 'phone' \| 'dan')[] | ['openid'] | Scopes to request. |
| state | string | '123' | Random string to prevent CSRF. |
| response_type | string | 'code' | Response type. |
| username | 'phone' \| 'email' | | Prefilled username type. |
| autoClose | boolean | true | Auto-close browser on redirect match. |
ButtonProps
Available only for HamugaAuthButton.
| Prop | Type | Default | Description |
| ----------- | ----------- | ----------------------- | ------------------------ |
| title | string | 'Хамуга ID-р нэвтрэх' | Button text. |
| style | ViewStyle | | Button container styles. |
| textStyle | TextStyle | | Button text styles. |
Монгол
Суулгах
npm install hamuga-id-sdkБагцыг суулгасны дараа native сангуудыг суулгах шаардлагатай:
cd example/ios && pod installiOS-д зориулсан тэмдэглэл: Энэхүү сан нь SafariServices.framework-ийг ашигладаг. Podspec үүнийг автоматаар холбодог боловч асуудал гарвал Xcode-ийн "Link Binary With Libraries" хэсэгт гараар нэмээрэй.
Хэрэглээ
Hamuga ID-р нэвтрэх урсгалыг эхлүүлэх хоёр арга бий:
1. HamugaAuthButton
Нэвтрэх цонхыг нээх товчлуур бүхий компонент.
import { HamugaAuthButton } from 'hamuga-id-sdk';
// ...
<HamugaAuthButton
baseUrl="https://your-auth-server.com"
client_id="your-client-id"
redirect_uri="your-app://callback"
scope={['openid', 'email']}
/>;2. hamugaIdOpen
Нэвтрэх цонхыг програмын аргаар нээх функц.
import { hamugaIdOpen } from 'hamuga-id-sdk';
// ...
const handleLogin = () => {
hamugaIdOpen({
baseUrl: 'https://your-auth-server.com',
client_id: 'your-client-id',
redirect_uri: 'your-app://callback',
scope: ['openid', 'email'],
autoClose: true, // Сонголтоор: Redirect таарвал хөтөчийг автоматаар хаана
});
};Туслах функцууд
buildAuthUrl
Хөтөч нээхгүйгээр нэвтрэх URL-ийг үүсгэнэ.
import { buildAuthUrl } from 'hamuga-id-sdk';
const url = buildAuthUrl({
baseUrl: '...',
client_id: '...',
redirect_uri: '...',
scope: ['openid'],
});getAuthCodeFromUrl
Redirect URL-аас code параметрийг салгаж авна.
import { getAuthCodeFromUrl } from 'hamuga-id-sdk';
const code = getAuthCodeFromUrl(url);Пропсууд
AuthProps
HamugaAuthButton, hamugaIdOpen, болон buildAuthUrl-д боломжтой.
| Проп | Төрөл | Өгөгдмөл | Тайлбар |
| --------------- | --------------------------------------------- | ------------ | ------------------------------------------- |
| baseUrl | string | | Authentication серверийн хаяг. |
| client_id | string | | Таны client ID. |
| redirect_uri | string | | Амжилттай нэвтэрсний дараа буцах хаяг. |
| scope | ('openid' \| 'email' \| 'phone' \| 'dan')[] | ['openid'] | Хүсч буй эрхүүд. |
| state | string | '123' | CSRF-ээс сэргийлэх санамсаргүй текст. |
| response_type | string | 'code' | Хариуны төрөл. |
| username | 'phone' \| 'email' | | Урьдчилан бөглөх хэрэглэгчийн нэрний төрөл. |
| autoClose | boolean | true | Redirect таарвал хөтөчийг автоматаар хаах. |
ButtonProps
Зөвхөн HamugaAuthButton-д боломжтой.
| Проп | Төрөл | Өгөгдмөл | Тайлбар |
| ----------- | ----------- | ----------------------- | ------------------------------- |
| title | string | 'Хамуга ID-р нэвтрэх' | Товчлуур дээрх текст. |
| style | ViewStyle | | Товчлуурын савны загвар. |
| textStyle | TextStyle | | Товчлуур дээрх текстийн загвар. |
Contributing
See the contributing guide to learn how to contribute.
License
MIT
Made with create-react-native-library
