@digitalaidseattle/firebase 1.0.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/dist/declarations/src/FirebaseAuthService.d.ts +14 -0
- package/dist/declarations/src/FirebaseStorageService.d.ts +12 -0
- package/dist/declarations/src/FirestoreService.d.ts +15 -0
- package/dist/declarations/src/firebaseClient.d.ts +2 -0
- package/dist/declarations/src/index.d.ts +4 -0
- package/dist/digitalaidseattle-firebase.cjs.d.ts +2 -0
- package/dist/digitalaidseattle-firebase.cjs.dev.js +277 -0
- package/dist/digitalaidseattle-firebase.cjs.js +7 -0
- package/dist/digitalaidseattle-firebase.cjs.prod.js +277 -0
- package/dist/digitalaidseattle-firebase.esm.js +266 -0
- package/package.json +27 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
import { AuthError, AuthService, OAuthResponse, User } from '@digitalaidseattle/core';
|
2
|
+
declare class FirebaseAuthService implements AuthService {
|
3
|
+
currentUser: User | undefined;
|
4
|
+
auth: import("@firebase/auth").Auth;
|
5
|
+
constructor();
|
6
|
+
hasUser(): Promise<boolean>;
|
7
|
+
getUser: () => Promise<any | null>;
|
8
|
+
signOut: () => Promise<{
|
9
|
+
error: AuthError | null;
|
10
|
+
}>;
|
11
|
+
signInWithGoogle: () => Promise<any>;
|
12
|
+
signInWithAzure(): Promise<OAuthResponse>;
|
13
|
+
}
|
14
|
+
export { FirebaseAuthService };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
/**
|
2
|
+
* storageService.ts
|
3
|
+
*
|
4
|
+
* @copyright 2024 Digital Aid Seattle
|
5
|
+
*
|
6
|
+
*/
|
7
|
+
import { StorageService } from "@digitalaidseattle/core";
|
8
|
+
export declare class FirebaseStorageService implements StorageService {
|
9
|
+
storage: import("@firebase/storage").FirebaseStorage;
|
10
|
+
decoder: TextDecoder;
|
11
|
+
downloadFile: (filepath: string) => Promise<string>;
|
12
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
type Entity = {
|
2
|
+
id: string | undefined;
|
3
|
+
};
|
4
|
+
declare class FirestoreService<T extends Entity> {
|
5
|
+
collectionName: string;
|
6
|
+
db: import("@firebase/firestore").Firestore;
|
7
|
+
constructor(collectionName: string);
|
8
|
+
add: (entity: T) => Promise<void>;
|
9
|
+
update: (entity: T) => Promise<void>;
|
10
|
+
delete: (entity: T) => Promise<void>;
|
11
|
+
getAll: () => Promise<T[]>;
|
12
|
+
addBatch: (entities: T[]) => Promise<void>;
|
13
|
+
}
|
14
|
+
export { FirestoreService };
|
15
|
+
export type { Entity };
|
@@ -0,0 +1,2 @@
|
|
1
|
+
export * from "./declarations/src/index.js";
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGlnaXRhbGFpZHNlYXR0bGUtZmlyZWJhc2UuY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvaW5kZXguZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
|
@@ -0,0 +1,277 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
+
|
5
|
+
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
|
6
|
+
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
7
|
+
var _createClass = require('@babel/runtime/helpers/createClass');
|
8
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
9
|
+
var _regeneratorRuntime = require('@babel/runtime/regenerator');
|
10
|
+
var auth = require('firebase/auth');
|
11
|
+
var app = require('firebase/app');
|
12
|
+
var storage = require('firebase/storage');
|
13
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
14
|
+
var firestore = require('firebase/firestore');
|
15
|
+
var uuid = require('uuid');
|
16
|
+
|
17
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
18
|
+
|
19
|
+
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
|
20
|
+
|
21
|
+
var firebaseConfig = {
|
22
|
+
apiKey: undefined.VITE_FIREBASE_API_KEY,
|
23
|
+
authDomain: undefined.VITE_FIREBASE_AUTH_DOMAIN,
|
24
|
+
projectId: undefined.VITE_FIREBASE_PROJECT_ID,
|
25
|
+
storageBucket: undefined.VITE_FIREBASE_STORAGE_BUCKET,
|
26
|
+
messagingSenderId: undefined.VITE_FIREBASE_MESSAGING_SENDER_ID,
|
27
|
+
appId: undefined.VITE_FIREBASE_APP_ID,
|
28
|
+
measurementId: undefined.VITE_FIREBASE_MEASUREMENT_ID
|
29
|
+
};
|
30
|
+
|
31
|
+
// Initialize Firebase
|
32
|
+
var firebaseClient = app.initializeApp(firebaseConfig);
|
33
|
+
|
34
|
+
var FirebaseAuthService = /*#__PURE__*/function () {
|
35
|
+
function FirebaseAuthService() {
|
36
|
+
var _this = this;
|
37
|
+
_classCallCheck(this, FirebaseAuthService);
|
38
|
+
_defineProperty(this, "currentUser", undefined);
|
39
|
+
_defineProperty(this, "auth", auth.getAuth(firebaseClient));
|
40
|
+
_defineProperty(this, "getUser", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
|
41
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
|
42
|
+
while (1) switch (_context.prev = _context.next) {
|
43
|
+
case 0:
|
44
|
+
return _context.abrupt("return", _this.currentUser);
|
45
|
+
case 1:
|
46
|
+
case "end":
|
47
|
+
return _context.stop();
|
48
|
+
}
|
49
|
+
}, _callee);
|
50
|
+
})));
|
51
|
+
_defineProperty(this, "signOut", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
|
52
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context2) {
|
53
|
+
while (1) switch (_context2.prev = _context2.next) {
|
54
|
+
case 0:
|
55
|
+
_this.currentUser = undefined;
|
56
|
+
_this.auth.signOut();
|
57
|
+
return _context2.abrupt("return", {
|
58
|
+
error: null
|
59
|
+
});
|
60
|
+
case 3:
|
61
|
+
case "end":
|
62
|
+
return _context2.stop();
|
63
|
+
}
|
64
|
+
}, _callee2);
|
65
|
+
})));
|
66
|
+
_defineProperty(this, "signInWithGoogle", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
|
67
|
+
var provider, resp;
|
68
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee3$(_context3) {
|
69
|
+
while (1) switch (_context3.prev = _context3.next) {
|
70
|
+
case 0:
|
71
|
+
_context3.prev = 0;
|
72
|
+
provider = new auth.GoogleAuthProvider();
|
73
|
+
_context3.next = 4;
|
74
|
+
return auth.signInWithPopup(_this.auth, provider);
|
75
|
+
case 4:
|
76
|
+
resp = _context3.sent;
|
77
|
+
_this.currentUser = {
|
78
|
+
email: resp.user.email,
|
79
|
+
user_metadata: {
|
80
|
+
name: resp.user.displayName,
|
81
|
+
avatar_url: resp.user.photoURL,
|
82
|
+
email: resp.user.email
|
83
|
+
}
|
84
|
+
};
|
85
|
+
return _context3.abrupt("return", {
|
86
|
+
data: {
|
87
|
+
url: undefined.VITE_AUTH_DOMAIN
|
88
|
+
}
|
89
|
+
});
|
90
|
+
case 9:
|
91
|
+
_context3.prev = 9;
|
92
|
+
_context3.t0 = _context3["catch"](0);
|
93
|
+
console.error(_context3.t0);
|
94
|
+
case 12:
|
95
|
+
case "end":
|
96
|
+
return _context3.stop();
|
97
|
+
}
|
98
|
+
}, _callee3, null, [[0, 9]]);
|
99
|
+
})));
|
100
|
+
this.auth.onAuthStateChanged(function (user) {
|
101
|
+
if (user) {
|
102
|
+
_this.currentUser = {
|
103
|
+
email: user.email,
|
104
|
+
user_metadata: {
|
105
|
+
name: user.displayName,
|
106
|
+
avatar_url: user.photoURL,
|
107
|
+
email: user.email
|
108
|
+
}
|
109
|
+
};
|
110
|
+
}
|
111
|
+
});
|
112
|
+
}
|
113
|
+
return _createClass(FirebaseAuthService, [{
|
114
|
+
key: "hasUser",
|
115
|
+
value: function hasUser() {
|
116
|
+
return Promise.resolve(this.currentUser !== null);
|
117
|
+
}
|
118
|
+
}, {
|
119
|
+
key: "signInWithAzure",
|
120
|
+
value: function signInWithAzure() {
|
121
|
+
// Placeholder implementation, as Azure sign-in is not implemented
|
122
|
+
return Promise.reject(new Error('Method not implemented.'));
|
123
|
+
}
|
124
|
+
}]);
|
125
|
+
}();
|
126
|
+
|
127
|
+
var FirebaseStorageService = /*#__PURE__*/_createClass(function FirebaseStorageService() {
|
128
|
+
var _this = this;
|
129
|
+
_classCallCheck(this, FirebaseStorageService);
|
130
|
+
_defineProperty(this, "storage", storage.getStorage(firebaseClient));
|
131
|
+
_defineProperty(this, "decoder", new TextDecoder("utf-8"));
|
132
|
+
_defineProperty(this, "downloadFile", /*#__PURE__*/function () {
|
133
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(filepath) {
|
134
|
+
var fileRef;
|
135
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
|
136
|
+
while (1) switch (_context.prev = _context.next) {
|
137
|
+
case 0:
|
138
|
+
fileRef = storage.ref(_this.storage, filepath);
|
139
|
+
return _context.abrupt("return", storage.getBytes(fileRef).then(function (b) {
|
140
|
+
return _this.decoder.decode(b);
|
141
|
+
}));
|
142
|
+
case 2:
|
143
|
+
case "end":
|
144
|
+
return _context.stop();
|
145
|
+
}
|
146
|
+
}, _callee);
|
147
|
+
}));
|
148
|
+
return function (_x) {
|
149
|
+
return _ref.apply(this, arguments);
|
150
|
+
};
|
151
|
+
}());
|
152
|
+
});
|
153
|
+
|
154
|
+
var FirestoreService = /*#__PURE__*/_createClass(function FirestoreService(collectionName) {
|
155
|
+
var _this = this;
|
156
|
+
_classCallCheck(this, FirestoreService);
|
157
|
+
_defineProperty(this, "collectionName", "player");
|
158
|
+
_defineProperty(this, "db", firestore.getFirestore(firebaseClient));
|
159
|
+
// Add a document to a collection
|
160
|
+
_defineProperty(this, "add", /*#__PURE__*/function () {
|
161
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(entity) {
|
162
|
+
var docRef;
|
163
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
|
164
|
+
while (1) switch (_context.prev = _context.next) {
|
165
|
+
case 0:
|
166
|
+
_context.prev = 0;
|
167
|
+
entity.id = uuid.v4();
|
168
|
+
_context.next = 4;
|
169
|
+
return firestore.addDoc(firestore.collection(_this.db, _this.collectionName), entity);
|
170
|
+
case 4:
|
171
|
+
docRef = _context.sent;
|
172
|
+
console.log("Document written with ID: ", docRef.id);
|
173
|
+
_context.next = 11;
|
174
|
+
break;
|
175
|
+
case 8:
|
176
|
+
_context.prev = 8;
|
177
|
+
_context.t0 = _context["catch"](0);
|
178
|
+
console.error("Error adding document: ", _context.t0);
|
179
|
+
case 11:
|
180
|
+
case "end":
|
181
|
+
return _context.stop();
|
182
|
+
}
|
183
|
+
}, _callee, null, [[0, 8]]);
|
184
|
+
}));
|
185
|
+
return function (_x) {
|
186
|
+
return _ref.apply(this, arguments);
|
187
|
+
};
|
188
|
+
}());
|
189
|
+
// Update a document to a collection
|
190
|
+
_defineProperty(this, "update", /*#__PURE__*/function () {
|
191
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2(entity) {
|
192
|
+
var docRef;
|
193
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context2) {
|
194
|
+
while (1) switch (_context2.prev = _context2.next) {
|
195
|
+
case 0:
|
196
|
+
_context2.prev = 0;
|
197
|
+
if (!entity.id) {
|
198
|
+
_context2.next = 4;
|
199
|
+
break;
|
200
|
+
}
|
201
|
+
docRef = firestore.doc(_this.db, _this.collectionName, entity.id);
|
202
|
+
return _context2.abrupt("return", firestore.updateDoc(docRef, entity));
|
203
|
+
case 4:
|
204
|
+
throw new Error('Entity does not have an id.');
|
205
|
+
case 7:
|
206
|
+
_context2.prev = 7;
|
207
|
+
_context2.t0 = _context2["catch"](0);
|
208
|
+
console.error("Error updating document: ", _context2.t0);
|
209
|
+
case 10:
|
210
|
+
case "end":
|
211
|
+
return _context2.stop();
|
212
|
+
}
|
213
|
+
}, _callee2, null, [[0, 7]]);
|
214
|
+
}));
|
215
|
+
return function (_x2) {
|
216
|
+
return _ref2.apply(this, arguments);
|
217
|
+
};
|
218
|
+
}());
|
219
|
+
_defineProperty(this, "delete", /*#__PURE__*/function () {
|
220
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3(entity) {
|
221
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee3$(_context3) {
|
222
|
+
while (1) switch (_context3.prev = _context3.next) {
|
223
|
+
case 0:
|
224
|
+
if (!entity.id) {
|
225
|
+
_context3.next = 2;
|
226
|
+
break;
|
227
|
+
}
|
228
|
+
return _context3.abrupt("return", firestore.deleteDoc(firestore.doc(_this.db, _this.collectionName, entity.id)));
|
229
|
+
case 2:
|
230
|
+
throw new Error('Entity does not have an id.');
|
231
|
+
case 3:
|
232
|
+
case "end":
|
233
|
+
return _context3.stop();
|
234
|
+
}
|
235
|
+
}, _callee3);
|
236
|
+
}));
|
237
|
+
return function (_x3) {
|
238
|
+
return _ref3.apply(this, arguments);
|
239
|
+
};
|
240
|
+
}());
|
241
|
+
// Get all documents from a collection
|
242
|
+
_defineProperty(this, "getAll", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4() {
|
243
|
+
var querySnapshot;
|
244
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee4$(_context4) {
|
245
|
+
while (1) switch (_context4.prev = _context4.next) {
|
246
|
+
case 0:
|
247
|
+
_context4.next = 2;
|
248
|
+
return firestore.getDocs(firestore.collection(_this.db, _this.collectionName));
|
249
|
+
case 2:
|
250
|
+
querySnapshot = _context4.sent;
|
251
|
+
return _context4.abrupt("return", querySnapshot.docs.map(function (doc) {
|
252
|
+
return _objectSpread(_objectSpread({}, doc.data()), {}, {
|
253
|
+
id: doc.id
|
254
|
+
});
|
255
|
+
}));
|
256
|
+
case 4:
|
257
|
+
case "end":
|
258
|
+
return _context4.stop();
|
259
|
+
}
|
260
|
+
}, _callee4);
|
261
|
+
})));
|
262
|
+
_defineProperty(this, "addBatch", function (entities) {
|
263
|
+
var batch = firestore.writeBatch(_this.db);
|
264
|
+
entities.forEach(function (e) {
|
265
|
+
e.id = uuid.v4();
|
266
|
+
var docRef = firestore.doc(_this.db, _this.collectionName, e.id);
|
267
|
+
batch.set(docRef, e);
|
268
|
+
});
|
269
|
+
return batch.commit();
|
270
|
+
});
|
271
|
+
this.collectionName = collectionName;
|
272
|
+
});
|
273
|
+
|
274
|
+
exports.FirebaseAuthService = FirebaseAuthService;
|
275
|
+
exports.FirebaseStorageService = FirebaseStorageService;
|
276
|
+
exports.FirestoreService = FirestoreService;
|
277
|
+
exports.firebaseClient = firebaseClient;
|
@@ -0,0 +1,277 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
+
|
5
|
+
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
|
6
|
+
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
7
|
+
var _createClass = require('@babel/runtime/helpers/createClass');
|
8
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
9
|
+
var _regeneratorRuntime = require('@babel/runtime/regenerator');
|
10
|
+
var auth = require('firebase/auth');
|
11
|
+
var app = require('firebase/app');
|
12
|
+
var storage = require('firebase/storage');
|
13
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
14
|
+
var firestore = require('firebase/firestore');
|
15
|
+
var uuid = require('uuid');
|
16
|
+
|
17
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
18
|
+
|
19
|
+
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
|
20
|
+
|
21
|
+
var firebaseConfig = {
|
22
|
+
apiKey: undefined.VITE_FIREBASE_API_KEY,
|
23
|
+
authDomain: undefined.VITE_FIREBASE_AUTH_DOMAIN,
|
24
|
+
projectId: undefined.VITE_FIREBASE_PROJECT_ID,
|
25
|
+
storageBucket: undefined.VITE_FIREBASE_STORAGE_BUCKET,
|
26
|
+
messagingSenderId: undefined.VITE_FIREBASE_MESSAGING_SENDER_ID,
|
27
|
+
appId: undefined.VITE_FIREBASE_APP_ID,
|
28
|
+
measurementId: undefined.VITE_FIREBASE_MEASUREMENT_ID
|
29
|
+
};
|
30
|
+
|
31
|
+
// Initialize Firebase
|
32
|
+
var firebaseClient = app.initializeApp(firebaseConfig);
|
33
|
+
|
34
|
+
var FirebaseAuthService = /*#__PURE__*/function () {
|
35
|
+
function FirebaseAuthService() {
|
36
|
+
var _this = this;
|
37
|
+
_classCallCheck(this, FirebaseAuthService);
|
38
|
+
_defineProperty(this, "currentUser", undefined);
|
39
|
+
_defineProperty(this, "auth", auth.getAuth(firebaseClient));
|
40
|
+
_defineProperty(this, "getUser", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
|
41
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
|
42
|
+
while (1) switch (_context.prev = _context.next) {
|
43
|
+
case 0:
|
44
|
+
return _context.abrupt("return", _this.currentUser);
|
45
|
+
case 1:
|
46
|
+
case "end":
|
47
|
+
return _context.stop();
|
48
|
+
}
|
49
|
+
}, _callee);
|
50
|
+
})));
|
51
|
+
_defineProperty(this, "signOut", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
|
52
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context2) {
|
53
|
+
while (1) switch (_context2.prev = _context2.next) {
|
54
|
+
case 0:
|
55
|
+
_this.currentUser = undefined;
|
56
|
+
_this.auth.signOut();
|
57
|
+
return _context2.abrupt("return", {
|
58
|
+
error: null
|
59
|
+
});
|
60
|
+
case 3:
|
61
|
+
case "end":
|
62
|
+
return _context2.stop();
|
63
|
+
}
|
64
|
+
}, _callee2);
|
65
|
+
})));
|
66
|
+
_defineProperty(this, "signInWithGoogle", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
|
67
|
+
var provider, resp;
|
68
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee3$(_context3) {
|
69
|
+
while (1) switch (_context3.prev = _context3.next) {
|
70
|
+
case 0:
|
71
|
+
_context3.prev = 0;
|
72
|
+
provider = new auth.GoogleAuthProvider();
|
73
|
+
_context3.next = 4;
|
74
|
+
return auth.signInWithPopup(_this.auth, provider);
|
75
|
+
case 4:
|
76
|
+
resp = _context3.sent;
|
77
|
+
_this.currentUser = {
|
78
|
+
email: resp.user.email,
|
79
|
+
user_metadata: {
|
80
|
+
name: resp.user.displayName,
|
81
|
+
avatar_url: resp.user.photoURL,
|
82
|
+
email: resp.user.email
|
83
|
+
}
|
84
|
+
};
|
85
|
+
return _context3.abrupt("return", {
|
86
|
+
data: {
|
87
|
+
url: undefined.VITE_AUTH_DOMAIN
|
88
|
+
}
|
89
|
+
});
|
90
|
+
case 9:
|
91
|
+
_context3.prev = 9;
|
92
|
+
_context3.t0 = _context3["catch"](0);
|
93
|
+
console.error(_context3.t0);
|
94
|
+
case 12:
|
95
|
+
case "end":
|
96
|
+
return _context3.stop();
|
97
|
+
}
|
98
|
+
}, _callee3, null, [[0, 9]]);
|
99
|
+
})));
|
100
|
+
this.auth.onAuthStateChanged(function (user) {
|
101
|
+
if (user) {
|
102
|
+
_this.currentUser = {
|
103
|
+
email: user.email,
|
104
|
+
user_metadata: {
|
105
|
+
name: user.displayName,
|
106
|
+
avatar_url: user.photoURL,
|
107
|
+
email: user.email
|
108
|
+
}
|
109
|
+
};
|
110
|
+
}
|
111
|
+
});
|
112
|
+
}
|
113
|
+
return _createClass(FirebaseAuthService, [{
|
114
|
+
key: "hasUser",
|
115
|
+
value: function hasUser() {
|
116
|
+
return Promise.resolve(this.currentUser !== null);
|
117
|
+
}
|
118
|
+
}, {
|
119
|
+
key: "signInWithAzure",
|
120
|
+
value: function signInWithAzure() {
|
121
|
+
// Placeholder implementation, as Azure sign-in is not implemented
|
122
|
+
return Promise.reject(new Error('Method not implemented.'));
|
123
|
+
}
|
124
|
+
}]);
|
125
|
+
}();
|
126
|
+
|
127
|
+
var FirebaseStorageService = /*#__PURE__*/_createClass(function FirebaseStorageService() {
|
128
|
+
var _this = this;
|
129
|
+
_classCallCheck(this, FirebaseStorageService);
|
130
|
+
_defineProperty(this, "storage", storage.getStorage(firebaseClient));
|
131
|
+
_defineProperty(this, "decoder", new TextDecoder("utf-8"));
|
132
|
+
_defineProperty(this, "downloadFile", /*#__PURE__*/function () {
|
133
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(filepath) {
|
134
|
+
var fileRef;
|
135
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
|
136
|
+
while (1) switch (_context.prev = _context.next) {
|
137
|
+
case 0:
|
138
|
+
fileRef = storage.ref(_this.storage, filepath);
|
139
|
+
return _context.abrupt("return", storage.getBytes(fileRef).then(function (b) {
|
140
|
+
return _this.decoder.decode(b);
|
141
|
+
}));
|
142
|
+
case 2:
|
143
|
+
case "end":
|
144
|
+
return _context.stop();
|
145
|
+
}
|
146
|
+
}, _callee);
|
147
|
+
}));
|
148
|
+
return function (_x) {
|
149
|
+
return _ref.apply(this, arguments);
|
150
|
+
};
|
151
|
+
}());
|
152
|
+
});
|
153
|
+
|
154
|
+
var FirestoreService = /*#__PURE__*/_createClass(function FirestoreService(collectionName) {
|
155
|
+
var _this = this;
|
156
|
+
_classCallCheck(this, FirestoreService);
|
157
|
+
_defineProperty(this, "collectionName", "player");
|
158
|
+
_defineProperty(this, "db", firestore.getFirestore(firebaseClient));
|
159
|
+
// Add a document to a collection
|
160
|
+
_defineProperty(this, "add", /*#__PURE__*/function () {
|
161
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(entity) {
|
162
|
+
var docRef;
|
163
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
|
164
|
+
while (1) switch (_context.prev = _context.next) {
|
165
|
+
case 0:
|
166
|
+
_context.prev = 0;
|
167
|
+
entity.id = uuid.v4();
|
168
|
+
_context.next = 4;
|
169
|
+
return firestore.addDoc(firestore.collection(_this.db, _this.collectionName), entity);
|
170
|
+
case 4:
|
171
|
+
docRef = _context.sent;
|
172
|
+
console.log("Document written with ID: ", docRef.id);
|
173
|
+
_context.next = 11;
|
174
|
+
break;
|
175
|
+
case 8:
|
176
|
+
_context.prev = 8;
|
177
|
+
_context.t0 = _context["catch"](0);
|
178
|
+
console.error("Error adding document: ", _context.t0);
|
179
|
+
case 11:
|
180
|
+
case "end":
|
181
|
+
return _context.stop();
|
182
|
+
}
|
183
|
+
}, _callee, null, [[0, 8]]);
|
184
|
+
}));
|
185
|
+
return function (_x) {
|
186
|
+
return _ref.apply(this, arguments);
|
187
|
+
};
|
188
|
+
}());
|
189
|
+
// Update a document to a collection
|
190
|
+
_defineProperty(this, "update", /*#__PURE__*/function () {
|
191
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2(entity) {
|
192
|
+
var docRef;
|
193
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context2) {
|
194
|
+
while (1) switch (_context2.prev = _context2.next) {
|
195
|
+
case 0:
|
196
|
+
_context2.prev = 0;
|
197
|
+
if (!entity.id) {
|
198
|
+
_context2.next = 4;
|
199
|
+
break;
|
200
|
+
}
|
201
|
+
docRef = firestore.doc(_this.db, _this.collectionName, entity.id);
|
202
|
+
return _context2.abrupt("return", firestore.updateDoc(docRef, entity));
|
203
|
+
case 4:
|
204
|
+
throw new Error('Entity does not have an id.');
|
205
|
+
case 7:
|
206
|
+
_context2.prev = 7;
|
207
|
+
_context2.t0 = _context2["catch"](0);
|
208
|
+
console.error("Error updating document: ", _context2.t0);
|
209
|
+
case 10:
|
210
|
+
case "end":
|
211
|
+
return _context2.stop();
|
212
|
+
}
|
213
|
+
}, _callee2, null, [[0, 7]]);
|
214
|
+
}));
|
215
|
+
return function (_x2) {
|
216
|
+
return _ref2.apply(this, arguments);
|
217
|
+
};
|
218
|
+
}());
|
219
|
+
_defineProperty(this, "delete", /*#__PURE__*/function () {
|
220
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3(entity) {
|
221
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee3$(_context3) {
|
222
|
+
while (1) switch (_context3.prev = _context3.next) {
|
223
|
+
case 0:
|
224
|
+
if (!entity.id) {
|
225
|
+
_context3.next = 2;
|
226
|
+
break;
|
227
|
+
}
|
228
|
+
return _context3.abrupt("return", firestore.deleteDoc(firestore.doc(_this.db, _this.collectionName, entity.id)));
|
229
|
+
case 2:
|
230
|
+
throw new Error('Entity does not have an id.');
|
231
|
+
case 3:
|
232
|
+
case "end":
|
233
|
+
return _context3.stop();
|
234
|
+
}
|
235
|
+
}, _callee3);
|
236
|
+
}));
|
237
|
+
return function (_x3) {
|
238
|
+
return _ref3.apply(this, arguments);
|
239
|
+
};
|
240
|
+
}());
|
241
|
+
// Get all documents from a collection
|
242
|
+
_defineProperty(this, "getAll", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4() {
|
243
|
+
var querySnapshot;
|
244
|
+
return _regeneratorRuntime__default["default"].wrap(function _callee4$(_context4) {
|
245
|
+
while (1) switch (_context4.prev = _context4.next) {
|
246
|
+
case 0:
|
247
|
+
_context4.next = 2;
|
248
|
+
return firestore.getDocs(firestore.collection(_this.db, _this.collectionName));
|
249
|
+
case 2:
|
250
|
+
querySnapshot = _context4.sent;
|
251
|
+
return _context4.abrupt("return", querySnapshot.docs.map(function (doc) {
|
252
|
+
return _objectSpread(_objectSpread({}, doc.data()), {}, {
|
253
|
+
id: doc.id
|
254
|
+
});
|
255
|
+
}));
|
256
|
+
case 4:
|
257
|
+
case "end":
|
258
|
+
return _context4.stop();
|
259
|
+
}
|
260
|
+
}, _callee4);
|
261
|
+
})));
|
262
|
+
_defineProperty(this, "addBatch", function (entities) {
|
263
|
+
var batch = firestore.writeBatch(_this.db);
|
264
|
+
entities.forEach(function (e) {
|
265
|
+
e.id = uuid.v4();
|
266
|
+
var docRef = firestore.doc(_this.db, _this.collectionName, e.id);
|
267
|
+
batch.set(docRef, e);
|
268
|
+
});
|
269
|
+
return batch.commit();
|
270
|
+
});
|
271
|
+
this.collectionName = collectionName;
|
272
|
+
});
|
273
|
+
|
274
|
+
exports.FirebaseAuthService = FirebaseAuthService;
|
275
|
+
exports.FirebaseStorageService = FirebaseStorageService;
|
276
|
+
exports.FirestoreService = FirestoreService;
|
277
|
+
exports.firebaseClient = firebaseClient;
|
@@ -0,0 +1,266 @@
|
|
1
|
+
import _asyncToGenerator from '@babel/runtime/helpers/esm/asyncToGenerator';
|
2
|
+
import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
|
3
|
+
import _createClass from '@babel/runtime/helpers/esm/createClass';
|
4
|
+
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
|
5
|
+
import _regeneratorRuntime from '@babel/runtime/regenerator';
|
6
|
+
import { getAuth, GoogleAuthProvider, signInWithPopup } from 'firebase/auth';
|
7
|
+
import { initializeApp } from 'firebase/app';
|
8
|
+
import { getStorage, ref, getBytes } from 'firebase/storage';
|
9
|
+
import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
|
10
|
+
import { getFirestore, addDoc, collection, doc, updateDoc, deleteDoc, getDocs, writeBatch } from 'firebase/firestore';
|
11
|
+
import { v4 } from 'uuid';
|
12
|
+
|
13
|
+
var firebaseConfig = {
|
14
|
+
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
|
15
|
+
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
|
16
|
+
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
|
17
|
+
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
|
18
|
+
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
|
19
|
+
appId: import.meta.env.VITE_FIREBASE_APP_ID,
|
20
|
+
measurementId: import.meta.env.VITE_FIREBASE_MEASUREMENT_ID
|
21
|
+
};
|
22
|
+
|
23
|
+
// Initialize Firebase
|
24
|
+
var firebaseClient = initializeApp(firebaseConfig);
|
25
|
+
|
26
|
+
var FirebaseAuthService = /*#__PURE__*/function () {
|
27
|
+
function FirebaseAuthService() {
|
28
|
+
var _this = this;
|
29
|
+
_classCallCheck(this, FirebaseAuthService);
|
30
|
+
_defineProperty(this, "currentUser", undefined);
|
31
|
+
_defineProperty(this, "auth", getAuth(firebaseClient));
|
32
|
+
_defineProperty(this, "getUser", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
33
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
34
|
+
while (1) switch (_context.prev = _context.next) {
|
35
|
+
case 0:
|
36
|
+
return _context.abrupt("return", _this.currentUser);
|
37
|
+
case 1:
|
38
|
+
case "end":
|
39
|
+
return _context.stop();
|
40
|
+
}
|
41
|
+
}, _callee);
|
42
|
+
})));
|
43
|
+
_defineProperty(this, "signOut", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
|
44
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
45
|
+
while (1) switch (_context2.prev = _context2.next) {
|
46
|
+
case 0:
|
47
|
+
_this.currentUser = undefined;
|
48
|
+
_this.auth.signOut();
|
49
|
+
return _context2.abrupt("return", {
|
50
|
+
error: null
|
51
|
+
});
|
52
|
+
case 3:
|
53
|
+
case "end":
|
54
|
+
return _context2.stop();
|
55
|
+
}
|
56
|
+
}, _callee2);
|
57
|
+
})));
|
58
|
+
_defineProperty(this, "signInWithGoogle", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
|
59
|
+
var provider, resp;
|
60
|
+
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
61
|
+
while (1) switch (_context3.prev = _context3.next) {
|
62
|
+
case 0:
|
63
|
+
_context3.prev = 0;
|
64
|
+
provider = new GoogleAuthProvider();
|
65
|
+
_context3.next = 4;
|
66
|
+
return signInWithPopup(_this.auth, provider);
|
67
|
+
case 4:
|
68
|
+
resp = _context3.sent;
|
69
|
+
_this.currentUser = {
|
70
|
+
email: resp.user.email,
|
71
|
+
user_metadata: {
|
72
|
+
name: resp.user.displayName,
|
73
|
+
avatar_url: resp.user.photoURL,
|
74
|
+
email: resp.user.email
|
75
|
+
}
|
76
|
+
};
|
77
|
+
return _context3.abrupt("return", {
|
78
|
+
data: {
|
79
|
+
url: import.meta.env.VITE_AUTH_DOMAIN
|
80
|
+
}
|
81
|
+
});
|
82
|
+
case 9:
|
83
|
+
_context3.prev = 9;
|
84
|
+
_context3.t0 = _context3["catch"](0);
|
85
|
+
console.error(_context3.t0);
|
86
|
+
case 12:
|
87
|
+
case "end":
|
88
|
+
return _context3.stop();
|
89
|
+
}
|
90
|
+
}, _callee3, null, [[0, 9]]);
|
91
|
+
})));
|
92
|
+
this.auth.onAuthStateChanged(function (user) {
|
93
|
+
if (user) {
|
94
|
+
_this.currentUser = {
|
95
|
+
email: user.email,
|
96
|
+
user_metadata: {
|
97
|
+
name: user.displayName,
|
98
|
+
avatar_url: user.photoURL,
|
99
|
+
email: user.email
|
100
|
+
}
|
101
|
+
};
|
102
|
+
}
|
103
|
+
});
|
104
|
+
}
|
105
|
+
return _createClass(FirebaseAuthService, [{
|
106
|
+
key: "hasUser",
|
107
|
+
value: function hasUser() {
|
108
|
+
return Promise.resolve(this.currentUser !== null);
|
109
|
+
}
|
110
|
+
}, {
|
111
|
+
key: "signInWithAzure",
|
112
|
+
value: function signInWithAzure() {
|
113
|
+
// Placeholder implementation, as Azure sign-in is not implemented
|
114
|
+
return Promise.reject(new Error('Method not implemented.'));
|
115
|
+
}
|
116
|
+
}]);
|
117
|
+
}();
|
118
|
+
|
119
|
+
var FirebaseStorageService = /*#__PURE__*/_createClass(function FirebaseStorageService() {
|
120
|
+
var _this = this;
|
121
|
+
_classCallCheck(this, FirebaseStorageService);
|
122
|
+
_defineProperty(this, "storage", getStorage(firebaseClient));
|
123
|
+
_defineProperty(this, "decoder", new TextDecoder("utf-8"));
|
124
|
+
_defineProperty(this, "downloadFile", /*#__PURE__*/function () {
|
125
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(filepath) {
|
126
|
+
var fileRef;
|
127
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
128
|
+
while (1) switch (_context.prev = _context.next) {
|
129
|
+
case 0:
|
130
|
+
fileRef = ref(_this.storage, filepath);
|
131
|
+
return _context.abrupt("return", getBytes(fileRef).then(function (b) {
|
132
|
+
return _this.decoder.decode(b);
|
133
|
+
}));
|
134
|
+
case 2:
|
135
|
+
case "end":
|
136
|
+
return _context.stop();
|
137
|
+
}
|
138
|
+
}, _callee);
|
139
|
+
}));
|
140
|
+
return function (_x) {
|
141
|
+
return _ref.apply(this, arguments);
|
142
|
+
};
|
143
|
+
}());
|
144
|
+
});
|
145
|
+
|
146
|
+
var FirestoreService = /*#__PURE__*/_createClass(function FirestoreService(collectionName) {
|
147
|
+
var _this = this;
|
148
|
+
_classCallCheck(this, FirestoreService);
|
149
|
+
_defineProperty(this, "collectionName", "player");
|
150
|
+
_defineProperty(this, "db", getFirestore(firebaseClient));
|
151
|
+
// Add a document to a collection
|
152
|
+
_defineProperty(this, "add", /*#__PURE__*/function () {
|
153
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(entity) {
|
154
|
+
var docRef;
|
155
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
156
|
+
while (1) switch (_context.prev = _context.next) {
|
157
|
+
case 0:
|
158
|
+
_context.prev = 0;
|
159
|
+
entity.id = v4();
|
160
|
+
_context.next = 4;
|
161
|
+
return addDoc(collection(_this.db, _this.collectionName), entity);
|
162
|
+
case 4:
|
163
|
+
docRef = _context.sent;
|
164
|
+
console.log("Document written with ID: ", docRef.id);
|
165
|
+
_context.next = 11;
|
166
|
+
break;
|
167
|
+
case 8:
|
168
|
+
_context.prev = 8;
|
169
|
+
_context.t0 = _context["catch"](0);
|
170
|
+
console.error("Error adding document: ", _context.t0);
|
171
|
+
case 11:
|
172
|
+
case "end":
|
173
|
+
return _context.stop();
|
174
|
+
}
|
175
|
+
}, _callee, null, [[0, 8]]);
|
176
|
+
}));
|
177
|
+
return function (_x) {
|
178
|
+
return _ref.apply(this, arguments);
|
179
|
+
};
|
180
|
+
}());
|
181
|
+
// Update a document to a collection
|
182
|
+
_defineProperty(this, "update", /*#__PURE__*/function () {
|
183
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(entity) {
|
184
|
+
var docRef;
|
185
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
186
|
+
while (1) switch (_context2.prev = _context2.next) {
|
187
|
+
case 0:
|
188
|
+
_context2.prev = 0;
|
189
|
+
if (!entity.id) {
|
190
|
+
_context2.next = 4;
|
191
|
+
break;
|
192
|
+
}
|
193
|
+
docRef = doc(_this.db, _this.collectionName, entity.id);
|
194
|
+
return _context2.abrupt("return", updateDoc(docRef, entity));
|
195
|
+
case 4:
|
196
|
+
throw new Error('Entity does not have an id.');
|
197
|
+
case 7:
|
198
|
+
_context2.prev = 7;
|
199
|
+
_context2.t0 = _context2["catch"](0);
|
200
|
+
console.error("Error updating document: ", _context2.t0);
|
201
|
+
case 10:
|
202
|
+
case "end":
|
203
|
+
return _context2.stop();
|
204
|
+
}
|
205
|
+
}, _callee2, null, [[0, 7]]);
|
206
|
+
}));
|
207
|
+
return function (_x2) {
|
208
|
+
return _ref2.apply(this, arguments);
|
209
|
+
};
|
210
|
+
}());
|
211
|
+
_defineProperty(this, "delete", /*#__PURE__*/function () {
|
212
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(entity) {
|
213
|
+
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
214
|
+
while (1) switch (_context3.prev = _context3.next) {
|
215
|
+
case 0:
|
216
|
+
if (!entity.id) {
|
217
|
+
_context3.next = 2;
|
218
|
+
break;
|
219
|
+
}
|
220
|
+
return _context3.abrupt("return", deleteDoc(doc(_this.db, _this.collectionName, entity.id)));
|
221
|
+
case 2:
|
222
|
+
throw new Error('Entity does not have an id.');
|
223
|
+
case 3:
|
224
|
+
case "end":
|
225
|
+
return _context3.stop();
|
226
|
+
}
|
227
|
+
}, _callee3);
|
228
|
+
}));
|
229
|
+
return function (_x3) {
|
230
|
+
return _ref3.apply(this, arguments);
|
231
|
+
};
|
232
|
+
}());
|
233
|
+
// Get all documents from a collection
|
234
|
+
_defineProperty(this, "getAll", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
|
235
|
+
var querySnapshot;
|
236
|
+
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
|
237
|
+
while (1) switch (_context4.prev = _context4.next) {
|
238
|
+
case 0:
|
239
|
+
_context4.next = 2;
|
240
|
+
return getDocs(collection(_this.db, _this.collectionName));
|
241
|
+
case 2:
|
242
|
+
querySnapshot = _context4.sent;
|
243
|
+
return _context4.abrupt("return", querySnapshot.docs.map(function (doc) {
|
244
|
+
return _objectSpread(_objectSpread({}, doc.data()), {}, {
|
245
|
+
id: doc.id
|
246
|
+
});
|
247
|
+
}));
|
248
|
+
case 4:
|
249
|
+
case "end":
|
250
|
+
return _context4.stop();
|
251
|
+
}
|
252
|
+
}, _callee4);
|
253
|
+
})));
|
254
|
+
_defineProperty(this, "addBatch", function (entities) {
|
255
|
+
var batch = writeBatch(_this.db);
|
256
|
+
entities.forEach(function (e) {
|
257
|
+
e.id = v4();
|
258
|
+
var docRef = doc(_this.db, _this.collectionName, e.id);
|
259
|
+
batch.set(docRef, e);
|
260
|
+
});
|
261
|
+
return batch.commit();
|
262
|
+
});
|
263
|
+
this.collectionName = collectionName;
|
264
|
+
});
|
265
|
+
|
266
|
+
export { FirebaseAuthService, FirebaseStorageService, FirestoreService, firebaseClient };
|
package/package.json
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"name": "@digitalaidseattle/firebase",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "Wrapper for firebase that works with DAS Component library",
|
5
|
+
"repository": "null//github.com/null/github.com/tree/master/packages/firebase",
|
6
|
+
"main": "dist/digitalaidseattle-firebase.cjs.js",
|
7
|
+
"module": "dist/digitalaidseattle-firebase.esm.js",
|
8
|
+
"types": "dist/declarations/src/index.d.ts",
|
9
|
+
"dependencies": {
|
10
|
+
"@babel/runtime": "^7.25.0",
|
11
|
+
"@digitalaidseattle/core": "1.0.4",
|
12
|
+
"firebase": "^11.2.0",
|
13
|
+
"react": "^18.3.1",
|
14
|
+
"uuid": "^11.0.5"
|
15
|
+
},
|
16
|
+
"devDependencies": {
|
17
|
+
"@testing-library/react": "^14.2.1",
|
18
|
+
"vite": "^5.4.10",
|
19
|
+
"vitest": "^1.2.2"
|
20
|
+
},
|
21
|
+
"scripts": {
|
22
|
+
"build": "tsc --project tsconfig.json"
|
23
|
+
},
|
24
|
+
"files": [
|
25
|
+
"dist"
|
26
|
+
]
|
27
|
+
}
|