-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from cornellh4i/create-delete-user
Create add and delete user functions
- Loading branch information
Showing
10 changed files
with
1,329 additions
and
52 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.env | ||
secrets/ | ||
node_modules/ | ||
serviceAccountKey.json |
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
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,67 @@ | ||
var admin = require("firebase-admin"); | ||
|
||
var serviceAccount = require("./serviceAccountKey.json"); | ||
|
||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount), | ||
}); | ||
|
||
/** | ||
* Create a new user given their phone number; number should have +(country code) prepended, e.g. +16071234567 | ||
*/ | ||
export function createUser(number: string, claim: number) { | ||
let customClaims = {}; | ||
switch (claim) { | ||
case 0: { | ||
customClaims = { "mobile-entry": true }; | ||
break; | ||
} | ||
case 1: { | ||
customClaims = { "web-entry": true }; | ||
break; | ||
} | ||
case 2: { | ||
customClaims = { evaluator: true }; | ||
break; | ||
} | ||
case 3: { | ||
customClaims = { admin: true }; | ||
break; | ||
} | ||
} | ||
admin | ||
.auth() | ||
.createUser({ | ||
phoneNumber: number, | ||
}) | ||
.then((userRecord: any) => { | ||
admin.auth().setCustomUserClaims(userRecord.uid, customClaims); | ||
console.log("Successfully created new user:", userRecord.uid); | ||
}) | ||
.catch((error: any) => { | ||
console.log("Error creating new user: ", error); | ||
}); | ||
} | ||
|
||
/** | ||
* Delete a user given their phone number; number should have +(country code) prepended, e.g. +16071234567 | ||
*/ | ||
export function deleteUser(number: string) { | ||
const auth = admin.auth(); | ||
auth | ||
.getUserByPhoneNumber(number) | ||
.then((userRecord: any) => { | ||
console.log(`Successfully fetched user data: ${userRecord.toJSON()}`); | ||
auth | ||
.deleteUser(userRecord.uid) | ||
.then(() => { | ||
console.log("Successfully deleted user"); | ||
}) | ||
.catch((error: any) => { | ||
console.log("Error deleting user:", error); | ||
}); | ||
}) | ||
.catch((error: any) => { | ||
console.log("Error fetching user data:", error); | ||
}); | ||
} |
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
Oops, something went wrong.