valtech-components 2.0.452 → 2.0.453
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/esm2022/lib/services/auth/auth-state.service.mjs +173 -0
- package/esm2022/lib/services/auth/auth.service.mjs +454 -0
- package/esm2022/lib/services/auth/config.mjs +76 -0
- package/esm2022/lib/services/auth/guards.mjs +194 -0
- package/esm2022/lib/services/auth/index.mjs +70 -0
- package/esm2022/lib/services/auth/interceptor.mjs +98 -0
- package/esm2022/lib/services/auth/storage.service.mjs +141 -0
- package/esm2022/lib/services/auth/sync.service.mjs +149 -0
- package/esm2022/lib/services/auth/token.service.mjs +113 -0
- package/esm2022/lib/services/auth/types.mjs +29 -0
- package/esm2022/lib/services/firebase/config.mjs +108 -0
- package/esm2022/lib/services/firebase/firebase.service.mjs +288 -0
- package/esm2022/lib/services/firebase/firestore-collection.mjs +254 -0
- package/esm2022/lib/services/firebase/firestore.service.mjs +509 -0
- package/esm2022/lib/services/firebase/index.mjs +49 -0
- package/esm2022/lib/services/firebase/messaging.service.mjs +512 -0
- package/esm2022/lib/services/firebase/shared-config.mjs +138 -0
- package/esm2022/lib/services/firebase/storage.service.mjs +422 -0
- package/esm2022/lib/services/firebase/types.mjs +8 -0
- package/esm2022/lib/services/firebase/utils/path-builder.mjs +195 -0
- package/esm2022/lib/services/firebase/utils/query-builder.mjs +302 -0
- package/esm2022/public-api.mjs +3 -5
- package/fesm2022/valtech-components.mjs +4195 -4
- package/fesm2022/valtech-components.mjs.map +1 -1
- package/lib/services/auth/auth-state.service.d.ts +85 -0
- package/lib/services/auth/auth.service.d.ts +146 -0
- package/lib/services/auth/config.d.ts +38 -0
- package/lib/services/auth/guards.d.ts +123 -0
- package/lib/services/auth/index.d.ts +63 -0
- package/lib/services/auth/interceptor.d.ts +22 -0
- package/lib/services/auth/storage.service.d.ts +48 -0
- package/lib/services/auth/sync.service.d.ts +49 -0
- package/lib/services/auth/token.service.d.ts +51 -0
- package/lib/services/auth/types.d.ts +315 -0
- package/lib/services/firebase/config.d.ts +49 -0
- package/lib/services/firebase/firebase.service.d.ts +140 -0
- package/lib/services/firebase/firestore-collection.d.ts +175 -0
- package/lib/services/firebase/firestore.service.d.ts +304 -0
- package/lib/services/firebase/index.d.ts +39 -0
- package/lib/services/firebase/messaging.service.d.ts +263 -0
- package/lib/services/firebase/shared-config.d.ts +126 -0
- package/lib/services/firebase/storage.service.d.ts +206 -0
- package/lib/services/firebase/types.d.ts +281 -0
- package/lib/services/firebase/utils/path-builder.d.ts +132 -0
- package/lib/services/firebase/utils/query-builder.d.ts +210 -0
- package/package.json +1 -1
- package/public-api.d.ts +2 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tipos e interfaces para el servicio de autenticación de Valtech.
|
|
3
|
+
* Alineados con el backend AuthV2.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Configuración para el servicio de autenticación.
|
|
7
|
+
*/
|
|
8
|
+
export interface ValtechAuthConfig {
|
|
9
|
+
/** URL base de la API (ej: 'https://api.myvaltech.com') */
|
|
10
|
+
apiUrl: string;
|
|
11
|
+
/** Prefijo para endpoints de auth (default: '/v2/auth') */
|
|
12
|
+
authPrefix?: string;
|
|
13
|
+
/** Prefijo para las claves de localStorage (default: 'valtech_auth_') */
|
|
14
|
+
storagePrefix?: string;
|
|
15
|
+
/** Tiempo antes de expiración para refrescar token en segundos (default: 60) */
|
|
16
|
+
refreshBeforeExpiry?: number;
|
|
17
|
+
/** Habilitar sincronización entre pestañas (default: true) */
|
|
18
|
+
enableTabSync?: boolean;
|
|
19
|
+
/** Ruta de redirección cuando no autenticado (default: '/login') */
|
|
20
|
+
loginRoute?: string;
|
|
21
|
+
/** Ruta de redirección cuando ya autenticado (default: '/') */
|
|
22
|
+
homeRoute?: string;
|
|
23
|
+
/** Ruta para acceso denegado (default: '/unauthorized') */
|
|
24
|
+
unauthorizedRoute?: string;
|
|
25
|
+
/** Habilitar integración con FirebaseService (default: false) */
|
|
26
|
+
enableFirebaseIntegration?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Estado completo de autenticación.
|
|
30
|
+
*/
|
|
31
|
+
export interface AuthState {
|
|
32
|
+
/** Usuario está autenticado */
|
|
33
|
+
isAuthenticated: boolean;
|
|
34
|
+
/** Estado de carga inicial */
|
|
35
|
+
isLoading: boolean;
|
|
36
|
+
/** Token de acceso actual */
|
|
37
|
+
accessToken: string | null;
|
|
38
|
+
/** Token de refresco actual */
|
|
39
|
+
refreshToken: string | null;
|
|
40
|
+
/** ID del usuario */
|
|
41
|
+
userId: string | null;
|
|
42
|
+
/** Email del usuario */
|
|
43
|
+
email: string | null;
|
|
44
|
+
/** Roles del usuario */
|
|
45
|
+
roles: string[];
|
|
46
|
+
/** Permisos del usuario (formato 'resource:action') */
|
|
47
|
+
permissions: string[];
|
|
48
|
+
/** Usuario es super admin */
|
|
49
|
+
isSuperAdmin: boolean;
|
|
50
|
+
/** Timestamp de expiración del accessToken (ms) */
|
|
51
|
+
expiresAt: number | null;
|
|
52
|
+
/** Error de autenticación (si existe) */
|
|
53
|
+
error: AuthError | null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Información del usuario autenticado.
|
|
57
|
+
*/
|
|
58
|
+
export interface AuthUser {
|
|
59
|
+
userId: string;
|
|
60
|
+
email: string;
|
|
61
|
+
name?: string;
|
|
62
|
+
roles: string[];
|
|
63
|
+
permissions: string[];
|
|
64
|
+
isSuperAdmin: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Error de autenticación.
|
|
68
|
+
*/
|
|
69
|
+
export interface AuthError {
|
|
70
|
+
code: string;
|
|
71
|
+
message: string;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Estado inicial de autenticación.
|
|
75
|
+
*/
|
|
76
|
+
export declare const INITIAL_AUTH_STATE: AuthState;
|
|
77
|
+
/** Métodos de MFA soportados */
|
|
78
|
+
export type MFAMethod = 'EMAIL' | 'SMS';
|
|
79
|
+
/**
|
|
80
|
+
* Estado de MFA pendiente.
|
|
81
|
+
*/
|
|
82
|
+
export interface MFAPendingState {
|
|
83
|
+
required: boolean;
|
|
84
|
+
mfaToken: string | null;
|
|
85
|
+
method: MFAMethod | null;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Estado inicial de MFA.
|
|
89
|
+
*/
|
|
90
|
+
export declare const INITIAL_MFA_STATE: MFAPendingState;
|
|
91
|
+
/**
|
|
92
|
+
* Resultado de setup de MFA.
|
|
93
|
+
*/
|
|
94
|
+
export interface MFASetupResult {
|
|
95
|
+
codeSent: boolean;
|
|
96
|
+
message: string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Estado de MFA del usuario.
|
|
100
|
+
*/
|
|
101
|
+
export interface MFAStatus {
|
|
102
|
+
enabled: boolean;
|
|
103
|
+
method: MFAMethod | null;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Request para signup (registro).
|
|
107
|
+
*/
|
|
108
|
+
export interface SignupRequest {
|
|
109
|
+
email: string;
|
|
110
|
+
password: string;
|
|
111
|
+
name: string;
|
|
112
|
+
phone?: string;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Response de signup (registro).
|
|
116
|
+
*/
|
|
117
|
+
export interface SignupResponse {
|
|
118
|
+
operationId: string;
|
|
119
|
+
userId: string;
|
|
120
|
+
message: string;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Request para verificar email.
|
|
124
|
+
*/
|
|
125
|
+
export interface VerifyEmailRequest {
|
|
126
|
+
email: string;
|
|
127
|
+
code: string;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Response de verificación de email.
|
|
131
|
+
* Si es exitoso, incluye tokens para auto-login.
|
|
132
|
+
*/
|
|
133
|
+
export interface VerifyEmailResponse {
|
|
134
|
+
operationId: string;
|
|
135
|
+
verified: boolean;
|
|
136
|
+
accessToken?: string;
|
|
137
|
+
refreshToken?: string;
|
|
138
|
+
firebaseToken?: string;
|
|
139
|
+
expiresIn?: number;
|
|
140
|
+
tokenType?: string;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Request para reenviar código de verificación.
|
|
144
|
+
*/
|
|
145
|
+
export interface ResendCodeRequest {
|
|
146
|
+
email: string;
|
|
147
|
+
type: 'EMAIL_VERIFY' | 'PASSWORD_RESET';
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Response de reenvío de código.
|
|
151
|
+
*/
|
|
152
|
+
export interface ResendCodeResponse {
|
|
153
|
+
operationId: string;
|
|
154
|
+
sent: boolean;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Request para signin.
|
|
158
|
+
*/
|
|
159
|
+
export interface SigninRequest {
|
|
160
|
+
email: string;
|
|
161
|
+
password: string;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Response de signin.
|
|
165
|
+
*/
|
|
166
|
+
export interface SigninResponse {
|
|
167
|
+
operationId: string;
|
|
168
|
+
accessToken?: string;
|
|
169
|
+
refreshToken?: string;
|
|
170
|
+
firebaseToken?: string;
|
|
171
|
+
expiresIn?: number;
|
|
172
|
+
tokenType?: string;
|
|
173
|
+
mfaRequired?: boolean;
|
|
174
|
+
mfaToken?: string;
|
|
175
|
+
mfaMethod?: MFAMethod;
|
|
176
|
+
roles?: string[];
|
|
177
|
+
permissions?: string[];
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Request para verificar MFA.
|
|
181
|
+
*/
|
|
182
|
+
export interface MFAVerifyRequest {
|
|
183
|
+
mfaToken: string;
|
|
184
|
+
code: string;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Response de verificar MFA.
|
|
188
|
+
*/
|
|
189
|
+
export interface MFAVerifyResponse {
|
|
190
|
+
operationId: string;
|
|
191
|
+
accessToken: string;
|
|
192
|
+
refreshToken: string;
|
|
193
|
+
firebaseToken?: string;
|
|
194
|
+
expiresIn: number;
|
|
195
|
+
tokenType: string;
|
|
196
|
+
roles?: string[];
|
|
197
|
+
permissions?: string[];
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Request para refrescar token.
|
|
201
|
+
*/
|
|
202
|
+
export interface RefreshRequest {
|
|
203
|
+
refreshToken: string;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Response de refrescar token.
|
|
207
|
+
*/
|
|
208
|
+
export interface RefreshResponse {
|
|
209
|
+
operationId: string;
|
|
210
|
+
accessToken: string;
|
|
211
|
+
expiresIn: number;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Request para logout.
|
|
215
|
+
*/
|
|
216
|
+
export interface LogoutRequest {
|
|
217
|
+
refreshToken: string;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Response de logout.
|
|
221
|
+
*/
|
|
222
|
+
export interface LogoutResponse {
|
|
223
|
+
operationId: string;
|
|
224
|
+
success: boolean;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Response de obtener permisos.
|
|
228
|
+
*/
|
|
229
|
+
export interface GetPermissionsResponse {
|
|
230
|
+
operationId: string;
|
|
231
|
+
roles: string[];
|
|
232
|
+
permissions: string[];
|
|
233
|
+
isSuperAdmin: boolean;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Request para setup de MFA.
|
|
237
|
+
*/
|
|
238
|
+
export interface MFASetupRequest {
|
|
239
|
+
method: MFAMethod;
|
|
240
|
+
phone?: string;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Response de setup de MFA.
|
|
244
|
+
*/
|
|
245
|
+
export interface MFASetupResponse {
|
|
246
|
+
operationId: string;
|
|
247
|
+
codeSent: boolean;
|
|
248
|
+
message: string;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Request para confirmar MFA.
|
|
252
|
+
*/
|
|
253
|
+
export interface MFAConfirmRequest {
|
|
254
|
+
code: string;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Response de confirmar MFA.
|
|
258
|
+
*/
|
|
259
|
+
export interface MFAConfirmResponse {
|
|
260
|
+
operationId: string;
|
|
261
|
+
mfaEnabled: boolean;
|
|
262
|
+
method: MFAMethod;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Request para deshabilitar MFA.
|
|
266
|
+
*/
|
|
267
|
+
export interface MFADisableRequest {
|
|
268
|
+
password: string;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Response de deshabilitar MFA.
|
|
272
|
+
*/
|
|
273
|
+
export interface MFADisableResponse {
|
|
274
|
+
operationId: string;
|
|
275
|
+
mfaDisabled: boolean;
|
|
276
|
+
}
|
|
277
|
+
/** Tipos de eventos de sincronización entre pestañas */
|
|
278
|
+
export type AuthSyncEventType = 'LOGIN' | 'LOGOUT' | 'TOKEN_REFRESH' | 'PERMISSIONS_UPDATE';
|
|
279
|
+
/**
|
|
280
|
+
* Evento de sincronización entre pestañas.
|
|
281
|
+
*/
|
|
282
|
+
export interface AuthSyncEvent {
|
|
283
|
+
type: AuthSyncEventType;
|
|
284
|
+
timestamp: number;
|
|
285
|
+
payload?: {
|
|
286
|
+
accessToken?: string;
|
|
287
|
+
expiresAt?: number;
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Claims del JWT de acceso.
|
|
292
|
+
*/
|
|
293
|
+
export interface JWTClaims {
|
|
294
|
+
/** User ID */
|
|
295
|
+
uid: string;
|
|
296
|
+
/** Email */
|
|
297
|
+
email: string;
|
|
298
|
+
/** Session ID */
|
|
299
|
+
sid?: string;
|
|
300
|
+
/** Issued at */
|
|
301
|
+
iat: number;
|
|
302
|
+
/** Expiration */
|
|
303
|
+
exp: number;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Datos persistidos en storage.
|
|
307
|
+
*/
|
|
308
|
+
export interface StoredAuthState {
|
|
309
|
+
accessToken: string;
|
|
310
|
+
refreshToken: string;
|
|
311
|
+
roles: string[];
|
|
312
|
+
permissions: string[];
|
|
313
|
+
isSuperAdmin: boolean;
|
|
314
|
+
expiresAt?: number;
|
|
315
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Firebase Configuration
|
|
3
|
+
*
|
|
4
|
+
* Configuración e inicialización de Firebase para aplicaciones Angular.
|
|
5
|
+
* Usa provideValtechFirebase() en el bootstrap de tu aplicación.
|
|
6
|
+
*/
|
|
7
|
+
import { EnvironmentProviders, InjectionToken } from '@angular/core';
|
|
8
|
+
import { ValtechFirebaseConfig } from './types';
|
|
9
|
+
/**
|
|
10
|
+
* Token de inyección para la configuración de Firebase.
|
|
11
|
+
* Usado internamente por los servicios de Firebase.
|
|
12
|
+
*/
|
|
13
|
+
export declare const VALTECH_FIREBASE_CONFIG: InjectionToken<ValtechFirebaseConfig>;
|
|
14
|
+
/**
|
|
15
|
+
* Provee Firebase a la aplicación Angular.
|
|
16
|
+
*
|
|
17
|
+
* @param config - Configuración de Firebase
|
|
18
|
+
* @returns EnvironmentProviders para usar en bootstrapApplication
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // main.ts
|
|
23
|
+
* import { bootstrapApplication } from '@angular/platform-browser';
|
|
24
|
+
* import { provideValtechFirebase } from 'valtech-components';
|
|
25
|
+
* import { environment } from './environments/environment';
|
|
26
|
+
*
|
|
27
|
+
* bootstrapApplication(AppComponent, {
|
|
28
|
+
* providers: [
|
|
29
|
+
* provideValtechFirebase({
|
|
30
|
+
* firebase: environment.firebase,
|
|
31
|
+
* persistence: true,
|
|
32
|
+
* emulator: environment.useEmulators ? {
|
|
33
|
+
* firestore: { host: 'localhost', port: 8080 },
|
|
34
|
+
* auth: { host: 'localhost', port: 9099 },
|
|
35
|
+
* storage: { host: 'localhost', port: 9199 },
|
|
36
|
+
* } : undefined,
|
|
37
|
+
* }),
|
|
38
|
+
* ],
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function provideValtechFirebase(config: ValtechFirebaseConfig): EnvironmentProviders;
|
|
43
|
+
/**
|
|
44
|
+
* Verifica si los emuladores están configurados.
|
|
45
|
+
*
|
|
46
|
+
* @param config - Configuración de Firebase
|
|
47
|
+
* @returns true si hay al menos un emulador configurado
|
|
48
|
+
*/
|
|
49
|
+
export declare function hasEmulators(config: ValtechFirebaseConfig): boolean;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Auth, UserCredential } from '@angular/fire/auth';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { FirebaseUser, SessionState, ValtechFirebaseConfig } from './types';
|
|
4
|
+
import * as i0 from "@angular/core";
|
|
5
|
+
/**
|
|
6
|
+
* Servicio de autenticación de Firebase.
|
|
7
|
+
*
|
|
8
|
+
* Este servicio NO maneja el login de usuarios directamente.
|
|
9
|
+
* En su lugar, trabaja con Custom Tokens generados por tu backend.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Después de autenticarte con tu backend (ej: Cognito)
|
|
14
|
+
* @Component({...})
|
|
15
|
+
* export class LoginComponent {
|
|
16
|
+
* private authService = inject(AuthService); // Tu servicio de auth
|
|
17
|
+
* private firebase = inject(FirebaseService); // Este servicio
|
|
18
|
+
*
|
|
19
|
+
* async login(email: string, password: string) {
|
|
20
|
+
* // 1. Autenticar con tu backend
|
|
21
|
+
* const response = await this.authService.login(email, password);
|
|
22
|
+
*
|
|
23
|
+
* // 2. El backend devuelve un Firebase Custom Token
|
|
24
|
+
* if (response.firebaseToken) {
|
|
25
|
+
* await this.firebase.signInWithCustomToken(response.firebaseToken);
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // Ahora el usuario puede acceder a Firestore, Storage, etc.
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* async logout() {
|
|
32
|
+
* await this.authService.logout();
|
|
33
|
+
* await this.firebase.signOut();
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare class FirebaseService {
|
|
39
|
+
private auth;
|
|
40
|
+
private config;
|
|
41
|
+
/** Estado interno de la sesión */
|
|
42
|
+
private sessionState;
|
|
43
|
+
/** Estado actual de la sesión como Observable */
|
|
44
|
+
readonly state$: Observable<SessionState>;
|
|
45
|
+
/** Usuario actual de Firebase como Observable */
|
|
46
|
+
readonly user$: Observable<FirebaseUser | null>;
|
|
47
|
+
/** Indica si el usuario está autenticado en Firebase */
|
|
48
|
+
readonly isAuthenticated$: Observable<boolean>;
|
|
49
|
+
constructor(auth: Auth, config: ValtechFirebaseConfig);
|
|
50
|
+
/**
|
|
51
|
+
* Autentica al usuario con un Custom Token generado por el backend.
|
|
52
|
+
*
|
|
53
|
+
* @param token - Firebase Custom Token generado por tu backend
|
|
54
|
+
* @returns UserCredential con la información del usuario
|
|
55
|
+
* @throws Error si el token es inválido o expiró
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Después de login exitoso con tu backend
|
|
60
|
+
* const { firebaseToken } = await backendAuth.login(email, password);
|
|
61
|
+
* await firebaseService.signInWithCustomToken(firebaseToken);
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
signInWithCustomToken(token: string): Promise<UserCredential>;
|
|
65
|
+
/**
|
|
66
|
+
* Cierra la sesión de Firebase.
|
|
67
|
+
* Llamar junto con el logout de tu sistema de autenticación principal.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* async logout() {
|
|
72
|
+
* await this.backendAuth.logout(); // Tu auth
|
|
73
|
+
* await this.firebase.signOut(); // Firebase
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
signOut(): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Obtiene el usuario actual de Firebase (síncrono).
|
|
80
|
+
* Retorna null si no hay usuario autenticado.
|
|
81
|
+
*/
|
|
82
|
+
get currentUser(): FirebaseUser | null;
|
|
83
|
+
/**
|
|
84
|
+
* Obtiene el UID del usuario actual.
|
|
85
|
+
* Retorna null si no hay usuario autenticado.
|
|
86
|
+
*/
|
|
87
|
+
get uid(): string | null;
|
|
88
|
+
/**
|
|
89
|
+
* Indica si hay un usuario autenticado actualmente.
|
|
90
|
+
*/
|
|
91
|
+
get isAuthenticated(): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Obtiene el ID Token de Firebase para el usuario actual.
|
|
94
|
+
* Útil para validar el usuario en tu backend.
|
|
95
|
+
*
|
|
96
|
+
* @param forceRefresh - Si true, fuerza la renovación del token
|
|
97
|
+
* @returns ID Token o null si no hay usuario
|
|
98
|
+
*/
|
|
99
|
+
getIdToken(forceRefresh?: boolean): Promise<string | null>;
|
|
100
|
+
/**
|
|
101
|
+
* Obtiene los claims personalizados del token del usuario.
|
|
102
|
+
* Los claims son establecidos por tu backend al crear el Custom Token.
|
|
103
|
+
*
|
|
104
|
+
* @returns Objeto con los claims o vacío si no hay usuario
|
|
105
|
+
*/
|
|
106
|
+
getClaims(): Promise<Record<string, unknown>>;
|
|
107
|
+
/**
|
|
108
|
+
* Verifica si el usuario tiene un rol específico.
|
|
109
|
+
* El rol debe estar definido en los claims del Custom Token.
|
|
110
|
+
*
|
|
111
|
+
* @param role - Nombre del rol a verificar
|
|
112
|
+
* @returns true si el usuario tiene el rol
|
|
113
|
+
*/
|
|
114
|
+
hasRole(role: string): Promise<boolean>;
|
|
115
|
+
/**
|
|
116
|
+
* Espera a que el estado de autenticación esté determinado.
|
|
117
|
+
* Útil en guards o al inicializar la app.
|
|
118
|
+
*
|
|
119
|
+
* @returns Usuario actual o null
|
|
120
|
+
*/
|
|
121
|
+
waitForAuth(): Promise<FirebaseUser | null>;
|
|
122
|
+
/**
|
|
123
|
+
* Obtiene la configuración actual de Firebase.
|
|
124
|
+
*/
|
|
125
|
+
getConfig(): ValtechFirebaseConfig;
|
|
126
|
+
/**
|
|
127
|
+
* Indica si los emuladores están habilitados.
|
|
128
|
+
*/
|
|
129
|
+
isUsingEmulators(): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Mapea un User de Firebase a nuestra interface FirebaseUser
|
|
132
|
+
*/
|
|
133
|
+
private mapUser;
|
|
134
|
+
/**
|
|
135
|
+
* Convierte errores de Firebase a mensajes en español
|
|
136
|
+
*/
|
|
137
|
+
private getErrorMessage;
|
|
138
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<FirebaseService, never>;
|
|
139
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<FirebaseService>;
|
|
140
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { FirestoreService } from './firestore.service';
|
|
3
|
+
import { FirestoreDocument, PaginatedResult, QueryOptions } from './types';
|
|
4
|
+
import * as i0 from "@angular/core";
|
|
5
|
+
/**
|
|
6
|
+
* Opciones de configuración para una colección.
|
|
7
|
+
*/
|
|
8
|
+
export interface CollectionOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Si true, usa soft delete (marca deletedAt en lugar de eliminar).
|
|
11
|
+
* Default: false
|
|
12
|
+
*/
|
|
13
|
+
softDelete?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Si true, maneja automáticamente createdAt/updatedAt.
|
|
16
|
+
* Default: true
|
|
17
|
+
*/
|
|
18
|
+
timestamps?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Referencia a una sub-colección tipada.
|
|
22
|
+
*/
|
|
23
|
+
export interface SubCollectionRef<T extends FirestoreDocument> {
|
|
24
|
+
getById(id: string): Promise<T | null>;
|
|
25
|
+
getAll(options?: QueryOptions): Promise<T[]>;
|
|
26
|
+
watch(id: string): Observable<T | null>;
|
|
27
|
+
watchAll(options?: QueryOptions): Observable<T[]>;
|
|
28
|
+
create(data: Omit<T, 'id' | 'createdAt' | 'updatedAt'>): Promise<T>;
|
|
29
|
+
update(id: string, data: Partial<T>): Promise<void>;
|
|
30
|
+
delete(id: string): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Factory para crear instancias de colección tipadas.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* @Injectable({ providedIn: 'root' })
|
|
38
|
+
* export class UsersService {
|
|
39
|
+
* private users = inject(FirestoreCollectionFactory).create<User>('users');
|
|
40
|
+
*
|
|
41
|
+
* getAll = () => this.users.getAll();
|
|
42
|
+
* getById = (id: string) => this.users.getById(id);
|
|
43
|
+
* create = (data: Omit<User, 'id'>) => this.users.create(data);
|
|
44
|
+
*
|
|
45
|
+
* // Métodos personalizados
|
|
46
|
+
* async getActiveUsers(): Promise<User[]> {
|
|
47
|
+
* return this.users.query({
|
|
48
|
+
* where: [{ field: 'active', operator: '==', value: true }]
|
|
49
|
+
* });
|
|
50
|
+
* }
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare class FirestoreCollectionFactory {
|
|
55
|
+
private firestore;
|
|
56
|
+
constructor(firestore: FirestoreService);
|
|
57
|
+
/**
|
|
58
|
+
* Crea una instancia de colección tipada.
|
|
59
|
+
*
|
|
60
|
+
* @param collectionPath - Ruta de la colección en Firestore
|
|
61
|
+
* @param options - Opciones de configuración
|
|
62
|
+
* @returns Instancia de TypedCollection
|
|
63
|
+
*/
|
|
64
|
+
create<T extends FirestoreDocument>(collectionPath: string, options?: CollectionOptions): TypedCollection<T>;
|
|
65
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<FirestoreCollectionFactory, never>;
|
|
66
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<FirestoreCollectionFactory>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Colección tipada con métodos CRUD.
|
|
70
|
+
*
|
|
71
|
+
* NO usa inject() - recibe FirestoreService por constructor.
|
|
72
|
+
* Esto evita el error NG0203.
|
|
73
|
+
*/
|
|
74
|
+
export declare class TypedCollection<T extends FirestoreDocument> {
|
|
75
|
+
private firestore;
|
|
76
|
+
private collectionPath;
|
|
77
|
+
private readonly options;
|
|
78
|
+
constructor(firestore: FirestoreService, collectionPath: string, options?: CollectionOptions);
|
|
79
|
+
/**
|
|
80
|
+
* Obtiene un documento por ID.
|
|
81
|
+
*/
|
|
82
|
+
getById(id: string): Promise<T | null>;
|
|
83
|
+
/**
|
|
84
|
+
* Obtiene todos los documentos de la colección.
|
|
85
|
+
*/
|
|
86
|
+
getAll(options?: QueryOptions): Promise<T[]>;
|
|
87
|
+
/**
|
|
88
|
+
* Ejecuta una query personalizada.
|
|
89
|
+
*/
|
|
90
|
+
query(options: QueryOptions): Promise<T[]>;
|
|
91
|
+
/**
|
|
92
|
+
* Obtiene documentos con paginación.
|
|
93
|
+
*/
|
|
94
|
+
paginate(options: QueryOptions & {
|
|
95
|
+
limit: number;
|
|
96
|
+
}): Promise<PaginatedResult<T>>;
|
|
97
|
+
/**
|
|
98
|
+
* Obtiene el primer documento que coincida con la query.
|
|
99
|
+
*/
|
|
100
|
+
getFirst(options?: QueryOptions): Promise<T | null>;
|
|
101
|
+
/**
|
|
102
|
+
* Cuenta los documentos que coinciden con la query.
|
|
103
|
+
* Nota: Esto carga todos los documentos, usar con cuidado en colecciones grandes.
|
|
104
|
+
*/
|
|
105
|
+
count(options?: QueryOptions): Promise<number>;
|
|
106
|
+
/**
|
|
107
|
+
* Verifica si un documento existe.
|
|
108
|
+
*/
|
|
109
|
+
exists(id: string): Promise<boolean>;
|
|
110
|
+
/**
|
|
111
|
+
* Suscribe a cambios de un documento.
|
|
112
|
+
*/
|
|
113
|
+
watch(id: string): Observable<T | null>;
|
|
114
|
+
/**
|
|
115
|
+
* Suscribe a cambios de la colección.
|
|
116
|
+
*/
|
|
117
|
+
watchAll(options?: QueryOptions): Observable<T[]>;
|
|
118
|
+
/**
|
|
119
|
+
* Suscribe a una query personalizada.
|
|
120
|
+
*/
|
|
121
|
+
watchQuery(options: QueryOptions): Observable<T[]>;
|
|
122
|
+
/**
|
|
123
|
+
* Crea un nuevo documento con ID auto-generado.
|
|
124
|
+
*/
|
|
125
|
+
create(data: Omit<T, 'id' | 'createdAt' | 'updatedAt'>): Promise<T>;
|
|
126
|
+
/**
|
|
127
|
+
* Crea un documento con ID específico.
|
|
128
|
+
*/
|
|
129
|
+
createWithId(id: string, data: Omit<T, 'id'>): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Actualiza campos de un documento.
|
|
132
|
+
*/
|
|
133
|
+
update(id: string, data: Partial<Omit<T, 'id' | 'createdAt'>>): Promise<void>;
|
|
134
|
+
/**
|
|
135
|
+
* Elimina un documento.
|
|
136
|
+
* Si softDelete está habilitado, marca como eliminado en lugar de borrar.
|
|
137
|
+
*/
|
|
138
|
+
delete(id: string): Promise<void>;
|
|
139
|
+
/**
|
|
140
|
+
* Restaura un documento soft-deleted.
|
|
141
|
+
*/
|
|
142
|
+
restore(id: string): Promise<void>;
|
|
143
|
+
/**
|
|
144
|
+
* Obtiene una referencia a una sub-colección.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* // En UsersService
|
|
149
|
+
* getUserDocuments(userId: string) {
|
|
150
|
+
* return this.users.subcollection<Document>(userId, 'documents');
|
|
151
|
+
* }
|
|
152
|
+
*
|
|
153
|
+
* // Uso
|
|
154
|
+
* const docs = await users.getUserDocuments('user123').getAll();
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
subcollection<S extends FirestoreDocument>(parentId: string, subcollectionName: string): SubCollectionRef<S>;
|
|
158
|
+
/**
|
|
159
|
+
* Aplica filtros por defecto a las queries.
|
|
160
|
+
*/
|
|
161
|
+
private applyDefaultFilters;
|
|
162
|
+
/**
|
|
163
|
+
* Genera un nuevo ID sin crear el documento.
|
|
164
|
+
*/
|
|
165
|
+
generateId(): string;
|
|
166
|
+
/**
|
|
167
|
+
* Obtiene la ruta de la colección.
|
|
168
|
+
*/
|
|
169
|
+
getPath(): string;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* @deprecated Use FirestoreCollectionFactory.create() instead.
|
|
173
|
+
* Type alias for backwards compatibility.
|
|
174
|
+
*/
|
|
175
|
+
export type FirestoreCollection<T extends FirestoreDocument> = TypedCollection<T>;
|