capacitor-auth-manager 0.0.1 → 0.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/CapacitorAuthManager.podspec +36 -0
- package/Readme.md +542 -20
- package/android/build.gradle +79 -0
- package/android/gradle.properties +25 -0
- package/android/proguard-rules.pro +38 -0
- package/android/settings.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +11 -0
- package/android/src/main/java/com/zaions/cap_auth_manager/AuthLogger.java +117 -0
- package/android/src/main/java/com/zaions/cap_auth_manager/AuthStorage.java +124 -0
- package/android/src/main/java/com/zaions/cap_auth_manager/BaseAuthProvider.java +16 -0
- package/android/src/main/java/com/zaions/cap_auth_manager/CapacitorAuthManager.java +385 -0
- package/android/src/main/java/com/zaions/cap_auth_manager/CapacitorAuthManagerPlugin.java +469 -0
- package/dist/esm/definitions.d.ts +521 -0
- package/dist/esm/definitions.js +103 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +6 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/providers/base-provider.d.ts +43 -0
- package/dist/esm/providers/base-provider.js +112 -0
- package/dist/esm/providers/base-provider.js.map +1 -0
- package/dist/esm/providers/oauth-provider.d.ts +30 -0
- package/dist/esm/providers/oauth-provider.js +255 -0
- package/dist/esm/providers/oauth-provider.js.map +1 -0
- package/dist/esm/providers/provider-factory.d.ts +7 -0
- package/dist/esm/providers/provider-factory.js +76 -0
- package/dist/esm/providers/provider-factory.js.map +1 -0
- package/dist/esm/providers/web/apple-provider.d.ts +33 -0
- package/dist/esm/providers/web/apple-provider.js +270 -0
- package/dist/esm/providers/web/apple-provider.js.map +1 -0
- package/dist/esm/providers/web/google-provider.d.ts +27 -0
- package/dist/esm/providers/web/google-provider.js +258 -0
- package/dist/esm/providers/web/google-provider.js.map +1 -0
- package/dist/esm/utils/auth-error.d.ts +16 -0
- package/dist/esm/utils/auth-error.js +53 -0
- package/dist/esm/utils/auth-error.js.map +1 -0
- package/dist/esm/utils/event-emitter.d.ts +9 -0
- package/dist/esm/utils/event-emitter.js +28 -0
- package/dist/esm/utils/event-emitter.js.map +1 -0
- package/dist/esm/utils/logger.d.ts +19 -0
- package/dist/esm/utils/logger.js +48 -0
- package/dist/esm/utils/logger.js.map +1 -0
- package/dist/esm/utils/storage.d.ts +16 -0
- package/dist/esm/utils/storage.js +86 -0
- package/dist/esm/utils/storage.js.map +1 -0
- package/dist/esm/web.d.ts +43 -0
- package/dist/esm/web.js +374 -1
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +1650 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +1652 -3
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/AuthLogger.swift +71 -0
- package/ios/Plugin/AuthStorage.swift +139 -0
- package/ios/Plugin/BaseAuthProvider.swift +33 -0
- package/ios/Plugin/CapacitorAuthManager.swift +406 -0
- package/ios/Plugin/Models.swift +376 -0
- package/ios/Plugin/Plugin.m +27 -0
- package/ios/Plugin/Plugin.swift +413 -0
- package/package.json +6 -2
- package/scripts/configure.js +295 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'CapacitorAuthManager'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['repository']['url']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '13.0'
|
|
15
|
+
s.swift_version = '5.9'
|
|
16
|
+
s.dependency 'Capacitor'
|
|
17
|
+
|
|
18
|
+
# Auth Provider Dependencies
|
|
19
|
+
s.dependency 'GoogleSignIn', '~> 7.0'
|
|
20
|
+
s.dependency 'FBSDKLoginKit', '~> 17.0'
|
|
21
|
+
|
|
22
|
+
# Apple Sign In is built into iOS
|
|
23
|
+
|
|
24
|
+
# Microsoft Authentication Library
|
|
25
|
+
s.dependency 'MSAL', '~> 1.5'
|
|
26
|
+
|
|
27
|
+
# Firebase Auth (optional, can be added when needed)
|
|
28
|
+
# s.dependency 'Firebase/Auth'
|
|
29
|
+
|
|
30
|
+
s.static_framework = true
|
|
31
|
+
|
|
32
|
+
# Required for Google Sign In
|
|
33
|
+
s.pod_target_xcconfig = {
|
|
34
|
+
'SWIFT_OBJC_BRIDGING_HEADER' => '$(PODS_TARGET_SRCROOT)/ios/Plugin/Plugin-Bridging-Header.h'
|
|
35
|
+
}
|
|
36
|
+
end
|
package/Readme.md
CHANGED
|
@@ -1,31 +1,553 @@
|
|
|
1
1
|
# Capacitor Auth Manager
|
|
2
2
|
|
|
3
|
-
A comprehensive
|
|
3
|
+
A comprehensive authentication plugin for Capacitor that provides secure, type-safe, and framework-independent authentication implementations across Android, iOS, and Web platforms.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Features
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
- 🔐 **13+ Authentication Providers**: Google, Apple, Microsoft, Facebook, GitHub, Slack, LinkedIn, Firebase, Email Magic Link, SMS, Email/Phone/Username + Password, Email Code, and Biometric Auth
|
|
8
|
+
- 📱 **Cross-Platform**: Works seamlessly on iOS, Android, and Web
|
|
9
|
+
- 🔄 **Subscribable Auth State**: Real-time authentication state updates
|
|
10
|
+
- 🎯 **Type-Safe**: Full TypeScript support with comprehensive type definitions
|
|
11
|
+
- 🏗️ **Framework Independent**: Works with any JavaScript framework
|
|
12
|
+
- 🔒 **Secure**: Uses only official SDKs and follows security best practices
|
|
13
|
+
- ⚡ **Performant**: Optimized for minimal overhead and fast authentication flows
|
|
14
|
+
- 🔧 **Highly Configurable**: Each provider can be configured with all available options
|
|
10
15
|
|
|
11
|
-
|
|
16
|
+
## Installation
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
```bash
|
|
19
|
+
npm install capacitor-auth-manager
|
|
20
|
+
npx cap sync
|
|
21
|
+
```
|
|
14
22
|
|
|
15
|
-
|
|
23
|
+
## Quick Start
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
```typescript
|
|
26
|
+
import { CapacitorAuthManager, AuthProvider } from 'capacitor-auth-manager';
|
|
18
27
|
|
|
19
|
-
|
|
28
|
+
// Initialize the auth manager
|
|
29
|
+
await CapacitorAuthManager.initialize({
|
|
30
|
+
providers: [
|
|
31
|
+
{
|
|
32
|
+
provider: AuthProvider.GOOGLE,
|
|
33
|
+
options: {
|
|
34
|
+
clientId: 'YOUR_GOOGLE_CLIENT_ID',
|
|
35
|
+
scopes: ['email', 'profile']
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
provider: AuthProvider.APPLE,
|
|
40
|
+
options: {
|
|
41
|
+
clientId: 'YOUR_APPLE_CLIENT_ID',
|
|
42
|
+
redirectUri: 'YOUR_REDIRECT_URI'
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
persistence: 'local', // 'local' | 'session' | 'none'
|
|
47
|
+
autoRefreshToken: true,
|
|
48
|
+
enableLogging: true
|
|
49
|
+
});
|
|
20
50
|
|
|
21
|
-
|
|
51
|
+
// Sign in with Google
|
|
52
|
+
const result = await CapacitorAuthManager.signIn({
|
|
53
|
+
provider: AuthProvider.GOOGLE
|
|
54
|
+
});
|
|
22
55
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
56
|
+
console.log('Signed in user:', result.user);
|
|
57
|
+
|
|
58
|
+
// Listen to auth state changes
|
|
59
|
+
const listener = await CapacitorAuthManager.addAuthStateListener((user) => {
|
|
60
|
+
console.log('Auth state changed:', user);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Sign out
|
|
64
|
+
await CapacitorAuthManager.signOut();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Configuration
|
|
68
|
+
|
|
69
|
+
### Platform-Specific Setup
|
|
70
|
+
|
|
71
|
+
#### iOS Setup
|
|
72
|
+
|
|
73
|
+
1. Add required capabilities in Xcode:
|
|
74
|
+
- Sign in with Apple (if using Apple Auth)
|
|
75
|
+
- Keychain Sharing (for secure credential storage)
|
|
76
|
+
|
|
77
|
+
2. Update your `Info.plist`:
|
|
78
|
+
```xml
|
|
79
|
+
<!-- For Google Sign-In -->
|
|
80
|
+
<key>CFBundleURLTypes</key>
|
|
81
|
+
<array>
|
|
82
|
+
<dict>
|
|
83
|
+
<key>CFBundleURLSchemes</key>
|
|
84
|
+
<array>
|
|
85
|
+
<string>YOUR_REVERSED_CLIENT_ID</string>
|
|
86
|
+
</array>
|
|
87
|
+
</dict>
|
|
88
|
+
</array>
|
|
89
|
+
|
|
90
|
+
<!-- For Facebook Login -->
|
|
91
|
+
<key>FacebookAppID</key>
|
|
92
|
+
<string>YOUR_FACEBOOK_APP_ID</string>
|
|
93
|
+
<key>FacebookClientToken</key>
|
|
94
|
+
<string>YOUR_FACEBOOK_CLIENT_TOKEN</string>
|
|
95
|
+
<key>FacebookDisplayName</key>
|
|
96
|
+
<string>YOUR_APP_NAME</string>
|
|
97
|
+
|
|
98
|
+
<!-- For OAuth redirects -->
|
|
99
|
+
<key>LSApplicationQueriesSchemes</key>
|
|
100
|
+
<array>
|
|
101
|
+
<string>fbapi</string>
|
|
102
|
+
<string>fb-messenger-share-api</string>
|
|
103
|
+
<string>fbauth2</string>
|
|
104
|
+
<string>fbshareextension</string>
|
|
105
|
+
</array>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### Android Setup
|
|
109
|
+
|
|
110
|
+
1. Add to your `android/app/build.gradle`:
|
|
111
|
+
```gradle
|
|
112
|
+
android {
|
|
113
|
+
defaultConfig {
|
|
114
|
+
manifestPlaceholders = [
|
|
115
|
+
appAuthRedirectScheme: 'com.yourcompany.yourapp'
|
|
116
|
+
]
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
2. Update your `AndroidManifest.xml`:
|
|
122
|
+
```xml
|
|
123
|
+
<!-- For Google Sign-In -->
|
|
124
|
+
<meta-data
|
|
125
|
+
android:name="com.google.android.gms.auth.api.signin.GoogleSignInOptions"
|
|
126
|
+
android:value="@string/default_web_client_id" />
|
|
127
|
+
|
|
128
|
+
<!-- For Facebook Login -->
|
|
129
|
+
<meta-data
|
|
130
|
+
android:name="com.facebook.sdk.ApplicationId"
|
|
131
|
+
android:value="@string/facebook_app_id"/>
|
|
132
|
+
<meta-data
|
|
133
|
+
android:name="com.facebook.sdk.ClientToken"
|
|
134
|
+
android:value="@string/facebook_client_token"/>
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### Web Setup
|
|
138
|
+
|
|
139
|
+
For web platform, make sure your redirect URIs are properly configured in each provider's console:
|
|
140
|
+
|
|
141
|
+
- **Google**: Add your domain to authorized JavaScript origins
|
|
142
|
+
- **Apple**: Configure your Service ID with proper domains and redirect URLs
|
|
143
|
+
- **Facebook**: Add your domain to valid OAuth redirect URIs
|
|
144
|
+
- **Microsoft**: Configure redirect URIs in Azure AD
|
|
145
|
+
|
|
146
|
+
## Authentication Providers
|
|
147
|
+
|
|
148
|
+
### Google Authentication
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
await CapacitorAuthManager.initialize({
|
|
152
|
+
providers: [{
|
|
153
|
+
provider: AuthProvider.GOOGLE,
|
|
154
|
+
options: {
|
|
155
|
+
clientId: 'YOUR_GOOGLE_CLIENT_ID',
|
|
156
|
+
scopes: ['email', 'profile'],
|
|
157
|
+
serverClientId: 'YOUR_SERVER_CLIENT_ID', // For Android
|
|
158
|
+
offlineAccess: true, // To get refresh token
|
|
159
|
+
hostedDomain: 'yourdomain.com', // Restrict to domain
|
|
160
|
+
forceCodeForRefreshToken: true
|
|
161
|
+
}
|
|
162
|
+
}]
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const result = await CapacitorAuthManager.signIn({
|
|
166
|
+
provider: AuthProvider.GOOGLE,
|
|
167
|
+
options: {
|
|
168
|
+
loginHint: 'user@example.com', // Pre-fill email
|
|
169
|
+
prompt: 'select_account' // Force account selection
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Apple Authentication
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
await CapacitorAuthManager.initialize({
|
|
178
|
+
providers: [{
|
|
179
|
+
provider: AuthProvider.APPLE,
|
|
180
|
+
options: {
|
|
181
|
+
clientId: 'YOUR_SERVICE_ID',
|
|
182
|
+
redirectUri: 'https://yourapp.com/auth/callback',
|
|
183
|
+
scopes: [AppleAuthScope.EMAIL, AppleAuthScope.NAME],
|
|
184
|
+
usePopup: true,
|
|
185
|
+
state: 'YOUR_STATE', // Optional state parameter
|
|
186
|
+
nonce: 'YOUR_NONCE' // Optional nonce for security
|
|
187
|
+
}
|
|
188
|
+
}]
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const result = await CapacitorAuthManager.signIn({
|
|
192
|
+
provider: AuthProvider.APPLE
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Microsoft Authentication
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
await CapacitorAuthManager.initialize({
|
|
200
|
+
providers: [{
|
|
201
|
+
provider: AuthProvider.MICROSOFT,
|
|
202
|
+
options: {
|
|
203
|
+
clientId: 'YOUR_AZURE_CLIENT_ID',
|
|
204
|
+
tenantId: 'YOUR_TENANT_ID', // or 'common' for multi-tenant
|
|
205
|
+
redirectUri: 'https://yourapp.com/auth/callback',
|
|
206
|
+
scopes: ['openid', 'profile', 'email'],
|
|
207
|
+
authority: 'https://login.microsoftonline.com/YOUR_TENANT_ID'
|
|
208
|
+
}
|
|
209
|
+
}]
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Firebase Authentication
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
await CapacitorAuthManager.initialize({
|
|
217
|
+
providers: [{
|
|
218
|
+
provider: AuthProvider.FIREBASE,
|
|
219
|
+
options: {
|
|
220
|
+
apiKey: 'YOUR_FIREBASE_API_KEY',
|
|
221
|
+
authDomain: 'your-app.firebaseapp.com',
|
|
222
|
+
projectId: 'your-project-id',
|
|
223
|
+
storageBucket: 'your-app.appspot.com',
|
|
224
|
+
messagingSenderId: 'YOUR_SENDER_ID',
|
|
225
|
+
appId: 'YOUR_APP_ID'
|
|
226
|
+
}
|
|
227
|
+
}]
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Email/Password Authentication
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
await CapacitorAuthManager.initialize({
|
|
235
|
+
providers: [{
|
|
236
|
+
provider: AuthProvider.EMAIL_PASSWORD,
|
|
237
|
+
options: {
|
|
238
|
+
strengthRequirements: {
|
|
239
|
+
minLength: 8,
|
|
240
|
+
requireUppercase: true,
|
|
241
|
+
requireNumbers: true,
|
|
242
|
+
requireSpecialCharacters: true
|
|
243
|
+
},
|
|
244
|
+
allowPasswordReset: true,
|
|
245
|
+
requireEmailVerification: true
|
|
246
|
+
}
|
|
247
|
+
}]
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Sign up
|
|
251
|
+
const result = await CapacitorAuthManager.signIn({
|
|
252
|
+
provider: AuthProvider.EMAIL_PASSWORD,
|
|
253
|
+
credentials: {
|
|
254
|
+
email: 'user@example.com',
|
|
255
|
+
password: 'SecurePassword123!'
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// Password reset
|
|
260
|
+
await CapacitorAuthManager.sendPasswordResetEmail({
|
|
261
|
+
email: 'user@example.com'
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### SMS Authentication
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
await CapacitorAuthManager.initialize({
|
|
269
|
+
providers: [{
|
|
270
|
+
provider: AuthProvider.SMS,
|
|
271
|
+
options: {
|
|
272
|
+
provider: 'twilio', // or 'firebase', 'custom'
|
|
273
|
+
twilioConfig: {
|
|
274
|
+
accountSid: 'YOUR_ACCOUNT_SID',
|
|
275
|
+
authToken: 'YOUR_AUTH_TOKEN',
|
|
276
|
+
fromNumber: '+1234567890'
|
|
277
|
+
},
|
|
278
|
+
codeLength: 6,
|
|
279
|
+
codeExpiration: 300000, // 5 minutes
|
|
280
|
+
maxAttempts: 3
|
|
281
|
+
}
|
|
282
|
+
}]
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// Send SMS code
|
|
286
|
+
await CapacitorAuthManager.sendSmsCode({
|
|
287
|
+
phoneNumber: '+1234567890'
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// Verify SMS code
|
|
291
|
+
const result = await CapacitorAuthManager.verifySmsCode({
|
|
292
|
+
phoneNumber: '+1234567890',
|
|
293
|
+
code: '123456'
|
|
294
|
+
});
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Biometric Authentication
|
|
298
|
+
|
|
299
|
+
Note: For biometric authentication, this plugin integrates with [capacitor-biometric-authentication](https://www.npmjs.com/package/capacitor-biometric-authentication).
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
await CapacitorAuthManager.initialize({
|
|
303
|
+
providers: [{
|
|
304
|
+
provider: AuthProvider.BIOMETRIC,
|
|
305
|
+
options: {
|
|
306
|
+
fallbackToPasscode: true,
|
|
307
|
+
title: 'Authenticate',
|
|
308
|
+
subtitle: 'Use your biometric to sign in',
|
|
309
|
+
description: 'Place your finger on the sensor',
|
|
310
|
+
negativeButtonText: 'Cancel',
|
|
311
|
+
maxAttempts: 3
|
|
312
|
+
}
|
|
313
|
+
}]
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const result = await CapacitorAuthManager.signIn({
|
|
317
|
+
provider: AuthProvider.BIOMETRIC
|
|
318
|
+
});
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
## API Reference
|
|
322
|
+
|
|
323
|
+
### Core Methods
|
|
324
|
+
|
|
325
|
+
#### initialize(options)
|
|
326
|
+
Initializes the auth manager with provider configurations.
|
|
327
|
+
|
|
328
|
+
```typescript
|
|
329
|
+
interface AuthManagerInitOptions {
|
|
330
|
+
providers: AuthProviderConfig[];
|
|
331
|
+
persistence?: 'local' | 'session' | 'none';
|
|
332
|
+
autoRefreshToken?: boolean;
|
|
333
|
+
tokenRefreshBuffer?: number; // ms before token expiry
|
|
334
|
+
enableLogging?: boolean;
|
|
335
|
+
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
#### signIn(options)
|
|
340
|
+
Signs in a user with the specified provider.
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
interface SignInOptions {
|
|
344
|
+
provider: AuthProvider;
|
|
345
|
+
credentials?: AuthCredentials;
|
|
346
|
+
options?: SignInProviderOptions;
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
#### signOut(options?)
|
|
351
|
+
Signs out the current user or a specific provider.
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
interface SignOutOptions {
|
|
355
|
+
provider?: AuthProvider;
|
|
356
|
+
revokeToken?: boolean;
|
|
357
|
+
clearCache?: boolean;
|
|
358
|
+
redirectUrl?: string;
|
|
359
|
+
}
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
#### getCurrentUser()
|
|
363
|
+
Returns the currently authenticated user.
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
const user = await CapacitorAuthManager.getCurrentUser();
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
#### addAuthStateListener(callback)
|
|
370
|
+
Subscribes to authentication state changes.
|
|
371
|
+
|
|
372
|
+
```typescript
|
|
373
|
+
const listener = await CapacitorAuthManager.addAuthStateListener((user) => {
|
|
374
|
+
if (user) {
|
|
375
|
+
console.log('User signed in:', user);
|
|
376
|
+
} else {
|
|
377
|
+
console.log('User signed out');
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
// Remove listener
|
|
382
|
+
await listener.remove();
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
#### refreshToken(options?)
|
|
386
|
+
Manually refreshes the authentication token.
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
const result = await CapacitorAuthManager.refreshToken({
|
|
390
|
+
provider: AuthProvider.GOOGLE,
|
|
391
|
+
forceRefresh: true
|
|
392
|
+
});
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
### Account Management
|
|
396
|
+
|
|
397
|
+
#### linkAccount(options)
|
|
398
|
+
Links an additional authentication provider to the current user.
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
await CapacitorAuthManager.linkAccount({
|
|
402
|
+
provider: AuthProvider.GOOGLE
|
|
403
|
+
});
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
#### unlinkAccount(options)
|
|
407
|
+
Unlinks an authentication provider from the current user.
|
|
408
|
+
|
|
409
|
+
```typescript
|
|
410
|
+
await CapacitorAuthManager.unlinkAccount({
|
|
411
|
+
provider: AuthProvider.GOOGLE
|
|
412
|
+
});
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
#### updateProfile(options)
|
|
416
|
+
Updates the user's profile information.
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
await CapacitorAuthManager.updateProfile({
|
|
420
|
+
displayName: 'John Doe',
|
|
421
|
+
photoURL: 'https://example.com/photo.jpg'
|
|
422
|
+
});
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
#### deleteAccount(options?)
|
|
426
|
+
Deletes the current user's account.
|
|
427
|
+
|
|
428
|
+
```typescript
|
|
429
|
+
await CapacitorAuthManager.deleteAccount({
|
|
430
|
+
requireReauthentication: true
|
|
431
|
+
});
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### Utility Methods
|
|
435
|
+
|
|
436
|
+
#### isSupported(options)
|
|
437
|
+
Checks if a provider is supported on the current platform.
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
const result = await CapacitorAuthManager.isSupported({
|
|
441
|
+
provider: AuthProvider.APPLE
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
if (result.isSupported) {
|
|
445
|
+
// Provider is available
|
|
446
|
+
}
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
#### getIdToken(options?)
|
|
450
|
+
Gets the current ID token for the authenticated user.
|
|
451
|
+
|
|
452
|
+
```typescript
|
|
453
|
+
const token = await CapacitorAuthManager.getIdToken({
|
|
454
|
+
forceRefresh: true
|
|
455
|
+
});
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
## Error Handling
|
|
459
|
+
|
|
460
|
+
The plugin provides detailed error codes for different scenarios:
|
|
461
|
+
|
|
462
|
+
```typescript
|
|
463
|
+
try {
|
|
464
|
+
await CapacitorAuthManager.signIn({
|
|
465
|
+
provider: AuthProvider.GOOGLE
|
|
466
|
+
});
|
|
467
|
+
} catch (error) {
|
|
468
|
+
switch (error.code) {
|
|
469
|
+
case 'auth/user-cancelled':
|
|
470
|
+
// User cancelled the sign-in flow
|
|
471
|
+
break;
|
|
472
|
+
case 'auth/network-error':
|
|
473
|
+
// Network error occurred
|
|
474
|
+
break;
|
|
475
|
+
case 'auth/invalid-credentials':
|
|
476
|
+
// Invalid credentials provided
|
|
477
|
+
break;
|
|
478
|
+
case 'auth/popup-blocked':
|
|
479
|
+
// Popup was blocked by browser
|
|
480
|
+
break;
|
|
481
|
+
default:
|
|
482
|
+
// Other error
|
|
483
|
+
console.error('Auth error:', error);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
## Security Considerations
|
|
489
|
+
|
|
490
|
+
1. **Client IDs and Secrets**: Never expose client secrets in your client-side code
|
|
491
|
+
2. **Token Storage**: Tokens are securely stored using platform-specific secure storage
|
|
492
|
+
3. **HTTPS Required**: Always use HTTPS in production for web implementations
|
|
493
|
+
4. **Validate Tokens**: Always validate tokens on your backend before trusting them
|
|
494
|
+
5. **Biometric Security**: Biometric data never leaves the device
|
|
495
|
+
|
|
496
|
+
## Migration Guide
|
|
497
|
+
|
|
498
|
+
If you're migrating from other authentication solutions:
|
|
499
|
+
|
|
500
|
+
### From Firebase Auth
|
|
501
|
+
|
|
502
|
+
```typescript
|
|
503
|
+
// Before (Firebase Auth)
|
|
504
|
+
import { getAuth, signInWithPopup, GoogleAuthProvider } from 'firebase/auth';
|
|
505
|
+
const auth = getAuth();
|
|
506
|
+
const provider = new GoogleAuthProvider();
|
|
507
|
+
const result = await signInWithPopup(auth, provider);
|
|
508
|
+
|
|
509
|
+
// After (Capacitor Auth Manager)
|
|
510
|
+
import { CapacitorAuthManager, AuthProvider } from 'capacitor-auth-manager';
|
|
511
|
+
const result = await CapacitorAuthManager.signIn({
|
|
512
|
+
provider: AuthProvider.GOOGLE
|
|
513
|
+
});
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
### From Capacitor Google Auth
|
|
517
|
+
|
|
518
|
+
```typescript
|
|
519
|
+
// Before
|
|
520
|
+
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
|
|
521
|
+
await GoogleAuth.initialize();
|
|
522
|
+
const user = await GoogleAuth.signIn();
|
|
523
|
+
|
|
524
|
+
// After
|
|
525
|
+
import { CapacitorAuthManager, AuthProvider } from 'capacitor-auth-manager';
|
|
526
|
+
await CapacitorAuthManager.initialize({
|
|
527
|
+
providers: [{
|
|
528
|
+
provider: AuthProvider.GOOGLE,
|
|
529
|
+
options: { clientId: 'YOUR_CLIENT_ID' }
|
|
530
|
+
}]
|
|
531
|
+
});
|
|
532
|
+
const result = await CapacitorAuthManager.signIn({
|
|
533
|
+
provider: AuthProvider.GOOGLE
|
|
534
|
+
});
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
## Contributing
|
|
538
|
+
|
|
539
|
+
Contributions are welcome! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
540
|
+
|
|
541
|
+
## License
|
|
542
|
+
|
|
543
|
+
MIT
|
|
544
|
+
|
|
545
|
+
## Support
|
|
546
|
+
|
|
547
|
+
- 📧 Email: aoneahsan@gmail.com
|
|
548
|
+
- 🐛 Issues: [GitHub Issues](https://github.com/aoneahsan/capacitor-auth-manager/issues)
|
|
549
|
+
- 💬 Discussions: [GitHub Discussions](https://github.com/aoneahsan/capacitor-auth-manager/discussions)
|
|
550
|
+
|
|
551
|
+
## Credits
|
|
552
|
+
|
|
553
|
+
Created by [Ahsan Mahmood](https://aoneahsan.com) at [Zaions](https://zaions.com)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
dependencies {
|
|
14
|
+
classpath 'com.android.tools.build:gradle:8.5.2'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
apply plugin: 'com.android.library'
|
|
19
|
+
|
|
20
|
+
android {
|
|
21
|
+
namespace "com.zaions.cap_auth_manager"
|
|
22
|
+
compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
|
|
25
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.0"
|
|
28
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
29
|
+
}
|
|
30
|
+
buildTypes {
|
|
31
|
+
release {
|
|
32
|
+
minifyEnabled false
|
|
33
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
lintOptions {
|
|
37
|
+
abortOnError false
|
|
38
|
+
}
|
|
39
|
+
compileOptions {
|
|
40
|
+
sourceCompatibility JavaVersion.VERSION_17
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_17
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
repositories {
|
|
46
|
+
google()
|
|
47
|
+
mavenCentral()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
dependencies {
|
|
51
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
52
|
+
implementation project(':capacitor-android')
|
|
53
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
54
|
+
|
|
55
|
+
// Google Sign-In
|
|
56
|
+
implementation 'com.google.android.gms:play-services-auth:21.3.0'
|
|
57
|
+
|
|
58
|
+
// Facebook SDK
|
|
59
|
+
implementation 'com.facebook.android:facebook-login:17.0.3'
|
|
60
|
+
|
|
61
|
+
// Microsoft Authentication Library (MSAL)
|
|
62
|
+
implementation 'com.microsoft.identity.client:msal:5.2.0'
|
|
63
|
+
|
|
64
|
+
// Firebase Auth (optional)
|
|
65
|
+
// implementation 'com.google.firebase:firebase-auth:23.2.0'
|
|
66
|
+
|
|
67
|
+
// Biometric
|
|
68
|
+
implementation 'androidx.biometric:biometric:1.1.0'
|
|
69
|
+
|
|
70
|
+
// Coroutines for async operations
|
|
71
|
+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0'
|
|
72
|
+
|
|
73
|
+
// Security
|
|
74
|
+
implementation 'androidx.security:security-crypto:1.1.0-alpha06'
|
|
75
|
+
|
|
76
|
+
testImplementation "junit:junit:$junitVersion"
|
|
77
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
78
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
79
|
+
}
|