@rajeev02/auth 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +162 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # @rajeev02/auth
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@rajeev02/auth.svg)](https://www.npmjs.com/package/@rajeev02/auth)
4
+ [![license](https://img.shields.io/npm/l/@rajeev02/auth.svg)](https://github.com/Rajeev02/rajeev-sdk/blob/main/LICENSE)
5
+
6
+ **Universal Auth & Identity SDK** with phone OTP, social sign-in (Google/Apple/Facebook), Aadhaar eKYC, biometric authentication, and JWT session management.
7
+
8
+ Part of [Rajeev SDK](https://github.com/Rajeev02/rajeev-sdk) — cross-platform infrastructure libraries for building apps that work everywhere.
9
+
10
+ ## Why use this?
11
+
12
+ - **India-first auth flows** — Phone OTP (SMS/WhatsApp), Aadhaar eKYC with DigiLocker, UPI handle verification
13
+ - **Social sign-in** — Google, Apple, Facebook with unified callback API
14
+ - **Biometric auth** — Face ID, Touch ID, Android fingerprint — with graceful fallback
15
+ - **Session management** — JWT access/refresh tokens, auto-refresh, persistence, expiry tracking
16
+ - **MFA support** — TOTP (Google Authenticator), SMS OTP, biometric as second factor
17
+ - **Pure TypeScript** — No native dependencies. Plug in your own backend endpoints.
18
+
19
+ ## Platform Support
20
+
21
+ | Platform | Engine | Status |
22
+ | ---------- | ---------- | ------ |
23
+ | iOS 16+ | TypeScript | ✅ |
24
+ | Android 7+ | TypeScript | ✅ |
25
+ | Web | TypeScript | ✅ |
26
+ | watchOS 9+ | TypeScript | ✅ |
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ npm install @rajeev02/auth
32
+ ```
33
+
34
+ ### Peer Dependencies
35
+
36
+ - `react` >= 18.3.0
37
+ - `react-native` >= 0.84.0 _(optional)_
38
+
39
+ ## Quick Start
40
+
41
+ ### Session Management
42
+
43
+ ```typescript
44
+ import { SessionManager } from "@rajeev02/auth";
45
+
46
+ const session = new SessionManager({
47
+ onRefreshToken: async (refreshToken) => {
48
+ const res = await fetch("/api/refresh", {
49
+ method: "POST",
50
+ body: JSON.stringify({ refreshToken }),
51
+ });
52
+ return res.json(); // { accessToken, refreshToken, accessExpiresAt, refreshExpiresAt }
53
+ },
54
+ onSessionExpired: () => navigation.navigate("Login"),
55
+ onPersistTokens: async (tokens) => {
56
+ await SecureStore.setItemAsync("tokens", JSON.stringify(tokens));
57
+ },
58
+ onLoadTokens: async () => {
59
+ const raw = await SecureStore.getItemAsync("tokens");
60
+ return raw ? JSON.parse(raw) : null;
61
+ },
62
+ });
63
+
64
+ // Initialize (loads persisted tokens, auto-refreshes if needed)
65
+ const state = await session.initialize(); // → 'authenticated' | 'unauthenticated'
66
+
67
+ // Get access token for API calls (auto-refreshes if expired)
68
+ const token = await session.getAccessToken();
69
+ ```
70
+
71
+ ### Phone OTP Login
72
+
73
+ ```typescript
74
+ import { OtpManager } from "@rajeev02/auth";
75
+
76
+ const otp = new OtpManager({
77
+ onSendOtp: async (phone, method) => {
78
+ const res = await fetch("/api/otp/send", {
79
+ method: "POST",
80
+ body: JSON.stringify({ phone, method }),
81
+ });
82
+ return res.json(); // { requestId, expiresInSeconds }
83
+ },
84
+ onVerifyOtp: async (requestId, code) => {
85
+ const res = await fetch("/api/otp/verify", {
86
+ method: "POST",
87
+ body: JSON.stringify({ requestId, otp: code }),
88
+ });
89
+ return res.json(); // { success, tokens? }
90
+ },
91
+ resendCooldownSeconds: 30,
92
+ maxAttempts: 5,
93
+ });
94
+
95
+ // Send OTP
96
+ await otp.sendOtp("+919876543210", "sms");
97
+
98
+ // Verify (user enters code)
99
+ const result = await otp.verifyOtp("483921");
100
+ if (result.success) {
101
+ await session.setTokens(result.tokens);
102
+ }
103
+ ```
104
+
105
+ ### Social Sign-In
106
+
107
+ ```typescript
108
+ import { AuthProviderRegistry } from "@rajeev02/auth";
109
+
110
+ const providers = new AuthProviderRegistry();
111
+
112
+ providers.register({
113
+ id: "google",
114
+ name: "Google",
115
+ clientId: "YOUR_GOOGLE_CLIENT_ID",
116
+ scopes: ["profile", "email"],
117
+ redirectUri: "myapp://auth/google/callback",
118
+ });
119
+
120
+ const url = providers.getAuthorizationUrl("google");
121
+ // → redirect user to this URL
122
+ // → handle callback with providers.handleCallback("google", callbackUrl)
123
+ ```
124
+
125
+ ## API Reference
126
+
127
+ ### `SessionManager`
128
+
129
+ | Method | Returns | Description |
130
+ | ------------------- | -------------------- | ----------------------------------------------- |
131
+ | `initialize()` | `Promise<AuthState>` | Load tokens, auto-refresh, determine auth state |
132
+ | `setTokens(tokens)` | `Promise<void>` | Store new token pair |
133
+ | `getAccessToken()` | `Promise<string>` | Get valid access token (auto-refreshes) |
134
+ | `logout()` | `Promise<void>` | Clear all tokens |
135
+ | `getState()` | `AuthState` | Current auth state |
136
+ | `isTokenExpired()` | `boolean` | Check if access token is expired |
137
+
138
+ ### `OtpManager`
139
+
140
+ | Method | Returns | Description |
141
+ | ------------------------------ | ----------------------- | ----------------------------------- |
142
+ | `sendOtp(destination, method)` | `Promise<OtpResponse>` | Send OTP via SMS/WhatsApp |
143
+ | `verifyOtp(code)` | `Promise<VerifyResult>` | Verify entered OTP |
144
+ | `canResend()` | `boolean` | Check if resend cooldown has passed |
145
+ | `getRemainingCooldown()` | `number` | Seconds until resend is allowed |
146
+
147
+ ### `AuthProviderRegistry`
148
+
149
+ | Method | Returns | Description |
150
+ | --------------------------------- | --------------------- | --------------------------- |
151
+ | `register(config)` | `void` | Register an OAuth provider |
152
+ | `getAuthorizationUrl(providerId)` | `string` | Get OAuth authorization URL |
153
+ | `handleCallback(providerId, url)` | `Promise<AuthResult>` | Process OAuth callback |
154
+ | `getProviders()` | `AuthProvider[]` | List registered providers |
155
+
156
+ ## Full Documentation
157
+
158
+ 📖 [Complete API docs with Aadhaar eKYC, biometric, and MFA examples](https://github.com/Rajeev02/rajeev-sdk/blob/main/docs/usage/AUTH.md)
159
+
160
+ ## License
161
+
162
+ MIT © 2026 [Rajeev Kumar Joshi](https://rajeev02.github.io)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rajeev02/auth",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Universal Auth & Identity SDK — OTP, biometric, Aadhaar/DigiLocker, session management, social login",
5
5
  "main": "lib/index.js",
6
6
  "author": "Rajeev Kumar Joshi <rajeevjoshi91@gmail.com> (https://rajeev02.github.io)",