rn-swiftauth-sdk 1.0.1 โ 1.0.3
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.
- package/README.md +229 -370
- package/dist/components/LoginForm.js +19 -11
- package/dist/components/SignUpForm.js +17 -16
- package/dist/core/AuthProvider.js +56 -110
- package/dist/errors/errorMapper.d.ts +10 -2
- package/dist/errors/errorMapper.js +61 -99
- package/dist/errors/exceptions.d.ts +82 -0
- package/dist/errors/exceptions.js +134 -0
- package/dist/errors/index.d.ts +2 -1
- package/dist/errors/index.js +19 -15
- package/dist/types/auth.types.d.ts +12 -4
- package/dist/types/auth.types.js +0 -1
- package/dist/types/error.types.d.ts +19 -9
- package/dist/types/error.types.js +35 -13
- package/package.json +1 -1
- package/src/components/LoginForm.tsx +21 -12
- package/src/components/SignUpForm.tsx +20 -31
- package/src/core/AuthProvider.tsx +84 -146
- package/src/errors/errorMapper.ts +97 -105
- package/src/errors/exceptions.ts +174 -0
- package/src/errors/index.ts +22 -1
- package/src/types/auth.types.ts +20 -17
- package/src/types/error.types.ts +42 -15
package/README.md
CHANGED
|
@@ -5,219 +5,127 @@ A production-ready React Native authentication SDK powered by Firebase. Built wi
|
|
|
5
5
|
[](https://www.npmjs.com/package/rn-swiftauth-sdk)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## Features
|
|
9
9
|
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
10
|
+
- **Email/Password Authentication** - Built-in validation and error handling
|
|
11
|
+
- **Pre-built UI Components** - Beautiful, customizable auth screens out of the box
|
|
12
|
+
- **Headless Hooks** - Full control with `useAuth()` for custom implementations
|
|
13
|
+
- **Social Login Support** - Google and Apple Sign-In (iOS only for Apple)
|
|
14
|
+
- **Persistent Sessions** - Configurable session management (local/memory)
|
|
15
|
+
- **TypeScript First** - Full type safety and IntelliSense support
|
|
16
|
+
- **Zero Configuration** - Works with minimal setup
|
|
17
|
+
- **Expo & Bare React Native** - Compatible with both workflows
|
|
18
18
|
|
|
19
19
|
---
|
|
20
20
|
|
|
21
|
-
##
|
|
21
|
+
## Installation
|
|
22
22
|
|
|
23
23
|
### Prerequisites
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
- **Expo**: SDK 49+ (Recommended)
|
|
25
|
+
- Node.js v18 or higher
|
|
26
|
+
- React Native v0.70+
|
|
27
|
+
- Expo SDK 49+ (Recommended)
|
|
29
28
|
|
|
30
|
-
###
|
|
31
|
-
|
|
32
|
-
Install SwiftAuth SDK from npm:
|
|
29
|
+
### Install the SDK
|
|
33
30
|
|
|
34
31
|
```bash
|
|
35
32
|
npm install rn-swiftauth-sdk
|
|
36
33
|
```
|
|
37
34
|
|
|
38
|
-
|
|
35
|
+
For Expo projects:
|
|
39
36
|
```bash
|
|
40
37
|
npx expo install rn-swiftauth-sdk
|
|
41
38
|
```
|
|
42
39
|
|
|
43
|
-
###
|
|
44
|
-
|
|
45
|
-
SwiftAuth requires these peer dependencies:
|
|
40
|
+
### Install Required Dependencies
|
|
46
41
|
|
|
47
42
|
```bash
|
|
48
43
|
npm install firebase @react-native-async-storage/async-storage react-native-safe-area-context
|
|
49
44
|
```
|
|
50
45
|
|
|
51
|
-
|
|
46
|
+
For Expo:
|
|
52
47
|
```bash
|
|
53
48
|
npx expo install firebase @react-native-async-storage/async-storage react-native-safe-area-context
|
|
54
49
|
```
|
|
55
50
|
|
|
56
|
-
|
|
51
|
+
### Optional: Social Login Dependencies
|
|
52
|
+
|
|
53
|
+
**Google Sign-In:**
|
|
57
54
|
```bash
|
|
58
55
|
npm install @react-native-google-signin/google-signin
|
|
59
|
-
# or for Expo
|
|
60
|
-
npx expo install @react-native-google-signin/google-signin
|
|
61
56
|
```
|
|
62
57
|
|
|
63
|
-
**
|
|
58
|
+
**Apple Sign-In (iOS only):**
|
|
64
59
|
```bash
|
|
65
60
|
npm install @invertase/react-native-apple-authentication
|
|
66
|
-
# or for Expo
|
|
67
|
-
npx expo install @invertase/react-native-apple-authentication
|
|
68
61
|
```
|
|
69
62
|
|
|
70
|
-
###
|
|
71
|
-
|
|
72
|
-
If you're using Bare React Native (not Expo), install iOS CocoaPods:
|
|
63
|
+
### Bare React Native Only
|
|
73
64
|
|
|
65
|
+
Install iOS CocoaPods:
|
|
74
66
|
```bash
|
|
75
|
-
cd ios
|
|
76
|
-
pod install
|
|
77
|
-
cd ..
|
|
67
|
+
cd ios && pod install && cd ..
|
|
78
68
|
```
|
|
79
69
|
|
|
80
|
-
That's it! You're ready to start using SwiftAuth SDK.
|
|
81
|
-
|
|
82
70
|
---
|
|
83
71
|
|
|
84
|
-
##
|
|
72
|
+
## Firebase Setup
|
|
85
73
|
|
|
86
74
|
### 1. Create Firebase Project
|
|
87
75
|
|
|
88
|
-
1. Go to [Firebase Console](https://console.firebase.google.com
|
|
76
|
+
1. Go to [Firebase Console](https://console.firebase.google.com)
|
|
89
77
|
2. Create a new project or select existing one
|
|
90
|
-
3. Register a
|
|
78
|
+
3. Register a Web App (click the `</>` icon)
|
|
91
79
|
4. Copy the `firebaseConfig` object
|
|
92
80
|
|
|
93
81
|
### 2. Enable Authentication Methods
|
|
94
82
|
|
|
95
|
-
1.
|
|
83
|
+
1. Navigate to **Authentication > Sign-in method**
|
|
96
84
|
2. Enable **Email/Password**
|
|
97
85
|
3. (Optional) Enable **Google** and/or **Apple** for social login
|
|
98
86
|
|
|
99
87
|
### 3. Platform-Specific Configuration
|
|
100
88
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
**Update `app.json` for Expo:**
|
|
125
|
-
```json
|
|
126
|
-
{
|
|
127
|
-
"expo": {
|
|
128
|
-
"android": {
|
|
129
|
-
"googleServicesFile": "./google-services.json",
|
|
130
|
-
"package": "com.yourcompany.yourapp"
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
#### iOS Setup
|
|
137
|
-
|
|
138
|
-
1. In Firebase Console, register an **iOS app**
|
|
139
|
-
2. Download `GoogleService-Info.plist`
|
|
140
|
-
3. Place it in your project root for Expo, or `ios/YourAppName/GoogleService-Info.plist` for Bare React Native
|
|
141
|
-
|
|
142
|
-
**For Bare React Native:**
|
|
143
|
-
- Drag `GoogleService-Info.plist` into your Xcode project
|
|
144
|
-
|
|
145
|
-
**Update `app.json` for Expo:**
|
|
146
|
-
```json
|
|
147
|
-
{
|
|
148
|
-
"expo": {
|
|
149
|
-
"ios": {
|
|
150
|
-
"googleServicesFile": "./GoogleService-Info.plist",
|
|
151
|
-
"bundleIdentifier": "com.yourcompany.yourapp"
|
|
89
|
+
**Android:**
|
|
90
|
+
- Register an Android app in Firebase Console
|
|
91
|
+
- Download `google-services.json` and place in `android/app/`
|
|
92
|
+
- Add to `android/build.gradle`:
|
|
93
|
+
```gradle
|
|
94
|
+
classpath 'com.google.gms:google-services:4.3.15'
|
|
95
|
+
```
|
|
96
|
+
- Add to `android/app/build.gradle`:
|
|
97
|
+
```gradle
|
|
98
|
+
apply plugin: 'com.google.gms.google-services'
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**iOS:**
|
|
102
|
+
- Register an iOS app in Firebase Console
|
|
103
|
+
- Download `GoogleService-Info.plist` and place in project root (Expo) or `ios/YourAppName/` (Bare)
|
|
104
|
+
- Update `app.json`:
|
|
105
|
+
```json
|
|
106
|
+
{
|
|
107
|
+
"expo": {
|
|
108
|
+
"ios": {
|
|
109
|
+
"googleServicesFile": "./GoogleService-Info.plist",
|
|
110
|
+
"bundleIdentifier": "com.yourcompany.yourapp"
|
|
111
|
+
}
|
|
152
112
|
}
|
|
153
113
|
}
|
|
154
|
-
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
**Install CocoaPods (Bare React Native iOS only):**
|
|
158
|
-
```bash
|
|
159
|
-
cd ios
|
|
160
|
-
pod install
|
|
161
|
-
cd ..
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### 4. Google Sign-In Configuration (Optional)
|
|
165
|
-
|
|
166
|
-
#### Get Google Client IDs
|
|
167
|
-
|
|
168
|
-
1. Go to [Google Cloud Console](https://console.cloud.google.com/)
|
|
169
|
-
2. Select your Firebase project
|
|
170
|
-
3. Go to **APIs & Services** > **Credentials**
|
|
171
|
-
4. You'll need:
|
|
172
|
-
- **Web Client ID** (for all platforms)
|
|
173
|
-
- **iOS Client ID** (optional, iOS-specific)
|
|
174
|
-
|
|
175
|
-
**Update `app.json` for Google Sign-In:**
|
|
176
|
-
```json
|
|
177
|
-
{
|
|
178
|
-
"expo": {
|
|
179
|
-
"ios": {
|
|
180
|
-
"bundleIdentifier": "com.yourcompany.yourapp",
|
|
181
|
-
"googleServicesFile": "./GoogleService-Info.plist"
|
|
182
|
-
},
|
|
183
|
-
"android": {
|
|
184
|
-
"package": "com.yourcompany.yourapp",
|
|
185
|
-
"googleServicesFile": "./google-services.json"
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
### 5. Apple Sign-In Configuration (Optional, iOS only)
|
|
114
|
+
```
|
|
192
115
|
|
|
193
|
-
|
|
194
|
-
2. Enable **Sign in with Apple** capability for your App ID
|
|
195
|
-
3. For Bare React Native: Add the capability in Xcode: **Signing & Capabilities** > **+ Capability** > **Sign in with Apple**
|
|
196
|
-
|
|
197
|
-
**Update `app.json` for Expo:**
|
|
198
|
-
```json
|
|
199
|
-
{
|
|
200
|
-
"expo": {
|
|
201
|
-
"ios": {
|
|
202
|
-
"usesAppleSignIn": true
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
```
|
|
116
|
+
For detailed Firebase setup instructions, see the [Firebase documentation](https://firebase.google.com/docs/ios/setup).
|
|
207
117
|
|
|
208
118
|
---
|
|
209
119
|
|
|
210
|
-
##
|
|
120
|
+
## Quick Start
|
|
211
121
|
|
|
212
|
-
### Basic
|
|
122
|
+
### Basic Email Authentication
|
|
213
123
|
|
|
214
|
-
```
|
|
215
|
-
// App.tsx
|
|
124
|
+
```typescript
|
|
216
125
|
import React from 'react';
|
|
217
126
|
import { SafeAreaProvider } from 'react-native-safe-area-context';
|
|
218
127
|
import { AuthProvider, AuthScreen, useAuth } from 'rn-swiftauth-sdk';
|
|
219
128
|
|
|
220
|
-
// Your Firebase configuration
|
|
221
129
|
const firebaseConfig = {
|
|
222
130
|
apiKey: "AIzaSyD-Your-Actual-Key",
|
|
223
131
|
authDomain: "your-project.firebaseapp.com",
|
|
@@ -229,12 +137,7 @@ const firebaseConfig = {
|
|
|
229
137
|
|
|
230
138
|
const MainNavigation = () => {
|
|
231
139
|
const { user } = useAuth();
|
|
232
|
-
|
|
233
|
-
if (!user) {
|
|
234
|
-
return <AuthScreen />;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return <HomeScreen user={user} />;
|
|
140
|
+
return user ? <HomeScreen user={user} /> : <AuthScreen />;
|
|
238
141
|
};
|
|
239
142
|
|
|
240
143
|
export default function App() {
|
|
@@ -250,11 +153,14 @@ export default function App() {
|
|
|
250
153
|
|
|
251
154
|
### With Social Login
|
|
252
155
|
|
|
253
|
-
```
|
|
156
|
+
```typescript
|
|
254
157
|
const firebaseConfig = {
|
|
255
158
|
apiKey: "AIzaSyD-Your-Actual-Key",
|
|
256
159
|
authDomain: "your-project.firebaseapp.com",
|
|
257
160
|
projectId: "your-project-id",
|
|
161
|
+
storageBucket: "your-project.appspot.com",
|
|
162
|
+
messagingSenderId: "123456789",
|
|
163
|
+
appId: "1:123456789:web:abcdef",
|
|
258
164
|
|
|
259
165
|
// Enable social login
|
|
260
166
|
enableGoogle: true,
|
|
@@ -267,44 +173,16 @@ const firebaseConfig = {
|
|
|
267
173
|
|
|
268
174
|
---
|
|
269
175
|
|
|
270
|
-
##
|
|
176
|
+
## API Reference
|
|
271
177
|
|
|
272
178
|
### `<AuthProvider>`
|
|
273
179
|
|
|
274
180
|
Wraps your app and provides authentication context.
|
|
275
181
|
|
|
276
|
-
**Props:**
|
|
277
|
-
|
|
278
182
|
| Prop | Type | Required | Description |
|
|
279
183
|
|------|------|----------|-------------|
|
|
280
|
-
|
|
|
281
|
-
|
|
|
282
|
-
|
|
283
|
-
**AuthConfig Interface:**
|
|
284
|
-
|
|
285
|
-
```typescript
|
|
286
|
-
interface AuthConfig {
|
|
287
|
-
// Firebase credentials (Required)
|
|
288
|
-
apiKey: string;
|
|
289
|
-
authDomain: string;
|
|
290
|
-
projectId: string;
|
|
291
|
-
storageBucket?: string;
|
|
292
|
-
messagingSenderId?: string;
|
|
293
|
-
appId?: string;
|
|
294
|
-
|
|
295
|
-
// Session persistence
|
|
296
|
-
persistence?: 'local' | 'memory'; // Default: 'local'
|
|
297
|
-
|
|
298
|
-
// Feature flags
|
|
299
|
-
enableEmail?: boolean; // Default: true
|
|
300
|
-
enableGoogle?: boolean; // Default: false
|
|
301
|
-
enableApple?: boolean; // Default: false (iOS only)
|
|
302
|
-
|
|
303
|
-
// Google Sign-In
|
|
304
|
-
googleWebClientId?: string;
|
|
305
|
-
googleIosClientId?: string;
|
|
306
|
-
}
|
|
307
|
-
```
|
|
184
|
+
| config | AuthConfig | Yes | Firebase configuration object |
|
|
185
|
+
| children | ReactNode | Yes | Your app components |
|
|
308
186
|
|
|
309
187
|
### `useAuth()` Hook
|
|
310
188
|
|
|
@@ -312,63 +190,33 @@ Access authentication state and methods.
|
|
|
312
190
|
|
|
313
191
|
```typescript
|
|
314
192
|
const {
|
|
315
|
-
user,
|
|
316
|
-
status,
|
|
317
|
-
error,
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
193
|
+
user, // Current user object or null
|
|
194
|
+
status, // AuthStatus enum
|
|
195
|
+
error, // Last error object or null
|
|
196
|
+
isLoading, // Boolean loading state
|
|
197
|
+
signInWithEmail, // Sign in method
|
|
198
|
+
signUpWithEmail, // Sign up method
|
|
199
|
+
signOut, // Sign out method
|
|
200
|
+
clearError // Clear error state
|
|
322
201
|
} = useAuth();
|
|
323
202
|
```
|
|
324
203
|
|
|
325
|
-
|
|
204
|
+
#### Available Methods
|
|
326
205
|
|
|
327
206
|
| Method | Signature | Description |
|
|
328
207
|
|--------|-----------|-------------|
|
|
329
|
-
| `signInWithEmail` | `(
|
|
330
|
-
| `signUpWithEmail` | `(
|
|
208
|
+
| `signInWithEmail` | `(options: {email, password}) => Promise<void>` | Sign in existing user |
|
|
209
|
+
| `signUpWithEmail` | `(options: {email, password}) => Promise<void>` | Create new account |
|
|
331
210
|
| `signOut` | `() => Promise<void>` | Log out current user |
|
|
332
211
|
| `clearError` | `() => void` | Clear error state |
|
|
333
212
|
|
|
334
|
-
### `<AuthScreen>` Component
|
|
335
|
-
|
|
336
|
-
Pre-built authentication UI with login and signup.
|
|
337
|
-
|
|
338
|
-
**Props:**
|
|
339
|
-
|
|
340
|
-
| Prop | Type | Default | Description |
|
|
341
|
-
|------|------|---------|-------------|
|
|
342
|
-
| `styles` | `AuthScreenStyles` | `undefined` | Custom styles object |
|
|
343
|
-
| `titles` | `object` | `undefined` | Custom text labels |
|
|
344
|
-
| `showPasswordHints` | `boolean` | `true` | Show password requirements |
|
|
345
|
-
|
|
346
|
-
**Example:**
|
|
347
|
-
|
|
348
|
-
```tsx
|
|
349
|
-
<AuthScreen
|
|
350
|
-
titles={{
|
|
351
|
-
loginTitle: "Welcome Back",
|
|
352
|
-
loginSubtitle: "Sign in to continue",
|
|
353
|
-
signupTitle: "Create Account",
|
|
354
|
-
signupSubtitle: "Join us today"
|
|
355
|
-
}}
|
|
356
|
-
styles={{
|
|
357
|
-
container: { backgroundColor: '#f5f5f5' },
|
|
358
|
-
button: { backgroundColor: '#007AFF' },
|
|
359
|
-
buttonText: { color: '#ffffff' }
|
|
360
|
-
}}
|
|
361
|
-
showPasswordHints={true}
|
|
362
|
-
/>
|
|
363
|
-
```
|
|
364
|
-
|
|
365
213
|
---
|
|
366
214
|
|
|
367
|
-
##
|
|
215
|
+
## Customization
|
|
368
216
|
|
|
369
|
-
### Dark Mode Theme
|
|
217
|
+
### Dark Mode Theme Example
|
|
370
218
|
|
|
371
|
-
```
|
|
219
|
+
```typescript
|
|
372
220
|
<AuthScreen
|
|
373
221
|
styles={{
|
|
374
222
|
container: { backgroundColor: '#1a1a1a' },
|
|
@@ -385,100 +233,169 @@ Pre-built authentication UI with login and signup.
|
|
|
385
233
|
/>
|
|
386
234
|
```
|
|
387
235
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
Build your own interface using the `useAuth()` hook:
|
|
236
|
+
---
|
|
391
237
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
238
|
+
## Error Handling
|
|
239
|
+
|
|
240
|
+
SwiftAuth provides a comprehensive error handling system with custom exceptions that map Firebase errors to user-friendly messages.
|
|
241
|
+
|
|
242
|
+
### Custom Exception Classes
|
|
243
|
+
|
|
244
|
+
All errors extend the base `AuthException` class and include:
|
|
245
|
+
- `code`: Machine-readable error code
|
|
246
|
+
- `message`: User-friendly error message
|
|
247
|
+
- `timestamp`: When the error occurred
|
|
248
|
+
- `originalError`: The underlying Firebase error (optional)
|
|
249
|
+
- `toJSON()`: Serialize for logging/debugging
|
|
250
|
+
|
|
251
|
+
### Supported Exceptions
|
|
252
|
+
|
|
253
|
+
| Exception Class | Error Code | User Message |
|
|
254
|
+
|-----------------|------------|--------------|
|
|
255
|
+
| `InvalidCredentialsException` | `auth/invalid-credentials` | "Invalid email or password. Please check your credentials and try again." |
|
|
256
|
+
| `UserNotFoundException` | `auth/user-not-found` | "No account found with this email. Please sign up first." |
|
|
257
|
+
| `EmailAlreadyInUseException` | `auth/email-already-in-use` | "This email is already registered. Please sign in or use a different email." |
|
|
258
|
+
| `WeakPasswordException` | `auth/weak-password` | "Password is too weak. Please use at least 6 characters with a mix of letters and numbers." |
|
|
259
|
+
| `TokenExpiredException` | `auth/token-expired` | "Your session has expired. Please sign in again." |
|
|
260
|
+
| `NetworkException` | `auth/network-error` | "Network error. Please check your internet connection and try again." |
|
|
261
|
+
| `GoogleSignInCancelledException` | `auth/google-sign-in-cancelled` | "Google Sign-In was cancelled." |
|
|
262
|
+
| `AppleSignInCancelledException` | `auth/apple-sign-in-cancelled` | "Apple Sign-In was cancelled." |
|
|
263
|
+
| `AppleSignInNotSupportedException` | `auth/apple-sign-in-not-supported` | "Apple Sign-In is only available on iOS 13+ devices." |
|
|
264
|
+
| `GooglePlayServicesUnavailableException` | `auth/google-play-services-unavailable` | "Google Play Services are not available. Please update Google Play Services." |
|
|
265
|
+
| `ConfigurationException` | `auth/configuration-error` | Custom message based on configuration issue |
|
|
266
|
+
| `UnknownAuthException` | `auth/unknown` | "An unexpected error occurred." |
|
|
267
|
+
|
|
268
|
+
### Basic Error Display
|
|
269
|
+
```typescript
|
|
395
270
|
import { useAuth } from 'rn-swiftauth-sdk';
|
|
396
271
|
|
|
397
|
-
|
|
398
|
-
const {
|
|
399
|
-
const [email, setEmail] = useState('');
|
|
400
|
-
const [password, setPassword] = useState('');
|
|
401
|
-
|
|
402
|
-
const handleLogin = async () => {
|
|
403
|
-
try {
|
|
404
|
-
await signInWithEmail(email, password);
|
|
405
|
-
} catch (err) {
|
|
406
|
-
console.error('Login failed:', err);
|
|
407
|
-
}
|
|
408
|
-
};
|
|
272
|
+
const LoginScreen = () => {
|
|
273
|
+
const { error, clearError } = useAuth();
|
|
409
274
|
|
|
410
275
|
return (
|
|
411
|
-
<View
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
value={email}
|
|
419
|
-
onChangeText={setEmail}
|
|
420
|
-
style={{ borderWidth: 1, padding: 10, marginBottom: 10 }}
|
|
421
|
-
/>
|
|
422
|
-
|
|
423
|
-
<TextInput
|
|
424
|
-
placeholder="Password"
|
|
425
|
-
value={password}
|
|
426
|
-
onChangeText={setPassword}
|
|
427
|
-
secureTextEntry
|
|
428
|
-
style={{ borderWidth: 1, padding: 10, marginBottom: 20 }}
|
|
429
|
-
/>
|
|
430
|
-
|
|
431
|
-
<Button title="Sign In" onPress={handleLogin} />
|
|
276
|
+
<View>
|
|
277
|
+
{error && (
|
|
278
|
+
<View style={styles.errorContainer}>
|
|
279
|
+
<Text style={styles.errorText}>{error.message}</Text>
|
|
280
|
+
<Button title="Dismiss" onPress={clearError} />
|
|
281
|
+
</View>
|
|
282
|
+
)}
|
|
432
283
|
</View>
|
|
433
284
|
);
|
|
434
285
|
};
|
|
435
286
|
```
|
|
436
287
|
|
|
437
|
-
|
|
288
|
+
### Handling Specific Exception Types
|
|
289
|
+
```typescript
|
|
290
|
+
import {
|
|
291
|
+
useAuth,
|
|
292
|
+
InvalidCredentialsException,
|
|
293
|
+
EmailAlreadyInUseException,
|
|
294
|
+
NetworkException
|
|
295
|
+
} from 'rn-swiftauth-sdk';
|
|
296
|
+
|
|
297
|
+
const SignUpScreen = () => {
|
|
298
|
+
const { signUpWithEmail } = useAuth();
|
|
299
|
+
|
|
300
|
+
const handleSignUp = async () => {
|
|
301
|
+
try {
|
|
302
|
+
await signUpWithEmail({ email, password });
|
|
303
|
+
} catch (error) {
|
|
304
|
+
if (error instanceof EmailAlreadyInUseException) {
|
|
305
|
+
Alert.alert(
|
|
306
|
+
"Account Exists",
|
|
307
|
+
"Would you like to sign in instead?",
|
|
308
|
+
[
|
|
309
|
+
{ text: "Cancel", style: "cancel" },
|
|
310
|
+
{ text: "Sign In", onPress: () => navigation.navigate('SignIn') }
|
|
311
|
+
]
|
|
312
|
+
);
|
|
313
|
+
} else if (error instanceof NetworkException) {
|
|
314
|
+
Alert.alert("Connection Issue", "Please check your internet and try again.");
|
|
315
|
+
} else if (error instanceof InvalidCredentialsException) {
|
|
316
|
+
Alert.alert("Invalid Input", "Please check your email and password.");
|
|
317
|
+
} else {
|
|
318
|
+
Alert.alert("Error", error.message);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
};
|
|
438
322
|
|
|
439
|
-
|
|
323
|
+
return <Button title="Sign Up" onPress={handleSignUp} />;
|
|
324
|
+
};
|
|
325
|
+
```
|
|
440
326
|
|
|
441
|
-
|
|
327
|
+
### Accessing Original Firebase Errors
|
|
442
328
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
| `auth/user-not-found` | Account doesn't exist | "Invalid email or password." |
|
|
447
|
-
| `auth/email-already-in-use` | Email already registered | "This email is already registered." |
|
|
448
|
-
| `auth/weak-password` | Password too weak | "Password is too weak." |
|
|
449
|
-
| `auth/network-request-failed` | No internet connection | "Network error. Please check your connection." |
|
|
450
|
-
| `auth/invalid-email` | Invalid email format | "Invalid email address." |
|
|
451
|
-
| `auth/configuration-error` | Missing API keys | "Authentication is not configured correctly." |
|
|
329
|
+
For advanced use cases, access the raw Firebase error:
|
|
330
|
+
```typescript
|
|
331
|
+
const { error } = useAuth();
|
|
452
332
|
|
|
453
|
-
|
|
333
|
+
useEffect(() => {
|
|
334
|
+
if (error?.originalError) {
|
|
335
|
+
console.log('Firebase Error Code:', error.originalError.code);
|
|
336
|
+
console.log('Firebase Error Message:', error.originalError.message);
|
|
337
|
+
|
|
338
|
+
// Custom handling for specific Firebase codes
|
|
339
|
+
if (error.originalError.code === 'auth/requires-recent-login') {
|
|
340
|
+
showReauthenticationPrompt();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}, [error]);
|
|
344
|
+
```
|
|
454
345
|
|
|
455
|
-
|
|
346
|
+
### Error Logging for Debugging
|
|
347
|
+
```typescript
|
|
456
348
|
const { error } = useAuth();
|
|
457
349
|
|
|
458
|
-
|
|
459
|
-
if (error
|
|
460
|
-
|
|
350
|
+
useEffect(() => {
|
|
351
|
+
if (error) {
|
|
352
|
+
// Log full error details (includes timestamp, code, original error)
|
|
353
|
+
console.log('Auth Error:', JSON.stringify(error.toJSON(), null, 2));
|
|
354
|
+
|
|
355
|
+
// Send to error tracking service (e.g., Sentry)
|
|
356
|
+
logErrorToService(error.toJSON());
|
|
461
357
|
}
|
|
462
|
-
|
|
463
|
-
|
|
358
|
+
}, [error]);
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### Global Error Boundary
|
|
362
|
+
```typescript
|
|
363
|
+
import { useAuth } from 'rn-swiftauth-sdk';
|
|
364
|
+
|
|
365
|
+
const ErrorBoundary = ({ children }) => {
|
|
366
|
+
const { error, clearError } = useAuth();
|
|
367
|
+
|
|
368
|
+
if (error) {
|
|
369
|
+
return (
|
|
370
|
+
<View style={styles.errorScreen}>
|
|
371
|
+
<Text style={styles.errorTitle}>Oops!</Text>
|
|
372
|
+
<Text style={styles.errorMessage}>{error.message}</Text>
|
|
373
|
+
<Text style={styles.errorCode}>Error Code: {error.code}</Text>
|
|
374
|
+
<Button title="Try Again" onPress={clearError} />
|
|
375
|
+
</View>
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return <>{children}</>;
|
|
380
|
+
};
|
|
464
381
|
```
|
|
465
382
|
|
|
466
383
|
---
|
|
467
384
|
|
|
468
|
-
##
|
|
385
|
+
## Session Management
|
|
469
386
|
|
|
470
387
|
### Keep User Logged In (Default)
|
|
471
388
|
|
|
472
|
-
```
|
|
389
|
+
```typescript
|
|
473
390
|
const config = {
|
|
474
391
|
...firebaseConfig,
|
|
475
392
|
persistence: 'local' // User stays logged in
|
|
476
393
|
};
|
|
477
394
|
```
|
|
478
395
|
|
|
479
|
-
###
|
|
396
|
+
### Memory-Only Session
|
|
480
397
|
|
|
481
|
-
```
|
|
398
|
+
```typescript
|
|
482
399
|
const config = {
|
|
483
400
|
...firebaseConfig,
|
|
484
401
|
persistence: 'memory' // User logged out when app closes
|
|
@@ -487,15 +404,11 @@ const config = {
|
|
|
487
404
|
|
|
488
405
|
---
|
|
489
406
|
|
|
490
|
-
##
|
|
491
|
-
|
|
492
|
-
The SwiftAuth SDK handles the complexity of authentication (Google, Apple, Email), but you likely have your own API for user data.
|
|
407
|
+
## Backend Integration
|
|
493
408
|
|
|
494
|
-
The SDK exposes a secure Firebase ID Token (`user.token`)
|
|
409
|
+
The SDK exposes a secure Firebase ID Token (`user.token`) for backend authentication.
|
|
495
410
|
|
|
496
|
-
###
|
|
497
|
-
|
|
498
|
-
In your React Native app, access the token from the `useAuth` hook and include it in the `Authorization` header of your API requests.
|
|
411
|
+
### Frontend: Sending the Token
|
|
499
412
|
|
|
500
413
|
```typescript
|
|
501
414
|
import { useAuth } from 'rn-swiftauth-sdk';
|
|
@@ -511,13 +424,12 @@ const UserProfile = () => {
|
|
|
511
424
|
method: 'GET',
|
|
512
425
|
headers: {
|
|
513
426
|
'Content-Type': 'application/json',
|
|
514
|
-
// Send the token as a Bearer token
|
|
515
427
|
'Authorization': `Bearer ${user.token}`
|
|
516
428
|
}
|
|
517
429
|
});
|
|
518
|
-
|
|
430
|
+
|
|
519
431
|
const data = await response.json();
|
|
520
|
-
console.log(
|
|
432
|
+
console.log(data);
|
|
521
433
|
} catch (error) {
|
|
522
434
|
console.error('Request failed:', error);
|
|
523
435
|
}
|
|
@@ -527,24 +439,12 @@ const UserProfile = () => {
|
|
|
527
439
|
};
|
|
528
440
|
```
|
|
529
441
|
|
|
530
|
-
###
|
|
531
|
-
|
|
532
|
-
Your backend must verify this token to ensure the request is legitimate. You can use the Firebase Admin SDK for this.
|
|
533
|
-
|
|
534
|
-
**Example (Node.js / Express):**
|
|
442
|
+
### Backend: Verifying the Token (Node.js)
|
|
535
443
|
|
|
536
444
|
```javascript
|
|
537
445
|
const admin = require('firebase-admin');
|
|
538
|
-
const express = require('express');
|
|
539
|
-
const app = express();
|
|
540
|
-
|
|
541
|
-
// 1. Initialize Firebase Admin with your Service Account
|
|
542
|
-
const serviceAccount = require('./path/to/serviceAccountKey.json');
|
|
543
|
-
admin.initializeApp({
|
|
544
|
-
credential: admin.credential.cert(serviceAccount)
|
|
545
|
-
});
|
|
546
446
|
|
|
547
|
-
//
|
|
447
|
+
// Middleware to verify Firebase ID token
|
|
548
448
|
const verifyToken = async (req, res, next) => {
|
|
549
449
|
const authHeader = req.headers.authorization;
|
|
550
450
|
|
|
@@ -553,39 +453,25 @@ const verifyToken = async (req, res, next) => {
|
|
|
553
453
|
}
|
|
554
454
|
|
|
555
455
|
const idToken = authHeader.split('Bearer ')[1];
|
|
556
|
-
|
|
456
|
+
|
|
557
457
|
try {
|
|
558
|
-
// Decodes the token and checks signature
|
|
559
458
|
const decodedToken = await admin.auth().verifyIdToken(idToken);
|
|
560
|
-
|
|
561
|
-
// Attach user info to request (uid, email, etc.)
|
|
562
|
-
req.user = decodedToken;
|
|
459
|
+
req.user = decodedToken;
|
|
563
460
|
next();
|
|
564
461
|
} catch (error) {
|
|
565
462
|
res.status(403).send('Invalid Token');
|
|
566
463
|
}
|
|
567
464
|
};
|
|
568
465
|
|
|
569
|
-
//
|
|
466
|
+
// Protected route example
|
|
570
467
|
app.get('/profile', verifyToken, (req, res) => {
|
|
571
|
-
|
|
572
|
-
res.json({ message: `Hello ${req.user.email}, here is your private data.` });
|
|
468
|
+
res.json({ userId: req.user.uid, email: req.user.email });
|
|
573
469
|
});
|
|
574
470
|
```
|
|
575
471
|
|
|
576
|
-
### Supported Backends
|
|
577
|
-
|
|
578
|
-
This pattern works with any backend language that has a Firebase Admin SDK, including:
|
|
579
|
-
|
|
580
|
-
- Node.js
|
|
581
|
-
- Python
|
|
582
|
-
- Go
|
|
583
|
-
- Java
|
|
584
|
-
- .NET
|
|
585
|
-
|
|
586
472
|
---
|
|
587
473
|
|
|
588
|
-
##
|
|
474
|
+
## Example App
|
|
589
475
|
|
|
590
476
|
Check out our example implementation:
|
|
591
477
|
|
|
@@ -605,47 +491,20 @@ npx expo start
|
|
|
605
491
|
|
|
606
492
|
---
|
|
607
493
|
|
|
608
|
-
##
|
|
609
|
-
|
|
610
|
-
- [Installation Guide](https://github.com/allcodez/Auth-SDK_Stage8/blob/main/docs/installation.md)
|
|
611
|
-
- [Getting Started](https://github.com/allcodez/Auth-SDK_Stage8/blob/main/docs/getting-started.md)
|
|
612
|
-
- [API Reference](https://github.com/allcodez/Auth-SDK_Stage8/blob/main/docs/api-reference.md)
|
|
613
|
-
- [Usage Examples](https://github.com/allcodez/Auth-SDK_Stage8/blob/main/docs/usage-examples.md)
|
|
614
|
-
- [Error Codes](https://github.com/allcodez/Auth-SDK_Stage8/blob/main/docs/error-codes.md)
|
|
615
|
-
|
|
616
|
-
---
|
|
617
|
-
|
|
618
|
-
## ๐ค Contributing
|
|
619
|
-
|
|
620
|
-
We welcome contributions! See [CONTRIBUTING.md](https://github.com/allcodez/Auth-SDK_Stage8/blob/main/CONTRIBUTING.md) for guidelines.
|
|
621
|
-
|
|
622
|
-
---
|
|
623
|
-
|
|
624
|
-
## ๐ License
|
|
494
|
+
## Contributing
|
|
625
495
|
|
|
626
|
-
|
|
496
|
+
We welcome contributions! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
627
497
|
|
|
628
498
|
---
|
|
629
499
|
|
|
630
|
-
##
|
|
500
|
+
## License
|
|
631
501
|
|
|
632
|
-
-
|
|
633
|
-
- ๐ Issues: [GitHub Issues](https://github.com/allcodez/Auth-SDK_Stage8/issues)
|
|
634
|
-
- ๐ฌ Discussions: [GitHub Discussions](https://github.com/allcodez/Auth-SDK_Stage8/discussions)
|
|
635
|
-
- ๐ฆ NPM Package: [rn-swiftauth-sdk](https://www.npmjs.com/package/rn-swiftauth-sdk)
|
|
502
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
636
503
|
|
|
637
504
|
---
|
|
638
505
|
|
|
639
|
-
##
|
|
640
|
-
|
|
641
|
-
- [ ] Password reset functionality
|
|
642
|
-
- [ ] Email verification
|
|
643
|
-
- [ ] Phone authentication
|
|
644
|
-
- [ ] Multi-factor authentication (MFA)
|
|
645
|
-
- [ ] Biometric authentication
|
|
646
|
-
- [ ] Session refresh tokens
|
|
647
|
-
- [x] NPM package distribution
|
|
648
|
-
|
|
649
|
-
---
|
|
506
|
+
## Support
|
|
650
507
|
|
|
651
|
-
|
|
508
|
+
- **Issues:** [GitHub Issues](https://github.com/allcodez/Auth-SDK_Stage8/issues)
|
|
509
|
+
- **NPM Package:** [rn-swiftauth-sdk](https://www.npmjs.com/package/rn-swiftauth-sdk)
|
|
510
|
+
- **Documentation:** [Full Docs](https://github.com/allcodez/Auth-SDK_Stage8)
|