capacitor-auth-manager 0.0.2 → 1.1.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.
- package/LICENSE +1 -1
- package/Readme.md +245 -1
- package/android/build.gradle +1 -1
- package/android/proguard-rules.pro +1 -1
- package/android/src/main/java/com/{zaions/cap_auth_manager → aoneahsan/capacitor_auth_manager}/AuthLogger.java +1 -1
- package/android/src/main/java/com/{zaions/cap_auth_manager → aoneahsan/capacitor_auth_manager}/AuthStorage.java +1 -1
- package/android/src/main/java/com/{zaions/cap_auth_manager → aoneahsan/capacitor_auth_manager}/BaseAuthProvider.java +1 -1
- package/android/src/main/java/com/{zaions/cap_auth_manager → aoneahsan/capacitor_auth_manager}/CapacitorAuthManager.java +1 -1
- package/android/src/main/java/com/{zaions/cap_auth_manager → aoneahsan/capacitor_auth_manager}/CapacitorAuthManagerPlugin.java +1 -1
- package/dist/esm/definitions.d.ts +5 -5
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/providers/oauth-provider.d.ts +38 -7
- package/dist/esm/providers/oauth-provider.js +17 -12
- package/dist/esm/providers/oauth-provider.js.map +1 -1
- package/dist/esm/providers/web/apple-provider.d.ts +32 -5
- package/dist/esm/providers/web/apple-provider.js +15 -16
- package/dist/esm/providers/web/apple-provider.js.map +1 -1
- package/dist/esm/providers/web/google-provider.d.ts +98 -7
- package/dist/esm/providers/web/google-provider.js +19 -9
- package/dist/esm/providers/web/google-provider.js.map +1 -1
- package/dist/esm/utils/auth-error.d.ts +5 -5
- package/dist/esm/utils/auth-error.js +10 -3
- package/dist/esm/utils/auth-error.js.map +1 -1
- package/dist/esm/utils/event-emitter.d.ts +2 -2
- package/dist/esm/utils/logger.d.ts +4 -4
- package/dist/esm/utils/logger.js.map +1 -1
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +69 -43
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +69 -43
- package/dist/plugin.js.map +1 -1
- package/package.json +9 -9
package/dist/plugin.js
CHANGED
|
@@ -105,10 +105,6 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
105
105
|
AuthErrorCode["TEMPORARILY_UNAVAILABLE"] = "auth/temporarily-unavailable";
|
|
106
106
|
})(exports.AuthErrorCode || (exports.AuthErrorCode = {}));
|
|
107
107
|
|
|
108
|
-
const CapacitorAuthManager = core.registerPlugin('CapacitorAuthManager', {
|
|
109
|
-
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.CapacitorAuthManagerWeb()),
|
|
110
|
-
});
|
|
111
|
-
|
|
112
108
|
class AuthError extends Error {
|
|
113
109
|
constructor(code, message, provider, details) {
|
|
114
110
|
super(message);
|
|
@@ -142,8 +138,12 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
142
138
|
}
|
|
143
139
|
}
|
|
144
140
|
// Handle provider-specific error codes
|
|
145
|
-
|
|
146
|
-
|
|
141
|
+
const errorObj = error;
|
|
142
|
+
if (errorObj === null || errorObj === void 0 ? void 0 : errorObj.code) {
|
|
143
|
+
// Check if the code is a valid AuthErrorCode
|
|
144
|
+
if (Object.values(exports.AuthErrorCode).includes(errorObj.code)) {
|
|
145
|
+
code = errorObj.code;
|
|
146
|
+
}
|
|
147
147
|
}
|
|
148
148
|
return new AuthError(code, message, provider, details);
|
|
149
149
|
}
|
|
@@ -157,6 +157,16 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
157
157
|
};
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
+
function isAuthError(error) {
|
|
161
|
+
return error instanceof AuthError ||
|
|
162
|
+
(typeof error === 'object' && error !== null &&
|
|
163
|
+
error.name === 'AuthError' &&
|
|
164
|
+
error.code !== undefined);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const CapacitorAuthManager = core.registerPlugin('CapacitorAuthManager', {
|
|
168
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.CapacitorAuthManagerWeb()),
|
|
169
|
+
});
|
|
160
170
|
|
|
161
171
|
class EventEmitter {
|
|
162
172
|
constructor() {
|
|
@@ -445,6 +455,9 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
445
455
|
// Validate response
|
|
446
456
|
await this.validateOAuthResponse(authResponse, state, nonce);
|
|
447
457
|
// Exchange authorization code for tokens
|
|
458
|
+
if (!authResponse.code) {
|
|
459
|
+
throw new AuthError(exports.AuthErrorCode.INVALID_GRANT, 'No authorization code received', this.provider);
|
|
460
|
+
}
|
|
448
461
|
const tokenResponse = await this.exchangeCodeForTokens(authResponse.code, config);
|
|
449
462
|
// Parse user information
|
|
450
463
|
const userInfo = await this.parseUserFromTokenResponse(tokenResponse);
|
|
@@ -454,7 +467,7 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
454
467
|
// Save user and credential
|
|
455
468
|
await this.setCurrentUser(user);
|
|
456
469
|
await this.saveCredential(credential);
|
|
457
|
-
return this.createAuthResult(user, credential, userInfo.isNewUser);
|
|
470
|
+
return this.createAuthResult(user, credential, userInfo.isNewUser || false);
|
|
458
471
|
}
|
|
459
472
|
catch (error) {
|
|
460
473
|
this.logger.error(`OAuth flow failed for ${this.provider}`, error);
|
|
@@ -520,7 +533,8 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
520
533
|
});
|
|
521
534
|
const data = await response.json();
|
|
522
535
|
if (!response.ok) {
|
|
523
|
-
|
|
536
|
+
const errorData = data;
|
|
537
|
+
throw new AuthError(this.mapOAuthError(errorData.error), errorData.error_description || 'Token exchange failed', this.provider, errorData);
|
|
524
538
|
}
|
|
525
539
|
return data;
|
|
526
540
|
}
|
|
@@ -556,7 +570,8 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
556
570
|
});
|
|
557
571
|
const data = await response.json();
|
|
558
572
|
if (!response.ok) {
|
|
559
|
-
|
|
573
|
+
const errorData = data;
|
|
574
|
+
throw new AuthError(this.mapOAuthError(errorData.error), errorData.error_description || 'Token refresh failed', this.provider, errorData);
|
|
560
575
|
}
|
|
561
576
|
// Update credential with new tokens
|
|
562
577
|
const newCredential = this.createOAuthCredential(data);
|
|
@@ -629,20 +644,20 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
629
644
|
createAuthUser(userInfo, tokenResponse) {
|
|
630
645
|
return {
|
|
631
646
|
uid: userInfo.id || userInfo.sub || this.generateUniqueId(),
|
|
632
|
-
email: userInfo.email,
|
|
647
|
+
email: userInfo.email || null,
|
|
633
648
|
emailVerified: userInfo.email_verified || false,
|
|
634
|
-
displayName: userInfo.name || userInfo.displayName,
|
|
635
|
-
photoURL: userInfo.picture || userInfo.photoURL,
|
|
636
|
-
phoneNumber: userInfo.phone_number,
|
|
649
|
+
displayName: userInfo.name || userInfo.displayName || null,
|
|
650
|
+
photoURL: userInfo.picture || userInfo.photoURL || null,
|
|
651
|
+
phoneNumber: userInfo.phone_number || null,
|
|
637
652
|
isAnonymous: false,
|
|
638
653
|
tenantId: null,
|
|
639
654
|
providerData: [{
|
|
640
655
|
providerId: this.provider,
|
|
641
|
-
uid: userInfo.id || userInfo.sub,
|
|
642
|
-
displayName: userInfo.name,
|
|
643
|
-
email: userInfo.email,
|
|
644
|
-
phoneNumber: userInfo.phone_number,
|
|
645
|
-
photoURL: userInfo.picture,
|
|
656
|
+
uid: userInfo.id || userInfo.sub || '',
|
|
657
|
+
displayName: userInfo.name || null,
|
|
658
|
+
email: userInfo.email || null,
|
|
659
|
+
phoneNumber: userInfo.phone_number || null,
|
|
660
|
+
photoURL: userInfo.picture || null,
|
|
646
661
|
}],
|
|
647
662
|
metadata: {
|
|
648
663
|
creationTime: new Date().toISOString(),
|
|
@@ -719,11 +734,15 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
719
734
|
}
|
|
720
735
|
}
|
|
721
736
|
async _initialize() {
|
|
737
|
+
var _a, _b;
|
|
722
738
|
const options = this.options;
|
|
723
739
|
try {
|
|
724
740
|
// Load Google Identity Services library
|
|
725
741
|
await this.loadGoogleIdentityServices();
|
|
726
742
|
// Initialize Google client
|
|
743
|
+
if (!((_b = (_a = window.google) === null || _a === void 0 ? void 0 : _a.accounts) === null || _b === void 0 ? void 0 : _b.oauth2)) {
|
|
744
|
+
throw new Error('Google Identity Services not loaded');
|
|
745
|
+
}
|
|
727
746
|
window.google.accounts.oauth2.initCodeClient({
|
|
728
747
|
client_id: options.clientId,
|
|
729
748
|
scope: (options.scopes || ['openid', 'email', 'profile']).join(' '),
|
|
@@ -745,7 +764,7 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
745
764
|
async signIn(options) {
|
|
746
765
|
this.validateInitialized();
|
|
747
766
|
return new Promise((resolve, reject) => {
|
|
748
|
-
var _a, _b, _c;
|
|
767
|
+
var _a, _b, _c, _d, _e;
|
|
749
768
|
try {
|
|
750
769
|
const googleOptions = this.options;
|
|
751
770
|
// Configure sign-in options
|
|
@@ -759,6 +778,9 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
759
778
|
}
|
|
760
779
|
try {
|
|
761
780
|
// Exchange authorization code for tokens
|
|
781
|
+
if (!response.code) {
|
|
782
|
+
throw new AuthError(exports.AuthErrorCode.INVALID_GRANT, 'No authorization code received', this.provider);
|
|
783
|
+
}
|
|
762
784
|
const tokenResponse = await this.exchangeCodeForTokens(response.code, this.getOAuthConfig());
|
|
763
785
|
// Get user info
|
|
764
786
|
const userInfo = await this.getUserInfo(tokenResponse.access_token);
|
|
@@ -784,6 +806,9 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
784
806
|
signInConfig.hd = googleOptions.hostedDomain;
|
|
785
807
|
}
|
|
786
808
|
// Create new client with callback
|
|
809
|
+
if (!((_e = (_d = window.google) === null || _d === void 0 ? void 0 : _d.accounts) === null || _e === void 0 ? void 0 : _e.oauth2)) {
|
|
810
|
+
throw new Error('Google Identity Services not loaded');
|
|
811
|
+
}
|
|
787
812
|
const client = window.google.accounts.oauth2.initCodeClient(signInConfig);
|
|
788
813
|
// Request authorization code
|
|
789
814
|
client.requestCode();
|
|
@@ -910,21 +935,21 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
910
935
|
}
|
|
911
936
|
createAuthUser(userInfo, tokenResponse) {
|
|
912
937
|
return {
|
|
913
|
-
uid: userInfo.sub,
|
|
914
|
-
email: userInfo.email,
|
|
938
|
+
uid: userInfo.sub || '',
|
|
939
|
+
email: userInfo.email || null,
|
|
915
940
|
emailVerified: userInfo.email_verified || false,
|
|
916
|
-
displayName: userInfo.name,
|
|
917
|
-
photoURL: userInfo.picture,
|
|
941
|
+
displayName: userInfo.name || null,
|
|
942
|
+
photoURL: userInfo.picture || null,
|
|
918
943
|
phoneNumber: null,
|
|
919
944
|
isAnonymous: false,
|
|
920
945
|
tenantId: null,
|
|
921
946
|
providerData: [{
|
|
922
947
|
providerId: exports.AuthProvider.GOOGLE,
|
|
923
|
-
uid: userInfo.sub,
|
|
924
|
-
displayName: userInfo.name,
|
|
925
|
-
email: userInfo.email,
|
|
948
|
+
uid: userInfo.sub || '',
|
|
949
|
+
displayName: userInfo.name || null,
|
|
950
|
+
email: userInfo.email || null,
|
|
926
951
|
phoneNumber: null,
|
|
927
|
-
photoURL: userInfo.picture,
|
|
952
|
+
photoURL: userInfo.picture || null,
|
|
928
953
|
}],
|
|
929
954
|
metadata: {
|
|
930
955
|
creationTime: new Date().toISOString(),
|
|
@@ -979,7 +1004,7 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
979
1004
|
clientId: options.clientId,
|
|
980
1005
|
scope: this.mapAppleScopes(options.scopes).join(' '),
|
|
981
1006
|
redirectURI: options.redirectUri,
|
|
982
|
-
state: options.state,
|
|
1007
|
+
state: options.state || this.generateSecureRandomString(),
|
|
983
1008
|
nonce: options.nonce,
|
|
984
1009
|
usePopup: options.usePopup !== false, // Default to true
|
|
985
1010
|
});
|
|
@@ -1075,9 +1100,10 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
1075
1100
|
// This method is not used in Apple JS SDK flow
|
|
1076
1101
|
throw new AuthError(exports.AuthErrorCode.OPERATION_NOT_ALLOWED, 'Direct OAuth flow not supported for Apple. Use signIn method instead.', this.provider);
|
|
1077
1102
|
}
|
|
1078
|
-
async parseUserFromTokenResponse(
|
|
1103
|
+
async parseUserFromTokenResponse(_response) {
|
|
1079
1104
|
// Apple provides user info directly in the authorization response
|
|
1080
|
-
|
|
1105
|
+
// Note: This is a simplified implementation since Apple doesn't provide user info in the token response
|
|
1106
|
+
return {};
|
|
1081
1107
|
}
|
|
1082
1108
|
async loadAppleSDK() {
|
|
1083
1109
|
var _a;
|
|
@@ -1123,14 +1149,14 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
1123
1149
|
if (response.error) {
|
|
1124
1150
|
throw new AuthError(this.mapAppleError(response.error), response.error, this.provider);
|
|
1125
1151
|
}
|
|
1126
|
-
if (response.state !== expectedState) {
|
|
1152
|
+
if (response.authorization.state !== expectedState) {
|
|
1127
1153
|
throw new AuthError(exports.AuthErrorCode.INVALID_STATE, 'OAuth state mismatch', this.provider);
|
|
1128
1154
|
}
|
|
1129
1155
|
// ID token validation with nonce would be done here
|
|
1130
1156
|
// For production, you should validate the ID token on your backend
|
|
1131
1157
|
}
|
|
1132
1158
|
createAppleAuthUser(response) {
|
|
1133
|
-
var _a, _b, _c
|
|
1159
|
+
var _a, _b, _c;
|
|
1134
1160
|
const user = response.user || {};
|
|
1135
1161
|
const idToken = (_a = response.authorization) === null || _a === void 0 ? void 0 : _a.id_token;
|
|
1136
1162
|
// Parse ID token claims (in production, do this on backend)
|
|
@@ -1155,7 +1181,7 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
1155
1181
|
tenantId: null,
|
|
1156
1182
|
providerData: [{
|
|
1157
1183
|
providerId: exports.AuthProvider.APPLE,
|
|
1158
|
-
uid: claims.sub || ((_c = response.authorization) === null || _c === void 0 ? void 0 : _c.code),
|
|
1184
|
+
uid: claims.sub || ((_c = response.authorization) === null || _c === void 0 ? void 0 : _c.code) || '',
|
|
1159
1185
|
displayName: user.name ? `${user.name.firstName || ''} ${user.name.lastName || ''}`.trim() : null,
|
|
1160
1186
|
email: user.email || claims.email || null,
|
|
1161
1187
|
phoneNumber: null,
|
|
@@ -1165,23 +1191,21 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
1165
1191
|
creationTime: new Date().toISOString(),
|
|
1166
1192
|
lastSignInTime: new Date().toISOString(),
|
|
1167
1193
|
},
|
|
1168
|
-
refreshToken:
|
|
1194
|
+
refreshToken: undefined, // Apple doesn't provide refresh tokens through JS SDK
|
|
1169
1195
|
};
|
|
1170
1196
|
}
|
|
1171
1197
|
createAppleCredential(response) {
|
|
1172
|
-
var _a, _b
|
|
1198
|
+
var _a, _b;
|
|
1173
1199
|
return {
|
|
1174
1200
|
providerId: exports.AuthProvider.APPLE,
|
|
1175
1201
|
signInMethod: 'oauth',
|
|
1176
|
-
accessToken:
|
|
1177
|
-
idToken: (
|
|
1178
|
-
refreshToken:
|
|
1179
|
-
expiresAt:
|
|
1180
|
-
? this.calculateTokenExpiry(response.authorization.expires_in)
|
|
1181
|
-
: undefined,
|
|
1202
|
+
accessToken: undefined, // Apple doesn't provide access tokens through JS SDK
|
|
1203
|
+
idToken: (_a = response.authorization) === null || _a === void 0 ? void 0 : _a.id_token,
|
|
1204
|
+
refreshToken: undefined, // Apple doesn't provide refresh tokens through JS SDK
|
|
1205
|
+
expiresAt: undefined,
|
|
1182
1206
|
tokenType: 'Bearer',
|
|
1183
|
-
scope:
|
|
1184
|
-
rawNonce: response.
|
|
1207
|
+
scope: undefined, // Apple doesn't provide scope info in JS SDK response
|
|
1208
|
+
rawNonce: (_b = response.authorization) === null || _b === void 0 ? void 0 : _b.state,
|
|
1185
1209
|
};
|
|
1186
1210
|
}
|
|
1187
1211
|
mapAppleError(error) {
|
|
@@ -1648,7 +1672,9 @@ var CapacitorAuthManager = (function (exports, core) {
|
|
|
1648
1672
|
CapacitorAuthManagerWeb: CapacitorAuthManagerWeb
|
|
1649
1673
|
});
|
|
1650
1674
|
|
|
1675
|
+
exports.AuthError = AuthError;
|
|
1651
1676
|
exports.CapacitorAuthManager = CapacitorAuthManager;
|
|
1677
|
+
exports.isAuthError = isAuthError;
|
|
1652
1678
|
|
|
1653
1679
|
return exports;
|
|
1654
1680
|
|