No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cloudFunctions.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AuthCloudFunction = void 0;
  4. const uuid = require("uuid");
  5. const types_1 = require("../types");
  6. const emulatorLogger_1 = require("../emulatorLogger");
  7. const registry_1 = require("../registry");
  8. class AuthCloudFunction {
  9. constructor(projectId) {
  10. this.projectId = projectId;
  11. this.logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.AUTH);
  12. this.enabled = false;
  13. this.enabled = registry_1.EmulatorRegistry.isRunning(types_1.Emulators.FUNCTIONS);
  14. }
  15. async dispatch(action, user) {
  16. if (!this.enabled)
  17. return;
  18. const userInfoPayload = this.createUserInfoPayload(user);
  19. const multicastEventBody = this.createEventRequestBody(action, userInfoPayload);
  20. const c = registry_1.EmulatorRegistry.client(types_1.Emulators.FUNCTIONS);
  21. let res;
  22. let err;
  23. try {
  24. res = await c.post(`/functions/projects/${this.projectId}/trigger_multicast`, multicastEventBody);
  25. }
  26. catch (e) {
  27. err = e;
  28. }
  29. if (err || (res === null || res === void 0 ? void 0 : res.status) !== 200) {
  30. this.logger.logLabeled("WARN", "functions", `Firebase Authentication function was not triggered due to emulation error. Please file a bug.`);
  31. }
  32. }
  33. createEventRequestBody(action, userInfoPayload) {
  34. return {
  35. eventId: uuid.v4(),
  36. eventType: `providers/firebase.auth/eventTypes/user.${action}`,
  37. resource: {
  38. name: `projects/${this.projectId}`,
  39. service: "firebaseauth.googleapis.com",
  40. },
  41. params: {},
  42. timestamp: new Date().toISOString(),
  43. data: userInfoPayload,
  44. };
  45. }
  46. createUserInfoPayload(user) {
  47. return {
  48. uid: user.localId,
  49. email: user.email,
  50. emailVerified: user.emailVerified,
  51. displayName: user.displayName,
  52. photoURL: user.photoUrl,
  53. phoneNumber: user.phoneNumber,
  54. disabled: user.disabled,
  55. metadata: {
  56. creationTime: user.createdAt
  57. ? new Date(parseInt(user.createdAt, 10)).toISOString()
  58. : undefined,
  59. lastSignInTime: user.lastLoginAt
  60. ? new Date(parseInt(user.lastLoginAt, 10)).toISOString()
  61. : undefined,
  62. },
  63. customClaims: JSON.parse(user.customAttributes || "{}"),
  64. providerData: user.providerUserInfo,
  65. tenantId: user.tenantId,
  66. mfaInfo: user.mfaInfo,
  67. };
  68. }
  69. }
  70. exports.AuthCloudFunction = AuthCloudFunction;