@mxpicture/gcp-functions-frontend 0.1.19 → 0.1.21
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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { MetaItem } from "@mxpicture/zod-meta";
|
|
2
2
|
import type { ApiKey, DocumentData, WithKey, FunctionName, FunctionPayload, FunctionResult } from "@mxpicture/gcp-functions-common/types";
|
|
3
3
|
import { IApi } from "@mxpicture/gcp-functions-common/api";
|
|
4
|
-
import type
|
|
4
|
+
import { type HttpsCallable } from "firebase/functions";
|
|
5
5
|
export declare const functionCallable: <DTO extends DocumentData, KEYS extends ApiKey>(name: FunctionName) => Promise<HttpsCallable<FunctionPayload<DTO, KEYS>, Promise<FunctionResult<WithKey<DTO>>>, unknown>>;
|
|
6
6
|
export declare class FrontendApi<DTO extends DocumentData> extends IApi<DTO> {
|
|
7
7
|
readonly name: FunctionName;
|
package/dist/api/FrontendApi.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { IApi } from "@mxpicture/gcp-functions-common/api";
|
|
2
|
+
import { httpsCallable } from "firebase/functions";
|
|
2
3
|
import { configFirebase } from "../config/firebase.config.js";
|
|
3
|
-
import { loadFirebaseFunctions } from "../loader/firebase.loader.js";
|
|
4
4
|
export const functionCallable = async (name) => {
|
|
5
|
-
const { httpsCallable } = await loadFirebaseFunctions();
|
|
6
5
|
return httpsCallable((await configFirebase()).functions, name);
|
|
7
6
|
};
|
|
8
7
|
export class FrontendApi extends IApi {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { GoogleAuthProvider, type Auth } from "firebase/auth";
|
|
2
|
-
import type
|
|
3
|
-
import type
|
|
4
|
-
import type { FirebaseConfig } from "../types/firebase.types.js";
|
|
5
|
-
export declare const initializeFirebase: (config: FirebaseConfig) => Promise<{
|
|
2
|
+
import { type FirebaseApp } from "firebase/app";
|
|
3
|
+
import { type Functions } from "firebase/functions";
|
|
4
|
+
import type { FirebaseConfig, FirebaseEmulatorConfig } from "../types/firebase.types.js";
|
|
5
|
+
export declare const initializeFirebase: (config: FirebaseConfig, emu?: FirebaseEmulatorConfig) => Promise<{
|
|
6
6
|
app: FirebaseApp;
|
|
7
7
|
auth: Auth;
|
|
8
8
|
functions: Functions;
|
|
@@ -1,26 +1,31 @@
|
|
|
1
|
-
import { GoogleAuthProvider } from "firebase/auth";
|
|
2
|
-
import {
|
|
1
|
+
import { connectAuthEmulator, getAuth, GoogleAuthProvider, } from "firebase/auth";
|
|
2
|
+
import { initializeApp } from "firebase/app";
|
|
3
|
+
import { connectFunctionsEmulator, getFunctions, } from "firebase/functions";
|
|
3
4
|
let _inst = null;
|
|
4
5
|
let _config = null;
|
|
5
|
-
|
|
6
|
+
let _emu = null;
|
|
7
|
+
export const initializeFirebase = async (config, emu) => {
|
|
6
8
|
if (_config)
|
|
7
9
|
throw new Error("Firebase config already present");
|
|
8
10
|
_config = { ...config };
|
|
11
|
+
if (emu)
|
|
12
|
+
_emu = { ...emu };
|
|
9
13
|
return configFirebase();
|
|
10
14
|
};
|
|
11
15
|
export const configFirebase = async () => {
|
|
12
16
|
if (!_inst) {
|
|
13
17
|
if (!_config)
|
|
14
18
|
throw new Error("Firebase config not provided. Use initializeFirebase at first");
|
|
15
|
-
const { getAuth } = await loadFirebaseAuth();
|
|
16
|
-
const { getFunctions } = await loadFirebaseFunctions();
|
|
17
|
-
const { initializeApp } = await loadFirebaseApp();
|
|
18
19
|
// Initialize Firebase
|
|
19
20
|
const app = initializeApp(_config);
|
|
20
21
|
// Initialize Firebase services
|
|
21
22
|
const auth = getAuth(app);
|
|
22
|
-
|
|
23
|
+
if (_emu?.authHost)
|
|
24
|
+
connectAuthEmulator(auth, `http://${_emu.authHost}:${_emu.authPort ?? 9099}`, { disableWarnings: true });
|
|
23
25
|
const functions = getFunctions(app);
|
|
26
|
+
if (_emu?.functionsHost)
|
|
27
|
+
connectFunctionsEmulator(functions, _emu.functionsHost, _emu.functionsPort ?? 5001);
|
|
28
|
+
const googleProvider = new GoogleAuthProvider();
|
|
24
29
|
_inst = { app, auth, functions, googleProvider };
|
|
25
30
|
}
|
|
26
31
|
return _inst;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
+
import { createUserWithEmailAndPassword, onAuthStateChanged, sendPasswordResetEmail, signInWithEmailAndPassword, signInWithPopup, signOut, } from "firebase/auth";
|
|
1
2
|
import { configFirebase } from "../../config/firebase.config.js";
|
|
2
|
-
import { loadFirebaseAuth } from "../../loader/firebase.loader.js";
|
|
3
3
|
import { authErrorText } from "../../helper/auth.helper.js";
|
|
4
4
|
class AuthService {
|
|
5
5
|
async login(credentials) {
|
|
6
6
|
try {
|
|
7
|
-
const { signInWithEmailAndPassword } = await loadFirebaseAuth();
|
|
8
7
|
const { user } = await signInWithEmailAndPassword((await configFirebase()).auth, credentials.email, credentials.password);
|
|
9
8
|
return this.mapFirebaseUserToAuthUser(user);
|
|
10
9
|
}
|
|
@@ -14,7 +13,6 @@ class AuthService {
|
|
|
14
13
|
}
|
|
15
14
|
async loginWithGoogle() {
|
|
16
15
|
try {
|
|
17
|
-
const { signInWithPopup } = await loadFirebaseAuth();
|
|
18
16
|
const config = await configFirebase();
|
|
19
17
|
const { user } = await signInWithPopup(config.auth, config.googleProvider);
|
|
20
18
|
return this.mapFirebaseUserToAuthUser(user);
|
|
@@ -25,7 +23,6 @@ class AuthService {
|
|
|
25
23
|
}
|
|
26
24
|
async register(credentials) {
|
|
27
25
|
try {
|
|
28
|
-
const { createUserWithEmailAndPassword } = await loadFirebaseAuth();
|
|
29
26
|
const { user } = await createUserWithEmailAndPassword((await configFirebase()).auth, credentials.email, credentials.password);
|
|
30
27
|
// Store additional user data in Firestore
|
|
31
28
|
// const userService = new UserService()
|
|
@@ -38,7 +35,6 @@ class AuthService {
|
|
|
38
35
|
}
|
|
39
36
|
async logout() {
|
|
40
37
|
try {
|
|
41
|
-
const { signOut } = await loadFirebaseAuth();
|
|
42
38
|
await signOut((await configFirebase()).auth);
|
|
43
39
|
}
|
|
44
40
|
catch (error) {
|
|
@@ -47,7 +43,6 @@ class AuthService {
|
|
|
47
43
|
}
|
|
48
44
|
async resetPassword(email) {
|
|
49
45
|
try {
|
|
50
|
-
const { sendPasswordResetEmail } = await loadFirebaseAuth();
|
|
51
46
|
await sendPasswordResetEmail((await configFirebase()).auth, email);
|
|
52
47
|
}
|
|
53
48
|
catch (error) {
|
|
@@ -55,7 +50,6 @@ class AuthService {
|
|
|
55
50
|
}
|
|
56
51
|
}
|
|
57
52
|
async onAuthStateChanged(callback) {
|
|
58
|
-
const { onAuthStateChanged } = await loadFirebaseAuth();
|
|
59
53
|
return onAuthStateChanged((await configFirebase()).auth, (user) => {
|
|
60
54
|
callback(user ? this.mapFirebaseUserToAuthUser(user) : null);
|
|
61
55
|
});
|