Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit dd85be2

Browse files
committed
feat: add firebase-access-controller
License: MIT Signed-off-by: Vaibhav Saini <[email protected]>
1 parent 89c1c86 commit dd85be2

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

src/firebase-access-controller.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"use strict";
2+
3+
const EventEmitter = require("events").EventEmitter;
4+
const io = require("orbit-db-io");
5+
/**
6+
* A Firebase based AccessController for AvionDB & OrbitDB
7+
*
8+
* A demo app using FirebaseAccessController
9+
* https://github.com/dappkit/aviondb-firebase
10+
*
11+
*/
12+
class FirebaseAccessController extends EventEmitter {
13+
constructor(ipfs, options) {
14+
super();
15+
this._ipfs = ipfs;
16+
this.firebase = options.firebase;
17+
this.firebaseConfig = options.firebaseConfig;
18+
if (this.firebase.apps.length === 0) {
19+
this.firebase.initializeApp(this.firebaseConfig);
20+
}
21+
}
22+
23+
/*
24+
Every AC needs to have a 'Factory' method
25+
that creates an instance of the AccessController
26+
*/
27+
static async create(orbitdb, options) {
28+
console.log(options);
29+
if (!options.firebaseConfig) {
30+
throw new Error("you need to pass a firebaseConfig Object");
31+
}
32+
return new FirebaseAccessController(orbitdb._ipfs, options);
33+
}
34+
35+
/* Return the type for this controller */
36+
static get type() {
37+
return "firebase-access-controller";
38+
}
39+
40+
/*
41+
Return the type for this controller
42+
*/
43+
get type() {
44+
return this.constructor.type;
45+
}
46+
47+
/*
48+
Called by the databases (the log) to see if entry should
49+
be allowed in the database. Return true if the entry is allowed,
50+
false is not allowed
51+
*/
52+
async canAppend(entry, identityProvider) {
53+
return new Promise((resolve, reject) => {
54+
this.firebase.auth().onAuthStateChanged(async (user) => {
55+
if (user) {
56+
// A user is signed in
57+
const verifiedIdentity = await identityProvider.verifyIdentity(
58+
entry.identity
59+
);
60+
// Allow access if identity verifies
61+
return resolve(verifiedIdentity);
62+
} else {
63+
// No user is signed in
64+
return resolve(false);
65+
}
66+
});
67+
});
68+
}
69+
70+
/* Add access */
71+
async grant(user) {
72+
await this.firebase.auth().createUser(user);
73+
}
74+
/* Remove access */
75+
async revoke() {
76+
await this.firebase.auth().currentUser.delete();
77+
}
78+
79+
/* AC creation and loading */
80+
async load(address) {
81+
if (address) {
82+
try {
83+
if (address.indexOf("/ipfs") === 0) {
84+
address = address.split("/")[2];
85+
}
86+
const access = await io.read(this._ipfs, address);
87+
this.firebaseConfig = access.firebaseConfig;
88+
} catch (e) {
89+
console.log("FirebaseAccessController.load ERROR:", e);
90+
}
91+
}
92+
}
93+
/* Returns AC manifest parameters object */
94+
async save() {
95+
let cid;
96+
try {
97+
cid = await io.write(this._ipfs, "dag-cbor", {
98+
firebaseConfig: this.firebaseConfig,
99+
});
100+
} catch (e) {
101+
console.log("FirebaseAccessController.save ERROR:", e);
102+
}
103+
// return the manifest data
104+
return { address: cid };
105+
}
106+
}
107+
108+
export default FirebaseAccessController;

0 commit comments

Comments
 (0)