rn-swiftauth-sdk 1.0.1 โ 1.0.2
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 +147 -374
- package/dist/components/LoginForm.js +19 -11
- package/dist/components/SignUpForm.js +17 -16
- package/dist/core/AuthProvider.js +45 -75
- package/dist/types/auth.types.d.ts +11 -3
- package/dist/types/auth.types.js +0 -1
- package/dist/types/error.types.d.ts +10 -1
- package/dist/types/error.types.js +16 -1
- package/package.json +1 -1
- package/src/components/LoginForm.tsx +21 -12
- package/src/components/SignUpForm.tsx +20 -31
- package/src/core/AuthProvider.tsx +67 -116
- package/src/types/auth.types.ts +20 -17
- package/src/types/error.types.ts +20 -1
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"
|
|
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
|
+
}
|
|
131
112
|
}
|
|
132
113
|
}
|
|
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
|
|
114
|
+
```
|
|
141
115
|
|
|
142
|
-
|
|
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"
|
|
152
|
-
}
|
|
153
|
-
}
|
|
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)
|
|
192
|
-
|
|
193
|
-
1. Go to your [Apple Developer Account](https://developer.apple.com/)
|
|
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,83 @@ Pre-built authentication UI with login and signup.
|
|
|
385
233
|
/>
|
|
386
234
|
```
|
|
387
235
|
|
|
388
|
-
|
|
236
|
+
---
|
|
389
237
|
|
|
390
|
-
|
|
238
|
+
## Error Handling
|
|
391
239
|
|
|
392
|
-
|
|
393
|
-
import { useState } from 'react';
|
|
394
|
-
import { View, TextInput, Button, Text } from 'react-native';
|
|
395
|
-
import { useAuth } from 'rn-swiftauth-sdk';
|
|
240
|
+
SwiftAuth maps Firebase errors to user-friendly messages:
|
|
396
241
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
242
|
+
| Error Code | User Message |
|
|
243
|
+
|------------|--------------|
|
|
244
|
+
| `auth/invalid-credentials` | "Invalid email or password." |
|
|
245
|
+
| `auth/user-not-found` | "Invalid email or password." |
|
|
246
|
+
| `auth/email-already-in-use` | "This email is already registered." |
|
|
247
|
+
| `auth/weak-password` | "Password is too weak." |
|
|
248
|
+
| `auth/network-request-failed` | "Network error. Please check your connection." |
|
|
401
249
|
|
|
402
|
-
|
|
403
|
-
try {
|
|
404
|
-
await signInWithEmail(email, password);
|
|
405
|
-
} catch (err) {
|
|
406
|
-
console.error('Login failed:', err);
|
|
407
|
-
}
|
|
408
|
-
};
|
|
250
|
+
### Basic Usage
|
|
409
251
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
<Text style={{ fontSize: 24, marginBottom: 20 }}>Login</Text>
|
|
413
|
-
|
|
414
|
-
{error && <Text style={{ color: 'red' }}>{error.message}</Text>}
|
|
415
|
-
|
|
416
|
-
<TextInput
|
|
417
|
-
placeholder="Email"
|
|
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} />
|
|
432
|
-
</View>
|
|
433
|
-
);
|
|
434
|
-
};
|
|
435
|
-
```
|
|
252
|
+
```typescript
|
|
253
|
+
const { error } = useAuth();
|
|
436
254
|
|
|
437
|
-
|
|
255
|
+
if (error) {
|
|
256
|
+
return <Text style={{ color: 'red' }}>{error.message}</Text>;
|
|
257
|
+
}
|
|
258
|
+
```
|
|
438
259
|
|
|
439
|
-
|
|
260
|
+
### Advanced: Raw Firebase Errors
|
|
440
261
|
|
|
441
|
-
|
|
262
|
+
For custom UI implementations, access raw Firebase errors:
|
|
442
263
|
|
|
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." |
|
|
264
|
+
**Method 1: Try/Catch Block**
|
|
265
|
+
```typescript
|
|
266
|
+
const { signInWithEmail } = useAuth();
|
|
452
267
|
|
|
453
|
-
|
|
268
|
+
const handleLogin = async () => {
|
|
269
|
+
try {
|
|
270
|
+
await signInWithEmail({ email, password });
|
|
271
|
+
} catch (rawError: any) {
|
|
272
|
+
if (rawError.code === 'auth/requires-recent-login') {
|
|
273
|
+
showReauthModal();
|
|
274
|
+
} else if (rawError.code === 'auth/quota-exceeded') {
|
|
275
|
+
Alert.alert("System Overload", "Please try again later.");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
```
|
|
454
280
|
|
|
455
|
-
|
|
281
|
+
**Method 2: Global State**
|
|
282
|
+
```typescript
|
|
456
283
|
const { error } = useAuth();
|
|
457
284
|
|
|
458
|
-
|
|
459
|
-
if (error
|
|
460
|
-
|
|
285
|
+
useEffect(() => {
|
|
286
|
+
if (error?.originalError) {
|
|
287
|
+
const rawCode = (error.originalError as any).code;
|
|
288
|
+
console.log("Raw Firebase Code:", rawCode);
|
|
289
|
+
|
|
290
|
+
if (rawCode === 'auth/invalid-email') {
|
|
291
|
+
setLocalizedMessage(t('errors.bad_email'));
|
|
292
|
+
}
|
|
461
293
|
}
|
|
462
|
-
|
|
463
|
-
}
|
|
294
|
+
}, [error]);
|
|
464
295
|
```
|
|
465
296
|
|
|
466
297
|
---
|
|
467
298
|
|
|
468
|
-
##
|
|
299
|
+
## Session Management
|
|
469
300
|
|
|
470
301
|
### Keep User Logged In (Default)
|
|
471
302
|
|
|
472
|
-
```
|
|
303
|
+
```typescript
|
|
473
304
|
const config = {
|
|
474
305
|
...firebaseConfig,
|
|
475
306
|
persistence: 'local' // User stays logged in
|
|
476
307
|
};
|
|
477
308
|
```
|
|
478
309
|
|
|
479
|
-
###
|
|
310
|
+
### Memory-Only Session
|
|
480
311
|
|
|
481
|
-
```
|
|
312
|
+
```typescript
|
|
482
313
|
const config = {
|
|
483
314
|
...firebaseConfig,
|
|
484
315
|
persistence: 'memory' // User logged out when app closes
|
|
@@ -487,15 +318,11 @@ const config = {
|
|
|
487
318
|
|
|
488
319
|
---
|
|
489
320
|
|
|
490
|
-
##
|
|
491
|
-
|
|
492
|
-
The SwiftAuth SDK handles the complexity of authentication (Google, Apple, Email), but you likely have your own API for user data.
|
|
321
|
+
## Backend Integration
|
|
493
322
|
|
|
494
|
-
The SDK exposes a secure Firebase ID Token (`user.token`)
|
|
323
|
+
The SDK exposes a secure Firebase ID Token (`user.token`) for backend authentication.
|
|
495
324
|
|
|
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.
|
|
325
|
+
### Frontend: Sending the Token
|
|
499
326
|
|
|
500
327
|
```typescript
|
|
501
328
|
import { useAuth } from 'rn-swiftauth-sdk';
|
|
@@ -511,13 +338,12 @@ const UserProfile = () => {
|
|
|
511
338
|
method: 'GET',
|
|
512
339
|
headers: {
|
|
513
340
|
'Content-Type': 'application/json',
|
|
514
|
-
// Send the token as a Bearer token
|
|
515
341
|
'Authorization': `Bearer ${user.token}`
|
|
516
342
|
}
|
|
517
343
|
});
|
|
518
|
-
|
|
344
|
+
|
|
519
345
|
const data = await response.json();
|
|
520
|
-
console.log(
|
|
346
|
+
console.log(data);
|
|
521
347
|
} catch (error) {
|
|
522
348
|
console.error('Request failed:', error);
|
|
523
349
|
}
|
|
@@ -527,24 +353,12 @@ const UserProfile = () => {
|
|
|
527
353
|
};
|
|
528
354
|
```
|
|
529
355
|
|
|
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):**
|
|
356
|
+
### Backend: Verifying the Token (Node.js)
|
|
535
357
|
|
|
536
358
|
```javascript
|
|
537
359
|
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
360
|
|
|
547
|
-
//
|
|
361
|
+
// Middleware to verify Firebase ID token
|
|
548
362
|
const verifyToken = async (req, res, next) => {
|
|
549
363
|
const authHeader = req.headers.authorization;
|
|
550
364
|
|
|
@@ -553,39 +367,25 @@ const verifyToken = async (req, res, next) => {
|
|
|
553
367
|
}
|
|
554
368
|
|
|
555
369
|
const idToken = authHeader.split('Bearer ')[1];
|
|
556
|
-
|
|
370
|
+
|
|
557
371
|
try {
|
|
558
|
-
// Decodes the token and checks signature
|
|
559
372
|
const decodedToken = await admin.auth().verifyIdToken(idToken);
|
|
560
|
-
|
|
561
|
-
// Attach user info to request (uid, email, etc.)
|
|
562
|
-
req.user = decodedToken;
|
|
373
|
+
req.user = decodedToken;
|
|
563
374
|
next();
|
|
564
375
|
} catch (error) {
|
|
565
376
|
res.status(403).send('Invalid Token');
|
|
566
377
|
}
|
|
567
378
|
};
|
|
568
379
|
|
|
569
|
-
//
|
|
380
|
+
// Protected route example
|
|
570
381
|
app.get('/profile', verifyToken, (req, res) => {
|
|
571
|
-
|
|
572
|
-
res.json({ message: `Hello ${req.user.email}, here is your private data.` });
|
|
382
|
+
res.json({ userId: req.user.uid, email: req.user.email });
|
|
573
383
|
});
|
|
574
384
|
```
|
|
575
385
|
|
|
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
386
|
---
|
|
587
387
|
|
|
588
|
-
##
|
|
388
|
+
## Example App
|
|
589
389
|
|
|
590
390
|
Check out our example implementation:
|
|
591
391
|
|
|
@@ -605,47 +405,20 @@ npx expo start
|
|
|
605
405
|
|
|
606
406
|
---
|
|
607
407
|
|
|
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
|
|
408
|
+
## Contributing
|
|
625
409
|
|
|
626
|
-
|
|
410
|
+
We welcome contributions! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
627
411
|
|
|
628
412
|
---
|
|
629
413
|
|
|
630
|
-
##
|
|
414
|
+
## License
|
|
631
415
|
|
|
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)
|
|
416
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
636
417
|
|
|
637
418
|
---
|
|
638
419
|
|
|
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
|
-
---
|
|
420
|
+
## Support
|
|
650
421
|
|
|
651
|
-
|
|
422
|
+
- **Issues:** [GitHub Issues](https://github.com/allcodez/Auth-SDK_Stage8/issues)
|
|
423
|
+
- **NPM Package:** [rn-swiftauth-sdk](https://www.npmjs.com/package/rn-swiftauth-sdk)
|
|
424
|
+
- **Documentation:** [Full Docs](https://github.com/allcodez/Auth-SDK_Stage8)
|