forked from EskiMojo14/keycaplendar
-
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.
Add WIP functionality for designers to add and edit their own sets.
- Loading branch information
1 parent
1c9033c
commit abf9fee
Showing
16 changed files
with
407 additions
and
266 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 |
---|---|---|
|
@@ -23,3 +23,5 @@ yarn-debug.log* | |
yarn-error.log* | ||
firebase-debug.log | ||
.firebase/*.cache | ||
logo.ai | ||
keycaplendar.xd |
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,102 +1,129 @@ | ||
|
||
const admin = require('firebase-admin'); | ||
const functions = require('firebase-functions'); | ||
const admin = require("firebase-admin"); | ||
const functions = require("firebase-functions"); | ||
|
||
const app = admin.initializeApp(); | ||
|
||
exports.isEditor = functions.https.onCall((data, context) => { | ||
if (context.auth && (context.auth.token.editor === true)) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
exports.isAdmin = functions.https.onCall((data, context) => { | ||
if (context.auth && (context.auth.token.admin === true)) { | ||
return true; | ||
exports.getClaims = functions.https.onCall((data, context) => { | ||
if (context.auth) { | ||
return { | ||
nickname: context.auth.token.nickname ? context.auth.token.nickname : "", | ||
designer: context.auth.token.designer ? context.auth.token.designer : false, | ||
editor: context.auth.token.editor ? context.auth.token.editor : false, | ||
admin: context.auth.token.admin ? context.auth.token.admin : false, | ||
}; | ||
} | ||
return false; | ||
return { | ||
nickname: "", | ||
designer: false, | ||
editor: false, | ||
admin: false, | ||
}; | ||
}); | ||
|
||
exports.listUsers = functions.https.onCall(async (data, context) => { | ||
if (!context.auth || context.auth.token.admin !== true) { | ||
return { | ||
error: "Current user is not an admin. Access is not permitted." | ||
} | ||
error: "Current user is not an admin. Access is not permitted.", | ||
}; | ||
} | ||
// List batch of users, 1000 at a time. | ||
const users = await admin.auth().listUsers(1000) | ||
const users = await admin | ||
.auth() | ||
.listUsers(1000) | ||
.then((result) => { | ||
const newArray = result.users.map((user) => { | ||
if (user.customClaims) { | ||
return { | ||
displayName: user.displayName, | ||
email: user.email, | ||
photoURL: user.photoURL, | ||
editor: (user.customClaims.editor ? true : false), | ||
admin: (user.customClaims.admin ? true : false ) | ||
nickname: user.customClaims.nickname ? user.customClaims.nickname : "", | ||
designer: user.customClaims.designer ? true : false, | ||
editor: user.customClaims.editor ? true : false, | ||
admin: user.customClaims.admin ? true : false, | ||
}; | ||
} else { | ||
return { | ||
displayName: user.displayName, | ||
email: user.email, | ||
photoURL: user.photoURL, | ||
nickname: "", | ||
designer: false, | ||
editor: false, | ||
admin: false | ||
admin: false, | ||
}; | ||
} | ||
}) | ||
}); | ||
return newArray; | ||
}) | ||
.catch((error) => { | ||
return { error: 'Error listing users: ' + error }; | ||
return { error: "Error listing users: " + error }; | ||
}); | ||
return users; | ||
}); | ||
|
||
|
||
exports.deleteUser = functions.https.onCall(async (data, context) => { | ||
if (!context.auth || context.auth.token.admin !== true) { | ||
return { | ||
error: "Current user is not an admin. Access is not permitted." | ||
} | ||
error: "Current user is not an admin. Access is not permitted.", | ||
}; | ||
} | ||
if (data.email === '[email protected]') { | ||
if (data.email === "[email protected]") { | ||
return { | ||
error: "This user cannot be deleted" | ||
} | ||
error: "This user cannot be deleted", | ||
}; | ||
} | ||
const currentUser = await admin.auth().getUser(context.auth.uid); | ||
const user = await admin.auth().getUserByEmail(data.email); | ||
admin.auth().deleteUser(user.uid) | ||
admin | ||
.auth() | ||
.deleteUser(user.uid) | ||
.then(() => { | ||
console.log(currentUser.displayName + ' successfully deleted account of ' + user.displayName + '.'); | ||
console.log(currentUser.displayName + " successfully deleted account of " + user.displayName + "."); | ||
return null; | ||
}) | ||
.catch((error) => { | ||
return { error: 'Error deleting user: ' + error}; | ||
return { error: "Error deleting user: " + error }; | ||
}); | ||
return 'Success'; | ||
return "Success"; | ||
}); | ||
|
||
exports.setRoles = functions.https.onCall(async (data, context) => { | ||
if (!context.auth || context.auth.token.admin !== true) { | ||
return { | ||
error: "User not admin." | ||
} | ||
error: "User not admin.", | ||
}; | ||
} | ||
const currentUser = await admin.auth().getUser(context.auth.uid); | ||
const user = await admin.auth().getUserByEmail(data.email); | ||
const claims = { | ||
designer: data.designer, | ||
nickname: data.nickname, | ||
editor: data.editor, | ||
admin: data.admin | ||
} | ||
await admin.auth().setCustomUserClaims(user.uid, claims).then(() => { | ||
console.log(currentUser.displayName + ' successfully edited account of ' + user.displayName + '. Editor: ' + data.editor + ', admin: ' + data.admin); | ||
return null | ||
}).catch((error) => { | ||
return { error: 'Error setting roles: ' + error}; | ||
}) | ||
admin: data.admin, | ||
}; | ||
await admin | ||
.auth() | ||
.setCustomUserClaims(user.uid, claims) | ||
.then(() => { | ||
console.log( | ||
currentUser.displayName + | ||
" successfully edited account of " + | ||
user.displayName + | ||
". Designer: " + | ||
data.designer + | ||
", editor: " + | ||
data.editor + | ||
", admin: " + | ||
data.admin + | ||
", nickname: " + | ||
data.nickname | ||
); | ||
return null; | ||
}) | ||
.catch((error) => { | ||
return { error: "Error setting roles: " + error }; | ||
}); | ||
const newUser = await admin.auth().getUserByEmail(data.email); | ||
return newUser.customClaims; | ||
}); |
Binary file not shown.
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
Oops, something went wrong.