capacitor-auth-manager 2.1.0 → 2.3.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/CapacitorAuthManager.podspec +1 -1
- package/Readme.md +223 -103
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/AuthStorage.java +28 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/CapacitorAuthManager.java +109 -15
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/FacebookAuthProvider.java +389 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/GoogleAuthProvider.java +353 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/MicrosoftAuthProvider.java +455 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/ProviderFactory.java +180 -0
- package/dist/angular.cjs.js +7036 -0
- package/dist/angular.cjs.js.map +1 -0
- package/dist/core.cjs.js +2216 -486
- package/dist/core.cjs.js.map +1 -1
- package/dist/esm/angular/auth.guard.d.ts +18 -0
- package/dist/esm/angular/auth.guard.js +82 -0
- package/dist/esm/angular/auth.guard.js.map +1 -0
- package/dist/esm/angular/auth.module.d.ts +18 -0
- package/dist/esm/angular/auth.module.js +56 -0
- package/dist/esm/angular/auth.module.js.map +1 -0
- package/dist/esm/angular/auth.service.d.ts +108 -0
- package/dist/esm/angular/auth.service.js +224 -0
- package/dist/esm/angular/auth.service.js.map +1 -0
- package/dist/esm/angular/index.d.ts +3 -0
- package/dist/esm/angular/index.js +4 -0
- package/dist/esm/angular/index.js.map +1 -0
- package/dist/esm/core/auth-manager.d.ts +61 -1
- package/dist/esm/core/auth-manager.js +285 -24
- package/dist/esm/core/auth-manager.js.map +1 -1
- package/dist/esm/core/platform.d.ts +7 -0
- package/dist/esm/core/platform.js +30 -10
- package/dist/esm/core/platform.js.map +1 -1
- package/dist/esm/core/provider-manifests.d.ts +9 -0
- package/dist/esm/core/provider-manifests.js +471 -0
- package/dist/esm/core/provider-manifests.js.map +1 -0
- package/dist/esm/core/provider-registry.d.ts +19 -1
- package/dist/esm/core/provider-registry.js +72 -480
- package/dist/esm/core/provider-registry.js.map +1 -1
- package/dist/esm/core/types.d.ts +18 -7
- package/dist/esm/definitions.d.ts +42 -1
- package/dist/esm/definitions.js +28 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/providers/base-provider.d.ts +14 -3
- package/dist/esm/providers/base-provider.js +53 -1
- package/dist/esm/providers/base-provider.js.map +1 -1
- package/dist/esm/providers/oauth-provider.d.ts +23 -2
- package/dist/esm/providers/oauth-provider.js +134 -17
- package/dist/esm/providers/oauth-provider.js.map +1 -1
- package/dist/esm/providers/provider-factory.d.ts +3 -1
- package/dist/esm/providers/provider-factory.js +29 -41
- package/dist/esm/providers/provider-factory.js.map +1 -1
- package/dist/esm/providers/web/apple-provider.d.ts +2 -1
- package/dist/esm/providers/web/apple-provider.js +25 -9
- package/dist/esm/providers/web/apple-provider.js.map +1 -1
- package/dist/esm/providers/web/biometric-provider.d.ts +20 -7
- package/dist/esm/providers/web/biometric-provider.js +124 -23
- package/dist/esm/providers/web/biometric-provider.js.map +1 -1
- package/dist/esm/providers/web/email-code-provider.d.ts +46 -0
- package/dist/esm/providers/web/email-code-provider.js +294 -0
- package/dist/esm/providers/web/email-code-provider.js.map +1 -0
- package/dist/esm/providers/web/email-password-provider.d.ts +4 -7
- package/dist/esm/providers/web/email-password-provider.js +38 -38
- package/dist/esm/providers/web/email-password-provider.js.map +1 -1
- package/dist/esm/providers/web/facebook-provider.d.ts +52 -1
- package/dist/esm/providers/web/facebook-provider.js +2 -2
- package/dist/esm/providers/web/facebook-provider.js.map +1 -1
- package/dist/esm/providers/web/firebase-provider.d.ts +5 -3
- package/dist/esm/providers/web/firebase-provider.js +74 -42
- package/dist/esm/providers/web/firebase-provider.js.map +1 -1
- package/dist/esm/providers/web/github-provider.d.ts +9 -0
- package/dist/esm/providers/web/github-provider.js +91 -14
- package/dist/esm/providers/web/github-provider.js.map +1 -1
- package/dist/esm/providers/web/index.d.ts +2 -0
- package/dist/esm/providers/web/index.js +2 -0
- package/dist/esm/providers/web/index.js.map +1 -1
- package/dist/esm/providers/web/linkedin-provider.d.ts +2 -3
- package/dist/esm/providers/web/linkedin-provider.js +40 -14
- package/dist/esm/providers/web/linkedin-provider.js.map +1 -1
- package/dist/esm/providers/web/magic-link-provider.d.ts +4 -6
- package/dist/esm/providers/web/magic-link-provider.js +49 -36
- package/dist/esm/providers/web/magic-link-provider.js.map +1 -1
- package/dist/esm/providers/web/microsoft-provider.d.ts +2 -0
- package/dist/esm/providers/web/microsoft-provider.js +45 -20
- package/dist/esm/providers/web/microsoft-provider.js.map +1 -1
- package/dist/esm/providers/web/phone-password-provider.d.ts +63 -0
- package/dist/esm/providers/web/phone-password-provider.js +440 -0
- package/dist/esm/providers/web/phone-password-provider.js.map +1 -0
- package/dist/esm/providers/web/slack-provider.d.ts +2 -3
- package/dist/esm/providers/web/slack-provider.js +38 -14
- package/dist/esm/providers/web/slack-provider.js.map +1 -1
- package/dist/esm/providers/web/sms-provider.d.ts +4 -6
- package/dist/esm/providers/web/sms-provider.js +33 -28
- package/dist/esm/providers/web/sms-provider.js.map +1 -1
- package/dist/esm/providers/web/username-password-provider.d.ts +4 -7
- package/dist/esm/providers/web/username-password-provider.js +32 -30
- package/dist/esm/providers/web/username-password-provider.js.map +1 -1
- package/dist/esm/react/hooks.d.ts +110 -0
- package/dist/esm/react/hooks.js +307 -0
- package/dist/esm/react/hooks.js.map +1 -0
- package/dist/esm/react/index.d.ts +2 -0
- package/dist/esm/react/index.js +3 -0
- package/dist/esm/react/index.js.map +1 -0
- package/dist/esm/react/types.d.ts +30 -0
- package/dist/esm/react/types.js +2 -0
- package/dist/esm/react/types.js.map +1 -0
- package/dist/esm/utils/auth-error.d.ts +7 -0
- package/dist/esm/utils/auth-error.js +31 -5
- package/dist/esm/utils/auth-error.js.map +1 -1
- package/dist/esm/utils/event-emitter.d.ts +7 -0
- package/dist/esm/utils/event-emitter.js +8 -2
- package/dist/esm/utils/event-emitter.js.map +1 -1
- package/dist/esm/utils/logger.d.ts +28 -1
- package/dist/esm/utils/logger.js +115 -1
- package/dist/esm/utils/logger.js.map +1 -1
- package/dist/esm/utils/pending-verification-store.d.ts +59 -0
- package/dist/esm/utils/pending-verification-store.js +103 -0
- package/dist/esm/utils/pending-verification-store.js.map +1 -0
- package/dist/esm/utils/storage.d.ts +29 -1
- package/dist/esm/utils/storage.js +64 -5
- package/dist/esm/utils/storage.js.map +1 -1
- package/dist/esm/vue/composables.d.ts +124 -0
- package/dist/esm/vue/composables.js +290 -0
- package/dist/esm/vue/composables.js.map +1 -0
- package/dist/esm/vue/index.d.ts +1 -0
- package/dist/esm/vue/index.js +2 -0
- package/dist/esm/vue/index.js.map +1 -0
- package/dist/esm/web.d.ts +32 -7
- package/dist/esm/web.js +247 -52
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +2674 -719
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +2674 -719
- package/dist/plugin.js.map +1 -1
- package/dist/react.cjs.js +6991 -0
- package/dist/react.cjs.js.map +1 -0
- package/dist/vue.cjs.js +6974 -0
- package/dist/vue.cjs.js.map +1 -0
- package/ios/Plugin/AppleAuthProvider.swift +288 -0
- package/ios/Plugin/AuthStorage.swift +49 -1
- package/ios/Plugin/CapacitorAuthManager.swift +45 -23
- package/ios/Plugin/FacebookAuthProvider.swift +297 -0
- package/ios/Plugin/GitHubAuthProvider.swift +54 -0
- package/ios/Plugin/GoogleAuthProvider.swift +265 -0
- package/ios/Plugin/LinkedInAuthProvider.swift +63 -0
- package/ios/Plugin/MicrosoftAuthProvider.swift +315 -0
- package/ios/Plugin/Models.swift +4 -1
- package/ios/Plugin/OAuthWebProvider.swift +415 -0
- package/ios/Plugin/SlackAuthProvider.swift +108 -0
- package/package.json +61 -40
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { AuthResult, AuthUser, SignInOptions, RefreshTokenOptions } from '../../definitions';
|
|
2
|
+
import { BaseAuthProvider, BaseProviderConfig } from '../base-provider';
|
|
3
|
+
export interface EmailCodeConfig {
|
|
4
|
+
sendCodeUrl: string;
|
|
5
|
+
verifyCodeUrl: string;
|
|
6
|
+
clientId?: string;
|
|
7
|
+
codeLength?: number;
|
|
8
|
+
resendDelay?: number;
|
|
9
|
+
codeExpiration?: number;
|
|
10
|
+
}
|
|
11
|
+
interface EmailCodeOptions extends SignInOptions {
|
|
12
|
+
email: string;
|
|
13
|
+
code?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class EmailCodeProvider extends BaseAuthProvider {
|
|
16
|
+
get name(): string;
|
|
17
|
+
private config;
|
|
18
|
+
private pendingVerifications;
|
|
19
|
+
private readonly MAX_ATTEMPTS;
|
|
20
|
+
constructor(input: BaseProviderConfig | EmailCodeConfig);
|
|
21
|
+
signIn(options?: EmailCodeOptions): Promise<AuthResult>;
|
|
22
|
+
private sendCode;
|
|
23
|
+
private verifyCode;
|
|
24
|
+
signOut(): Promise<void>;
|
|
25
|
+
getCurrentUser(): Promise<AuthUser | null>;
|
|
26
|
+
refreshToken(_options?: RefreshTokenOptions): Promise<AuthResult>;
|
|
27
|
+
initialize(): Promise<void>;
|
|
28
|
+
isSupported(): Promise<boolean>;
|
|
29
|
+
revokeAccess(_token?: string): Promise<void>;
|
|
30
|
+
resendCode(email: string): Promise<AuthResult>;
|
|
31
|
+
canResendCode(email: string): {
|
|
32
|
+
canResend: boolean;
|
|
33
|
+
waitTime?: number;
|
|
34
|
+
};
|
|
35
|
+
private isValidEmail;
|
|
36
|
+
private maskEmail;
|
|
37
|
+
private generateUid;
|
|
38
|
+
}
|
|
39
|
+
export declare const EmailCodeProviderManifest: {
|
|
40
|
+
name: string;
|
|
41
|
+
displayName: string;
|
|
42
|
+
iconUrl: string;
|
|
43
|
+
description: string;
|
|
44
|
+
setupInstructions: string;
|
|
45
|
+
};
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { AuthErrorCode, AuthProvider, } from '../../definitions';
|
|
2
|
+
import { AuthError } from '../../utils/auth-error';
|
|
3
|
+
import { BaseAuthProvider, resolveProviderConfig, } from '../base-provider';
|
|
4
|
+
import { PendingVerificationStore } from '../../utils/pending-verification-store';
|
|
5
|
+
export class EmailCodeProvider extends BaseAuthProvider {
|
|
6
|
+
get name() {
|
|
7
|
+
return 'email-code';
|
|
8
|
+
}
|
|
9
|
+
constructor(input) {
|
|
10
|
+
super(resolveProviderConfig(AuthProvider.EMAIL_CODE, input));
|
|
11
|
+
// Pending verifications are persisted (SSR-guarded localStorage) so they survive a reload /
|
|
12
|
+
// page change between sending and verifying the code. Only non-secret metadata is stored —
|
|
13
|
+
// never the verification code itself (see PendingVerificationStore + F-11).
|
|
14
|
+
this.pendingVerifications = new PendingVerificationStore('email-code');
|
|
15
|
+
this.MAX_ATTEMPTS = 3;
|
|
16
|
+
this.config = Object.assign({ codeLength: 6, resendDelay: 60000, codeExpiration: 10 * 60 * 1000 }, this.options);
|
|
17
|
+
}
|
|
18
|
+
async signIn(options) {
|
|
19
|
+
if (!(options === null || options === void 0 ? void 0 : options.email)) {
|
|
20
|
+
throw new AuthError(AuthErrorCode.EMAIL_REQUIRED, 'Email is required for email code authentication');
|
|
21
|
+
}
|
|
22
|
+
// Validate email format
|
|
23
|
+
if (!this.isValidEmail(options.email)) {
|
|
24
|
+
throw new AuthError(AuthErrorCode.INVALID_EMAIL, 'Please enter a valid email address');
|
|
25
|
+
}
|
|
26
|
+
// If code is provided, verify it
|
|
27
|
+
if (options.code) {
|
|
28
|
+
return this.verifyCode(options.email, options.code);
|
|
29
|
+
}
|
|
30
|
+
// Otherwise, send code
|
|
31
|
+
return this.sendCode(options.email);
|
|
32
|
+
}
|
|
33
|
+
async sendCode(email) {
|
|
34
|
+
try {
|
|
35
|
+
const normalizedEmail = email.toLowerCase().trim();
|
|
36
|
+
// Check if we can resend
|
|
37
|
+
const existing = this.pendingVerifications.get(normalizedEmail);
|
|
38
|
+
if (existing && existing.lastResent) {
|
|
39
|
+
const timeSinceLastResend = Date.now() - existing.lastResent;
|
|
40
|
+
if (timeSinceLastResend < this.config.resendDelay) {
|
|
41
|
+
const waitTime = Math.ceil((this.config.resendDelay - timeSinceLastResend) / 1000);
|
|
42
|
+
throw new AuthError(AuthErrorCode.RESEND_DELAY, `Please wait ${waitTime} seconds before requesting a new code`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Send email code via backend
|
|
46
|
+
const response = await fetch(this.config.sendCodeUrl, {
|
|
47
|
+
method: 'POST',
|
|
48
|
+
headers: {
|
|
49
|
+
'Content-Type': 'application/json',
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify({
|
|
52
|
+
email: normalizedEmail,
|
|
53
|
+
clientId: this.config.clientId,
|
|
54
|
+
codeLength: this.config.codeLength,
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
const error = await response.json().catch(() => ({}));
|
|
59
|
+
throw new AuthError(AuthErrorCode.SEND_CODE_FAILED, error.message || 'Failed to send verification code');
|
|
60
|
+
}
|
|
61
|
+
const data = await response.json();
|
|
62
|
+
// Store pending verification (persisted; metadata only, never the code).
|
|
63
|
+
this.pendingVerifications.set(normalizedEmail, {
|
|
64
|
+
identifier: normalizedEmail,
|
|
65
|
+
sessionId: data.sessionId,
|
|
66
|
+
expires: Date.now() + this.config.codeExpiration,
|
|
67
|
+
attempts: 0,
|
|
68
|
+
lastResent: Date.now(),
|
|
69
|
+
});
|
|
70
|
+
// Return partial auth result for pending state
|
|
71
|
+
const tempUser = {
|
|
72
|
+
uid: `email-code-pending:${data.sessionId}`,
|
|
73
|
+
email: normalizedEmail,
|
|
74
|
+
displayName: normalizedEmail.split('@')[0],
|
|
75
|
+
photoURL: null,
|
|
76
|
+
phoneNumber: null,
|
|
77
|
+
emailVerified: false,
|
|
78
|
+
providerData: [],
|
|
79
|
+
metadata: {
|
|
80
|
+
creationTime: new Date().toISOString(),
|
|
81
|
+
lastSignInTime: new Date().toISOString(),
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
user: tempUser,
|
|
86
|
+
credential: {
|
|
87
|
+
providerId: this.name,
|
|
88
|
+
signInMethod: 'email-code',
|
|
89
|
+
accessToken: data.sessionId,
|
|
90
|
+
},
|
|
91
|
+
additionalUserInfo: {
|
|
92
|
+
isNewUser: false,
|
|
93
|
+
providerId: this.name,
|
|
94
|
+
profile: {
|
|
95
|
+
email: normalizedEmail,
|
|
96
|
+
sessionId: data.sessionId,
|
|
97
|
+
message: `Verification code sent to ${this.maskEmail(normalizedEmail)}`,
|
|
98
|
+
pending: true,
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error instanceof AuthError)
|
|
105
|
+
throw error;
|
|
106
|
+
throw new AuthError(AuthErrorCode.SEND_CODE_FAILED, error.message || 'Failed to send verification code');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async verifyCode(email, code) {
|
|
110
|
+
const normalizedEmail = email.toLowerCase().trim();
|
|
111
|
+
const pending = this.pendingVerifications.get(normalizedEmail);
|
|
112
|
+
if (!pending) {
|
|
113
|
+
throw new AuthError(AuthErrorCode.NO_PENDING_VERIFICATION, 'No pending email verification found. Please request a new code.');
|
|
114
|
+
}
|
|
115
|
+
if (Date.now() > pending.expires) {
|
|
116
|
+
this.pendingVerifications.delete(normalizedEmail);
|
|
117
|
+
throw new AuthError(AuthErrorCode.CODE_EXPIRED, 'Verification code has expired');
|
|
118
|
+
}
|
|
119
|
+
if (pending.attempts >= this.MAX_ATTEMPTS) {
|
|
120
|
+
this.pendingVerifications.delete(normalizedEmail);
|
|
121
|
+
throw new AuthError(AuthErrorCode.TOO_MANY_ATTEMPTS, 'Too many failed attempts. Please request a new code.');
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
// Increment attempts and persist the bump (so the count survives a reload mid-flow).
|
|
125
|
+
pending.attempts++;
|
|
126
|
+
this.pendingVerifications.set(normalizedEmail, pending);
|
|
127
|
+
// Verify code with backend
|
|
128
|
+
const response = await fetch(this.config.verifyCodeUrl, {
|
|
129
|
+
method: 'POST',
|
|
130
|
+
headers: {
|
|
131
|
+
'Content-Type': 'application/json',
|
|
132
|
+
},
|
|
133
|
+
body: JSON.stringify({
|
|
134
|
+
email: normalizedEmail,
|
|
135
|
+
code,
|
|
136
|
+
sessionId: pending.sessionId,
|
|
137
|
+
clientId: this.config.clientId,
|
|
138
|
+
}),
|
|
139
|
+
});
|
|
140
|
+
if (!response.ok) {
|
|
141
|
+
const error = await response.json().catch(() => ({}));
|
|
142
|
+
if (response.status === 401) {
|
|
143
|
+
throw new AuthError(AuthErrorCode.INVALID_CODE, 'Invalid verification code');
|
|
144
|
+
}
|
|
145
|
+
throw new AuthError(AuthErrorCode.VERIFICATION_FAILED, error.message || 'Failed to verify code');
|
|
146
|
+
}
|
|
147
|
+
const data = await response.json();
|
|
148
|
+
// Create user from verified data
|
|
149
|
+
const user = {
|
|
150
|
+
uid: data.uid || this.generateUid(normalizedEmail),
|
|
151
|
+
email: normalizedEmail,
|
|
152
|
+
displayName: data.displayName || normalizedEmail.split('@')[0],
|
|
153
|
+
photoURL: data.photoURL || null,
|
|
154
|
+
phoneNumber: data.phoneNumber || null,
|
|
155
|
+
emailVerified: true,
|
|
156
|
+
providerData: [
|
|
157
|
+
{
|
|
158
|
+
providerId: this.name,
|
|
159
|
+
uid: data.uid || normalizedEmail,
|
|
160
|
+
displayName: data.displayName || normalizedEmail.split('@')[0],
|
|
161
|
+
email: normalizedEmail,
|
|
162
|
+
phoneNumber: null,
|
|
163
|
+
photoURL: data.photoURL || null,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
metadata: {
|
|
167
|
+
creationTime: data.createdAt || new Date().toISOString(),
|
|
168
|
+
lastSignInTime: new Date().toISOString(),
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
// Clean up pending verification
|
|
172
|
+
this.pendingVerifications.delete(normalizedEmail);
|
|
173
|
+
return {
|
|
174
|
+
user,
|
|
175
|
+
credential: {
|
|
176
|
+
providerId: this.name,
|
|
177
|
+
signInMethod: 'email-code',
|
|
178
|
+
accessToken: data.accessToken,
|
|
179
|
+
refreshToken: data.refreshToken,
|
|
180
|
+
expiresAt: data.expiresAt,
|
|
181
|
+
},
|
|
182
|
+
additionalUserInfo: {
|
|
183
|
+
isNewUser: data.isNewUser || false,
|
|
184
|
+
providerId: this.name,
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
if (error instanceof AuthError)
|
|
190
|
+
throw error;
|
|
191
|
+
throw new AuthError(AuthErrorCode.VERIFICATION_FAILED, error.message || 'Failed to verify code');
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async signOut() {
|
|
195
|
+
this.pendingVerifications.clear();
|
|
196
|
+
}
|
|
197
|
+
async getCurrentUser() {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
async refreshToken(_options) {
|
|
201
|
+
// Email code authentication is single-use and has no refresh flow; the user must request
|
|
202
|
+
// a new verification code rather than refresh a token.
|
|
203
|
+
throw new AuthError(AuthErrorCode.OPERATION_NOT_ALLOWED, 'Email code authentication does not support token refresh; request a new code instead.');
|
|
204
|
+
}
|
|
205
|
+
async initialize() {
|
|
206
|
+
// Email code provider doesn't require initialization
|
|
207
|
+
}
|
|
208
|
+
async isSupported() {
|
|
209
|
+
return !!(this.config.sendCodeUrl && this.config.verifyCodeUrl);
|
|
210
|
+
}
|
|
211
|
+
// linkAccount + unlinkAccount inherit BaseAuthProvider's OPERATION_NOT_ALLOWED defaults (F-44).
|
|
212
|
+
async revokeAccess(_token) {
|
|
213
|
+
this.pendingVerifications.clear();
|
|
214
|
+
}
|
|
215
|
+
// Public method to resend code
|
|
216
|
+
async resendCode(email) {
|
|
217
|
+
return this.sendCode(email);
|
|
218
|
+
}
|
|
219
|
+
// Public method to check if can resend
|
|
220
|
+
canResendCode(email) {
|
|
221
|
+
const normalizedEmail = email.toLowerCase().trim();
|
|
222
|
+
const pending = this.pendingVerifications.get(normalizedEmail);
|
|
223
|
+
if (!pending || !pending.lastResent) {
|
|
224
|
+
return { canResend: true };
|
|
225
|
+
}
|
|
226
|
+
const timeSinceLastResend = Date.now() - pending.lastResent;
|
|
227
|
+
if (timeSinceLastResend < this.config.resendDelay) {
|
|
228
|
+
return {
|
|
229
|
+
canResend: false,
|
|
230
|
+
waitTime: Math.ceil((this.config.resendDelay - timeSinceLastResend) / 1000),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
return { canResend: true };
|
|
234
|
+
}
|
|
235
|
+
isValidEmail(email) {
|
|
236
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
237
|
+
return emailRegex.test(email);
|
|
238
|
+
}
|
|
239
|
+
maskEmail(email) {
|
|
240
|
+
const [local, domain] = email.split('@');
|
|
241
|
+
if (local.length <= 2) {
|
|
242
|
+
return `${local[0]}***@${domain}`;
|
|
243
|
+
}
|
|
244
|
+
return `${local[0]}***${local[local.length - 1]}@${domain}`;
|
|
245
|
+
}
|
|
246
|
+
generateUid(email) {
|
|
247
|
+
return `email-code:${email.toLowerCase().replace(/[^a-z0-9]/g, '-')}`;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
// Provider manifest for dynamic loading
|
|
251
|
+
export const EmailCodeProviderManifest = {
|
|
252
|
+
name: 'email-code',
|
|
253
|
+
displayName: 'Email Verification Code',
|
|
254
|
+
iconUrl: 'https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/mail.svg',
|
|
255
|
+
description: 'Passwordless authentication via email verification code',
|
|
256
|
+
setupInstructions: `
|
|
257
|
+
To use Email Code authentication:
|
|
258
|
+
|
|
259
|
+
1. Set up backend endpoints:
|
|
260
|
+
- Send code endpoint: POST request to send verification email
|
|
261
|
+
- Verify code endpoint: POST request to verify the code
|
|
262
|
+
|
|
263
|
+
2. Configure the provider:
|
|
264
|
+
\`\`\`javascript
|
|
265
|
+
auth.configure({
|
|
266
|
+
providers: {
|
|
267
|
+
email_code: {
|
|
268
|
+
sendCodeUrl: 'https://your-api.com/email/send-code',
|
|
269
|
+
verifyCodeUrl: 'https://your-api.com/email/verify-code',
|
|
270
|
+
codeLength: 6, // Code length
|
|
271
|
+
resendDelay: 60000 // Minimum time between resends (ms)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
\`\`\`
|
|
276
|
+
|
|
277
|
+
3. Send verification code:
|
|
278
|
+
\`\`\`javascript
|
|
279
|
+
const result = await auth.signIn('email_code', { email: 'user@example.com' });
|
|
280
|
+
// Returns pending state with sessionId
|
|
281
|
+
\`\`\`
|
|
282
|
+
|
|
283
|
+
4. Verify code:
|
|
284
|
+
\`\`\`javascript
|
|
285
|
+
const result = await auth.signIn('email_code', {
|
|
286
|
+
email: 'user@example.com',
|
|
287
|
+
code: '123456'
|
|
288
|
+
});
|
|
289
|
+
\`\`\`
|
|
290
|
+
|
|
291
|
+
Note: This provider requires a backend service to send emails.
|
|
292
|
+
`,
|
|
293
|
+
};
|
|
294
|
+
//# sourceMappingURL=email-code-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-code-provider.js","sourceRoot":"","sources":["../../../../src/providers/web/email-code-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EAGb,YAAY,GACb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EACL,gBAAgB,EAEhB,qBAAqB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,wBAAwB,EAAE,MAAM,wCAAwC,CA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C;QAED,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAC7B,CAAC;IAEO,YAAY,CAAC,KAAa;QAChC,MAAM,UAAU,GAAG,4BAA4B,CAAC;QAChD,OAAO,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC;IAEO,SAAS,CAAC,KAAa;QAC7B,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACzC,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;YACtB,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,OAAO,MAAM,EAAE,CAAC;QACpC,CAAC;QACD,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC;IAC9D,CAAC;IAEO,WAAW,CAAC,KAAa;QAC/B,OAAO,cAAc,KAAK,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,YAAY,EAAE,GAAG,CAAC,EAAE,CAAC;IACxE,CAAC;CACF;AAED,wCAAwC;AACxC,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACvC,IAAI,EAAE,YAAY;IAClB,WAAW,EAAE,yBAAyB;IACtC,OAAO,EAAE,6DAA6D;IACtE,WAAW,EAAE,yDAAyD;IACtE,iBAAiB,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoCpB;CACA,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AuthResult, AuthUser, SignInOptions, SignUpOptions, RefreshTokenOptions } from '../../definitions';
|
|
2
|
-
import {
|
|
2
|
+
import { BaseAuthProvider, BaseProviderConfig } from '../base-provider';
|
|
3
3
|
export interface EmailPasswordConfig {
|
|
4
4
|
apiUrl: string;
|
|
5
5
|
clientId?: string;
|
|
@@ -22,13 +22,12 @@ interface EmailPasswordSignUpOptions extends SignUpOptions {
|
|
|
22
22
|
displayName?: string;
|
|
23
23
|
photoURL?: string;
|
|
24
24
|
}
|
|
25
|
-
export declare class EmailPasswordProvider
|
|
26
|
-
name: string;
|
|
25
|
+
export declare class EmailPasswordProvider extends BaseAuthProvider {
|
|
26
|
+
get name(): string;
|
|
27
27
|
private config;
|
|
28
|
-
private currentUser;
|
|
29
28
|
private authToken;
|
|
30
29
|
private refreshTokenValue;
|
|
31
|
-
constructor(
|
|
30
|
+
constructor(input: BaseProviderConfig | EmailPasswordConfig);
|
|
32
31
|
signIn(options?: EmailPasswordSignInOptions): Promise<AuthResult>;
|
|
33
32
|
signUp(options?: EmailPasswordSignUpOptions): Promise<AuthResult>;
|
|
34
33
|
signOut(): Promise<void>;
|
|
@@ -41,8 +40,6 @@ export declare class EmailPasswordProvider implements AuthProviderInterface {
|
|
|
41
40
|
private validatePassword;
|
|
42
41
|
initialize(): Promise<void>;
|
|
43
42
|
isSupported(): Promise<boolean>;
|
|
44
|
-
linkAccount(_options: any): Promise<AuthResult>;
|
|
45
|
-
unlinkAccount(_options: any): Promise<void>;
|
|
46
43
|
revokeAccess(_token?: string): Promise<void>;
|
|
47
44
|
}
|
|
48
45
|
export declare const EmailPasswordProviderManifest: {
|
|
@@ -1,22 +1,27 @@
|
|
|
1
|
+
import { AuthErrorCode, AuthProvider, } from '../../definitions';
|
|
1
2
|
import { AuthError } from '../../utils/auth-error';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import { BaseAuthProvider, resolveProviderConfig, } from '../base-provider';
|
|
4
|
+
export class EmailPasswordProvider extends BaseAuthProvider {
|
|
5
|
+
get name() {
|
|
6
|
+
return 'email-password';
|
|
7
|
+
}
|
|
8
|
+
constructor(input) {
|
|
9
|
+
super(resolveProviderConfig(AuthProvider.EMAIL_PASSWORD, input));
|
|
10
|
+
// currentUser is the inherited (in-memory) field from BaseAuthProvider; assigned directly here
|
|
11
|
+
// so email/password sessions stay in-memory exactly as before (no new storage persistence).
|
|
6
12
|
this.authToken = null;
|
|
7
13
|
this.refreshTokenValue = null;
|
|
8
|
-
// Initialize with config
|
|
9
14
|
this.config = Object.assign({ allowSignUp: true, passwordRequirements: {
|
|
10
15
|
minLength: 8,
|
|
11
16
|
requireUppercase: true,
|
|
12
17
|
requireLowercase: true,
|
|
13
18
|
requireNumbers: true,
|
|
14
19
|
requireSpecialChars: false,
|
|
15
|
-
} },
|
|
20
|
+
} }, this.options);
|
|
16
21
|
}
|
|
17
22
|
async signIn(options) {
|
|
18
23
|
if (!(options === null || options === void 0 ? void 0 : options.email) || !(options === null || options === void 0 ? void 0 : options.password)) {
|
|
19
|
-
throw new AuthError(
|
|
24
|
+
throw new AuthError(AuthErrorCode.CREDENTIALS_REQUIRED, 'Email and password are required');
|
|
20
25
|
}
|
|
21
26
|
try {
|
|
22
27
|
const response = await fetch(`${this.config.apiUrl}/auth/signin`, {
|
|
@@ -33,9 +38,9 @@ export class EmailPasswordProvider {
|
|
|
33
38
|
if (!response.ok) {
|
|
34
39
|
const error = await response.json().catch(() => ({}));
|
|
35
40
|
if (response.status === 401) {
|
|
36
|
-
throw new AuthError(
|
|
41
|
+
throw new AuthError(AuthErrorCode.INVALID_CREDENTIALS, 'Invalid email or password');
|
|
37
42
|
}
|
|
38
|
-
throw new AuthError(
|
|
43
|
+
throw new AuthError(AuthErrorCode.SIGN_IN_FAILED, error.message || 'Failed to sign in');
|
|
39
44
|
}
|
|
40
45
|
const data = await response.json();
|
|
41
46
|
// Create user object
|
|
@@ -82,24 +87,24 @@ export class EmailPasswordProvider {
|
|
|
82
87
|
catch (error) {
|
|
83
88
|
if (error instanceof AuthError)
|
|
84
89
|
throw error;
|
|
85
|
-
throw new AuthError(
|
|
90
|
+
throw new AuthError(AuthErrorCode.SIGN_IN_FAILED, error.message || 'Failed to sign in with email and password');
|
|
86
91
|
}
|
|
87
92
|
}
|
|
88
93
|
async signUp(options) {
|
|
89
94
|
if (!this.config.allowSignUp) {
|
|
90
|
-
throw new AuthError(
|
|
95
|
+
throw new AuthError(AuthErrorCode.SIGN_UP_DISABLED, 'Sign up is not allowed');
|
|
91
96
|
}
|
|
92
97
|
if (!(options === null || options === void 0 ? void 0 : options.email) || !(options === null || options === void 0 ? void 0 : options.password)) {
|
|
93
|
-
throw new AuthError(
|
|
98
|
+
throw new AuthError(AuthErrorCode.CREDENTIALS_REQUIRED, 'Email and password are required');
|
|
94
99
|
}
|
|
95
100
|
// Validate email
|
|
96
101
|
if (!this.isValidEmail(options.email)) {
|
|
97
|
-
throw new AuthError(
|
|
102
|
+
throw new AuthError(AuthErrorCode.INVALID_EMAIL, 'Invalid email address');
|
|
98
103
|
}
|
|
99
104
|
// Validate password
|
|
100
105
|
const passwordError = this.validatePassword(options.password);
|
|
101
106
|
if (passwordError) {
|
|
102
|
-
throw new AuthError(
|
|
107
|
+
throw new AuthError(AuthErrorCode.WEAK_PASSWORD, passwordError);
|
|
103
108
|
}
|
|
104
109
|
try {
|
|
105
110
|
const response = await fetch(`${this.config.apiUrl}/auth/signup`, {
|
|
@@ -118,9 +123,9 @@ export class EmailPasswordProvider {
|
|
|
118
123
|
if (!response.ok) {
|
|
119
124
|
const error = await response.json().catch(() => ({}));
|
|
120
125
|
if (response.status === 409) {
|
|
121
|
-
throw new AuthError(
|
|
126
|
+
throw new AuthError(AuthErrorCode.EMAIL_ALREADY_IN_USE, 'Email already in use');
|
|
122
127
|
}
|
|
123
|
-
throw new AuthError(
|
|
128
|
+
throw new AuthError(AuthErrorCode.SIGN_UP_FAILED, error.message || 'Failed to create account');
|
|
124
129
|
}
|
|
125
130
|
const data = await response.json();
|
|
126
131
|
// Create user object
|
|
@@ -167,7 +172,7 @@ export class EmailPasswordProvider {
|
|
|
167
172
|
catch (error) {
|
|
168
173
|
if (error instanceof AuthError)
|
|
169
174
|
throw error;
|
|
170
|
-
throw new AuthError(
|
|
175
|
+
throw new AuthError(AuthErrorCode.SIGN_UP_FAILED, error.message || 'Failed to create account');
|
|
171
176
|
}
|
|
172
177
|
}
|
|
173
178
|
async signOut() {
|
|
@@ -197,7 +202,7 @@ export class EmailPasswordProvider {
|
|
|
197
202
|
}
|
|
198
203
|
async refreshToken(_options) {
|
|
199
204
|
if (!this.refreshTokenValue) {
|
|
200
|
-
throw new AuthError(
|
|
205
|
+
throw new AuthError(AuthErrorCode.NO_REFRESH_TOKEN, 'No refresh token available');
|
|
201
206
|
}
|
|
202
207
|
try {
|
|
203
208
|
const response = await fetch(`${this.config.apiUrl}/auth/refresh`, {
|
|
@@ -211,7 +216,7 @@ export class EmailPasswordProvider {
|
|
|
211
216
|
}),
|
|
212
217
|
});
|
|
213
218
|
if (!response.ok) {
|
|
214
|
-
throw new AuthError(
|
|
219
|
+
throw new AuthError(AuthErrorCode.TOKEN_REFRESH_FAILED, 'Failed to refresh token');
|
|
215
220
|
}
|
|
216
221
|
const data = await response.json();
|
|
217
222
|
// Update tokens
|
|
@@ -233,17 +238,17 @@ export class EmailPasswordProvider {
|
|
|
233
238
|
catch (error) {
|
|
234
239
|
if (error instanceof AuthError)
|
|
235
240
|
throw error;
|
|
236
|
-
throw new AuthError(
|
|
241
|
+
throw new AuthError(AuthErrorCode.TOKEN_REFRESH_FAILED, error.message || 'Failed to refresh token');
|
|
237
242
|
}
|
|
238
243
|
}
|
|
239
244
|
async updatePassword(currentPassword, newPassword) {
|
|
240
245
|
if (!this.currentUser || !this.authToken) {
|
|
241
|
-
throw new AuthError(
|
|
246
|
+
throw new AuthError(AuthErrorCode.NOT_AUTHENTICATED, 'User not authenticated');
|
|
242
247
|
}
|
|
243
248
|
// Validate new password
|
|
244
249
|
const passwordError = this.validatePassword(newPassword);
|
|
245
250
|
if (passwordError) {
|
|
246
|
-
throw new AuthError(
|
|
251
|
+
throw new AuthError(AuthErrorCode.WEAK_PASSWORD, passwordError);
|
|
247
252
|
}
|
|
248
253
|
try {
|
|
249
254
|
const response = await fetch(`${this.config.apiUrl}/auth/update-password`, {
|
|
@@ -261,20 +266,20 @@ export class EmailPasswordProvider {
|
|
|
261
266
|
if (!response.ok) {
|
|
262
267
|
const error = await response.json().catch(() => ({}));
|
|
263
268
|
if (response.status === 401) {
|
|
264
|
-
throw new AuthError(
|
|
269
|
+
throw new AuthError(AuthErrorCode.WRONG_PASSWORD, 'Current password is incorrect');
|
|
265
270
|
}
|
|
266
|
-
throw new AuthError(
|
|
271
|
+
throw new AuthError(AuthErrorCode.SERVER_ERROR, error.message || 'Failed to update password');
|
|
267
272
|
}
|
|
268
273
|
}
|
|
269
274
|
catch (error) {
|
|
270
275
|
if (error instanceof AuthError)
|
|
271
276
|
throw error;
|
|
272
|
-
throw new AuthError(
|
|
277
|
+
throw new AuthError(AuthErrorCode.SERVER_ERROR, error.message || 'Failed to update password');
|
|
273
278
|
}
|
|
274
279
|
}
|
|
275
280
|
async sendPasswordResetEmail(email) {
|
|
276
281
|
if (!this.isValidEmail(email)) {
|
|
277
|
-
throw new AuthError(
|
|
282
|
+
throw new AuthError(AuthErrorCode.INVALID_EMAIL, 'Invalid email address');
|
|
278
283
|
}
|
|
279
284
|
try {
|
|
280
285
|
const response = await fetch(`${this.config.apiUrl}/auth/reset-password`, {
|
|
@@ -289,21 +294,21 @@ export class EmailPasswordProvider {
|
|
|
289
294
|
});
|
|
290
295
|
if (!response.ok) {
|
|
291
296
|
const error = await response.json().catch(() => ({}));
|
|
292
|
-
throw new AuthError(
|
|
297
|
+
throw new AuthError(AuthErrorCode.SERVER_ERROR, error.message || 'Failed to send reset email');
|
|
293
298
|
}
|
|
294
299
|
}
|
|
295
300
|
catch (error) {
|
|
296
301
|
if (error instanceof AuthError)
|
|
297
302
|
throw error;
|
|
298
|
-
throw new AuthError(
|
|
303
|
+
throw new AuthError(AuthErrorCode.SERVER_ERROR, error.message || 'Failed to send password reset email');
|
|
299
304
|
}
|
|
300
305
|
}
|
|
301
306
|
async sendEmailVerification() {
|
|
302
307
|
if (!this.currentUser || !this.authToken) {
|
|
303
|
-
throw new AuthError(
|
|
308
|
+
throw new AuthError(AuthErrorCode.NOT_AUTHENTICATED, 'User not authenticated');
|
|
304
309
|
}
|
|
305
310
|
if (this.currentUser.emailVerified) {
|
|
306
|
-
throw new AuthError(
|
|
311
|
+
throw new AuthError(AuthErrorCode.EMAIL_ALREADY_VERIFIED, 'Email already verified');
|
|
307
312
|
}
|
|
308
313
|
try {
|
|
309
314
|
const response = await fetch(`${this.config.apiUrl}/auth/verify-email`, {
|
|
@@ -318,13 +323,13 @@ export class EmailPasswordProvider {
|
|
|
318
323
|
});
|
|
319
324
|
if (!response.ok) {
|
|
320
325
|
const error = await response.json().catch(() => ({}));
|
|
321
|
-
throw new AuthError(
|
|
326
|
+
throw new AuthError(AuthErrorCode.VERIFICATION_FAILED, error.message || 'Failed to send verification email');
|
|
322
327
|
}
|
|
323
328
|
}
|
|
324
329
|
catch (error) {
|
|
325
330
|
if (error instanceof AuthError)
|
|
326
331
|
throw error;
|
|
327
|
-
throw new AuthError(
|
|
332
|
+
throw new AuthError(AuthErrorCode.VERIFICATION_FAILED, error.message || 'Failed to send verification email');
|
|
328
333
|
}
|
|
329
334
|
}
|
|
330
335
|
isValidEmail(email) {
|
|
@@ -358,12 +363,7 @@ export class EmailPasswordProvider {
|
|
|
358
363
|
// Email/Password auth is supported if we have the required config
|
|
359
364
|
return !!this.config.apiUrl;
|
|
360
365
|
}
|
|
361
|
-
|
|
362
|
-
throw new AuthError('NOT_SUPPORTED', 'Account linking is not supported for email/password authentication');
|
|
363
|
-
}
|
|
364
|
-
async unlinkAccount(_options) {
|
|
365
|
-
throw new AuthError('NOT_SUPPORTED', 'Account unlinking is not supported for email/password authentication');
|
|
366
|
-
}
|
|
366
|
+
// linkAccount + unlinkAccount inherit BaseAuthProvider's OPERATION_NOT_ALLOWED defaults (F-44).
|
|
367
367
|
async revokeAccess(_token) {
|
|
368
368
|
// For email/password auth, revoking access means signing out
|
|
369
369
|
await this.signOut();
|