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