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

Commit 855719f

Browse files
committed
feat: add firebase-access-controller
License: MIT Signed-off-by: Vaibhav Saini <vasa@dappkit.io>
1 parent 89c1c86 commit 855719f

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/firebase-access-controller.js

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

0 commit comments

Comments
 (0)