-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove v9 firebase and rely on service account to perform operations
- Loading branch information
1 parent
fc46030
commit 3b25317
Showing
20 changed files
with
143 additions
and
80 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"firestore": { | ||
"rules": "firestore.rules" | ||
}, | ||
"emulators": { | ||
"auth": { | ||
"port": 9099 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
rules_version = '2'; | ||
service cloud.firestore { | ||
match /databases/{database}/documents { | ||
match /{document=**} { | ||
allow read, write: if false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
EMULATOR_AUTH_PORT, | ||
EMULATOR_FIRESTORE_PORT, | ||
EMULATOR_HOST | ||
} from "data-layer/adapters/EmulatorConfig" | ||
import * as _admin from "firebase-admin" | ||
|
||
if (process.env.DEV || process.env.JEST_WORKER_ID !== undefined) { | ||
process.env.FIRESTORE_EMULATOR_HOST = `${EMULATOR_HOST}:${EMULATOR_FIRESTORE_PORT}` | ||
process.env.FIREBASE_AUTH_EMULATOR_HOST = `${EMULATOR_HOST}:${EMULATOR_AUTH_PORT}` | ||
} | ||
|
||
const firebase = _admin.initializeApp() | ||
|
||
export const admin = _admin | ||
|
||
export const auth = firebase.auth() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const EMULATOR_PROJECT_ID = "uasc-ceebc" | ||
export const EMULATOR_HOST = "localhost" | ||
export const EMULATOR_FIRESTORE_PORT = 8080 | ||
export const EMULATOR_AUTH_PORT = 9090 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,27 @@ | ||
// credit https://plainenglish.io/blog/using-firestore-with-typescript-in-the-v9-sdk-cf36851bb099 | ||
import "dotenv/config" | ||
import { UserAdditionalInfo } from "data-layer/models/firebase" | ||
import { | ||
getFirestore, | ||
CollectionReference, | ||
collection, | ||
DocumentData, | ||
connectFirestoreEmulator | ||
} from "firebase/firestore" | ||
import { USERS_COLLECTION } from "./CollectionNames" | ||
import { initializeApp } from "firebase/app" | ||
import { firebaseConfig } from "./FirestoreConfig" | ||
import { | ||
EMULATOR_FIRESTORE_PORT, | ||
EMULATOR_HOST, | ||
EMULATOR_PROJECT_ID | ||
} from "./EmulatorConfig" | ||
import { admin } from "business-layer/security/Firebase" | ||
|
||
if (process.env.DEV || process.env.JEST_WORKER_ID !== undefined) { | ||
initializeApp({ projectId: EMULATOR_PROJECT_ID }) | ||
} else { | ||
initializeApp(firebaseConfig) | ||
} | ||
const converter = <T>() => ({ | ||
toFirestore: (data: any) => data, | ||
fromFirestore: (doc: any) => doc.data() as T | ||
}) | ||
|
||
export const firestore = getFirestore() | ||
export const firestore = Object.assign( | ||
() => { | ||
return admin.firestore() | ||
}, | ||
{ | ||
doc: <T>(path: string) => { | ||
return admin.firestore().doc(path).withConverter<T>(converter<T>()) | ||
}, | ||
collection: <T>(path: string) => { | ||
return admin.firestore().collection(path).withConverter<T>(converter<T>()) | ||
} | ||
} | ||
) | ||
|
||
if (process.env.DEV || process.env.JEST_WORKER_ID !== undefined) { | ||
connectFirestoreEmulator(firestore, EMULATOR_HOST, EMULATOR_FIRESTORE_PORT) | ||
} | ||
|
||
const createCollection = <T = DocumentData>(collectionName: string) => { | ||
return collection(firestore, collectionName) as CollectionReference<T> | ||
} | ||
|
||
export const UsersCollection = | ||
createCollection<UserAdditionalInfo>(USERS_COLLECTION) | ||
export const db = { | ||
users: firestore.collection<UserAdditionalInfo>("users") | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,40 @@ | ||
import { UsersCollection } from "data-layer/adapters/FirestoreCollections" | ||
import { db } from "data-layer/adapters/FirestoreCollections" | ||
import { UserAdditionalInfo } from "data-layer/models/firebase" | ||
import { | ||
deleteDoc, | ||
doc, | ||
getDoc, | ||
getDocs, | ||
query, | ||
setDoc, | ||
updateDoc, | ||
where | ||
} from "firebase/firestore" | ||
|
||
export default class UserService { | ||
// Create | ||
public async addUser(uid: string, additionalInfo: UserAdditionalInfo) { | ||
await setDoc(doc(UsersCollection, uid), additionalInfo) | ||
await db.users.doc(uid).set(additionalInfo) | ||
} | ||
|
||
// Read | ||
public async getUsers() { | ||
const res = await getDocs(UsersCollection) | ||
const res = await db.users.get() | ||
const users = res.docs.map((user) => { | ||
return user.data() | ||
}) | ||
return users | ||
} | ||
|
||
public async getUser(uid: string) { | ||
const userDoc = await getDoc(doc(UsersCollection, uid)) | ||
const userDoc = await db.users.doc(uid).get() | ||
return userDoc.data() | ||
} | ||
|
||
public async getFilteredUsers(filters: Partial<UserAdditionalInfo>) { | ||
let q = query(UsersCollection) | ||
for (const filter of Object.keys(filters)) { | ||
const field = filter as keyof UserAdditionalInfo | ||
q = query(q, where(filter, "==", filters[field])) | ||
} | ||
const filteredUsers = await getDocs(q) | ||
return filteredUsers | ||
// TODO | ||
} | ||
|
||
// Update | ||
public async editUser( | ||
uid: string, | ||
updatedFields: Partial<UserAdditionalInfo> | ||
) { | ||
const userRef = doc(UsersCollection, uid) | ||
await updateDoc(userRef, updatedFields) | ||
await db.users.doc(uid).set(updatedFields, { merge: true }) | ||
} | ||
|
||
// Delete | ||
public async deleteUser(uid: string) { | ||
const userRef = await doc(UsersCollection, uid) | ||
await deleteDoc(userRef) | ||
await db.users.doc(uid).delete() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default class FireBaseError extends Error { | ||
protected statusCode: number | ||
constructor(name: string, statusCode: number, message?: string) { | ||
super(message) | ||
this.name = name | ||
this.statusCode = statusCode | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import admin from "firebase-admin" | ||
import dotenv from "dotenv" | ||
dotenv.config() | ||
|
||
/** | ||
* Credit John Chen | ||
* | ||
* How to use: | ||
* ``` | ||
* ts-node ./test/scripts/loginScript <USER_UID> | ||
* | ||
* ts-node ./test/scripts/loginScript mdLy2GYwTMZovNtnkj121dWU2YP2 | ||
* ``` | ||
*/ | ||
|
||
admin.initializeApp({ | ||
credential: admin.credential.applicationDefault() | ||
}) | ||
|
||
const createIdToken = async (uid: string) => { | ||
try { | ||
const customToken = await admin.auth().createCustomToken(uid) | ||
|
||
const res = await fetch( | ||
`https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=${process.env.API_KEY}`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
token: customToken, | ||
returnSecureToken: true | ||
}) | ||
} | ||
) | ||
|
||
const data = (await res.json()) as any | ||
console.log("\nAuthorization Header:") | ||
console.log("Bearer " + data.idToken) | ||
|
||
return data.idToken | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
} | ||
|
||
const args = process.argv.slice(2) | ||
|
||
if (args.length === 0) { | ||
console.log("Login with User ID:", process.env.USER_ID) | ||
createIdToken(process.env.USER_ID) | ||
} else { | ||
console.log("Login with User ID:", args[0]) | ||
createIdToken(args[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters