fayda-auth
Version:
A library for interacting with the Fayda authentication service.
100 lines (75 loc) • 2.38 kB
Markdown
A TypeScript library for interacting with the Fayda Authentication API.
```bash
npm i fayda-auth
```
```typescript
import { FaydaAuth } from 'fayda-auth';
const auth = new FaydaAuth({
apiKey: 'YOUR_API_KEY',
});
async function main() {
try {
// 1. Initiate OTP
const fcn = '1234567890123456'; // Replace with a valid Fayda Card Number
const initiateResponse = await auth.initiateOTP(fcn);
console.log('OTP Initiated:', initiateResponse);
// In a real application, you would prompt the user to enter the OTP
// For this example, we'll use a placeholder OTP
const otp = '123456'; // Replace with the actual OTP from the user
const { transactionId } = initiateResponse;
// 2. Verify OTP
const verifyResponse = await auth.verifyOTP(transactionId, otp, fcn);
console.log('OTP Verified:', verifyResponse);
console.log('User Data:', verifyResponse.user);
} catch (error) {
console.error('An error occurred:', error.message);
}
}
main();
```
Creates a new `FaydaAuth` client.
- `options`: `FaydaAuthOptions`
- `apiKey`: Your API key.
- `baseUrl` (optional): The base URL of the Fayda Auth API. Defaults to `https://fayda-auth.vercel.app/api/fayda`.
Initiates an OTP request for a given Fayda Card Number (FCN).
- `fcn`: `string` - The 16-digit FCN.
- Returns: `Promise<InitiateOTPResponse>`
### `verifyOTP(transactionId, otp, fcn)`
Verifies the OTP.
- `transactionId`: `string` - The transaction ID from the `initiateOTP` response.
- `otp`: `string` - The 6-digit OTP.
- `fcn`: `string` - The 16-digit FCN.
- Returns: `Promise<VerifyOTPResponse>`
### `VerifyOTPResponse`
{
"success": true,
"transactionId": "cd8e1472-57f4-49a2-ace9-6ec24ad3ac52",
"user": {
"uin": "8957253029940",
"fullName": [
{ "language": "eng", "value": "John Doe" },
{ "language": "amh", "value": "ጆን ዶ" }
],
"dateOfBirth": "1990/01/01",
"gender": [
{ "language": "eng", "value": "Male" },
{ "language": "amh", "value": "ወንድ" }
],
"phone": "0912345678",
"region": "Addis Ababa",
// ... other fields
},
"photo": "base64-encoded-image-data",
"qrCode": "base64-encoded-qr-data"
}
To build the library:
```bash
npm run build
```