Skip to content

Commit

Permalink
Merge pull request #122 from FirebaseExtended/@invertase/update-auth-…
Browse files Browse the repository at this point in the history
…claims-deps

chore(firestore-auth-claims): update dependencies
  • Loading branch information
cabljac authored May 31, 2023
2 parents 686c059 + 8681b33 commit f828f17
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 74 deletions.
1 change: 1 addition & 0 deletions _emulator/extensions/firestore-auth-claims.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LOCATION=europe-west2
3 changes: 1 addition & 2 deletions _emulator/firebase.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extensions": {
"firestore-record-user-acknowledgements": "../firestore-record-user-acknowledgements",
"firestore-bundle-server": "../firestore-bundle-server"
"firestore-auth-claims": "../firestore-auth-claims"
},
"storage": {
"rules": "storage.rules"
Expand Down
9 changes: 9 additions & 0 deletions _emulator/functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Compiled JavaScript files
lib/**/*.js
lib/**/*.js.map

# TypeScript v1 declaration files
typings/

# Node.js dependency directory
node_modules/
25 changes: 25 additions & 0 deletions _emulator/functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "functions",
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^11.4.1",
"firebase-functions": "^4.1.0",
"@types/express-serve-static-core": "4.17.30"
},
"devDependencies": {
"typescript": "^4.6.4"
},
"private": true
}
8 changes: 8 additions & 0 deletions _emulator/functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as functions from "firebase-functions";

export const findDocumentReferences = functions.https.onRequest(
(request, response) => {
/** Simply send back the data that we have posted */
response.send(["searchFunction/testing/functions-testing/example"]);
}
);
13 changes: 13 additions & 0 deletions _emulator/functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"compileOnSave": true,
"include": ["src"]
}
9 changes: 9 additions & 0 deletions firestore-auth-claims/functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Compiled JavaScript files
lib/**/*.js
lib/**/*.js.map

# TypeScript v1 declaration files
typings/

# Node.js dependency directory
node_modules/
13 changes: 6 additions & 7 deletions firestore-auth-claims/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
"lint": "eslint \"src/**/*\"",
"build": "tsc",
"generate-readme": "node ../../generate-experimental-readme.js firestore-auth-claims > ../README.md"

},
"main": "lib/index.js",
"dependencies": {
"@types/node": "^12.19.3",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"firebase-admin": "^8.6.0",
"firebase-functions": "^3.6.1"
"@types/node": "^18.14.6",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"firebase-admin": "^11.5.0",
"firebase-functions": "^4.2.1"
},
"devDependencies": {
"eslint": "^7.6.0",
"eslint-plugin-import": "^2.22.0",
"prettier": "^2.1.2",
"typescript": "^4.1.3"
"typescript": "^4.9.5"
},
"private": true
}
134 changes: 69 additions & 65 deletions firestore-auth-claims/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,80 @@ admin.initializeApp();
const auth = admin.auth();

const CLAIMS_FIELD: string | null = process.env.CLAIMS_FIELD || null;
const CLAIMS_COLLECTION = process.env.CLAIMS_COLLECTION || "user_claims";

exports.sync = functions.handler.firestore.document.onWrite(async (change) => {
const uid = change.after.id;
try {
// make sure the user exists (can be fetched) before trying to set claims
await auth.getUser(uid);
} catch (e) {
functions.logger.error(
`Unable to sync claims for user '${uid}', error:`,
e,
{ uid }
);
}
exports.sync = functions.firestore
.document(CLAIMS_COLLECTION)
.onWrite(async (change) => {
const uid = change.after.id;
try {
// make sure the user exists (can be fetched) before trying to set claims
await auth.getUser(uid);
} catch (e) {
functions.logger.error(
`Unable to sync claims for user '${uid}', error:`,
e,
{ uid }
);
}

if (
!change.after.exists ||
(CLAIMS_FIELD && !change.after.get(CLAIMS_FIELD))
) {
functions.logger.info(
`Claims for user '${uid}' were deleted, removing from Auth.`,
{ uid }
);
return auth.setCustomUserClaims(uid, null);
}
if (
!change.after.exists ||
(CLAIMS_FIELD && !change.after.get(CLAIMS_FIELD))
) {
functions.logger.info(
`Claims for user '${uid}' were deleted, removing from Auth.`,
{ uid }
);
return auth.setCustomUserClaims(uid, null);
}

const beforeData =
(CLAIMS_FIELD ? change.before.get(CLAIMS_FIELD) : change.before.data()) ||
{};
const data =
(CLAIMS_FIELD ? change.after.get(CLAIMS_FIELD) : change.after.data()) || {};
// don't write the _synced field to Auth
if (data._synced) {
delete data._synced;
}
if (beforeData._synced) {
delete beforeData._synced;
}
const beforeData =
(CLAIMS_FIELD ? change.before.get(CLAIMS_FIELD) : change.before.data()) ||
{};
const data =
(CLAIMS_FIELD ? change.after.get(CLAIMS_FIELD) : change.after.data()) ||
{};
// don't write the _synced field to Auth
if (data._synced) {
delete data._synced;
}
if (beforeData._synced) {
delete beforeData._synced;
}

if (isDeepStrictEqual(beforeData, data)) {
// don't persist identical claims
return;
}
if (isDeepStrictEqual(beforeData, data)) {
// don't persist identical claims
return;
}

functions.logger.info(
`Updating claims for user '${uid}', setting keys ${Object.keys(data).join(
", "
)}.`,
{ uid }
);
if (typeof data !== "object") {
functions.logger.error(
`Invalid custom claims for user '${uid}'. Must be object, was ${JSON.stringify(
data
)}`,
functions.logger.info(
`Updating claims for user '${uid}', setting keys ${Object.keys(data).join(
", "
)}.`,
{ uid }
);
return;
}
await auth.setCustomUserClaims(uid, data);
if (typeof data !== "object") {
functions.logger.error(
`Invalid custom claims for user '${uid}'. Must be object, was ${JSON.stringify(
data
)}`,
{ uid }
);
return;
}
await auth.setCustomUserClaims(uid, data);

const fpath = ["_synced"];
if (CLAIMS_FIELD) {
fpath.unshift(CLAIMS_FIELD);
}
functions.logger.info(
`Claims set for user '${uid}', logging sync time to Firestore`,
{ uid }
);
return change.after.ref.update(
new admin.firestore.FieldPath(...fpath),
admin.firestore.FieldValue.serverTimestamp()
);
});
const fpath = ["_synced"];
if (CLAIMS_FIELD) {
fpath.unshift(CLAIMS_FIELD);
}
functions.logger.info(
`Claims set for user '${uid}', logging sync time to Firestore`,
{ uid }
);
return change.after.ref.update(
new admin.firestore.FieldPath(...fpath),
admin.firestore.FieldValue.serverTimestamp()
);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"lint": "prettier --list-different \"**/*.{js,md,yml,ts,json,yaml}\"",
"clean": "lerna run --parallel clean && lerna clean",
"build": "lerna run --parallel build",
"local:emulator": "cd _emulator && firebase emulators:start -P demo-test",
"generate-readmes": "lerna run --parallel generate-readme",
"postinstall": "lerna bootstrap --no-ci && lerna run --parallel clean && npm run build",
"prepare": "husky install"
Expand Down

0 comments on commit f828f17

Please sign in to comment.