UNPKG

7 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2017 Firebase
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.UserBuilder = exports._userWithOptions = exports.user = exports.service = exports.provider = exports.HttpsError = exports.userRecordConstructor = exports.UserRecordMetadata = void 0;
25const identity_1 = require("../../common/providers/identity");
26Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return identity_1.HttpsError; } });
27Object.defineProperty(exports, "userRecordConstructor", { enumerable: true, get: function () { return identity_1.userRecordConstructor; } });
28Object.defineProperty(exports, "UserRecordMetadata", { enumerable: true, get: function () { return identity_1.UserRecordMetadata; } });
29const cloud_functions_1 = require("../cloud-functions");
30const manifest_1 = require("../../runtime/manifest");
31/** @internal */
32exports.provider = "google.firebase.auth";
33/** @internal */
34exports.service = "firebaseauth.googleapis.com";
35/**
36 * Handles events related to Firebase Auth users events.
37 *
38 * @param userOptions - Resource level options
39 * @returns UserBuilder - Builder used to create functions for Firebase Auth user lifecycle events
40 *
41 * @public
42 */
43function user(userOptions) {
44 return _userWithOptions({}, userOptions || {});
45}
46exports.user = user;
47/** @internal */
48function _userWithOptions(options, userOptions) {
49 return new UserBuilder(() => {
50 if (!process.env.GCLOUD_PROJECT) {
51 throw new Error("process.env.GCLOUD_PROJECT is not set.");
52 }
53 return "projects/" + process.env.GCLOUD_PROJECT;
54 }, options, userOptions);
55}
56exports._userWithOptions = _userWithOptions;
57/**
58 * Builder used to create functions for Firebase Auth user lifecycle events.
59 * @public
60 */
61class UserBuilder {
62 static dataConstructor(raw) {
63 return (0, identity_1.userRecordConstructor)(raw.data);
64 }
65 /* @internal */
66 constructor(triggerResource, options, userOptions) {
67 this.triggerResource = triggerResource;
68 this.options = options;
69 this.userOptions = userOptions;
70 }
71 /**
72 * Responds to the creation of a Firebase Auth user.
73 *
74 * @param handler Event handler that responds to the creation of a Firebase Auth user.
75 *
76 * @public
77 */
78 onCreate(handler) {
79 return this.onOperation(handler, "user.create");
80 }
81 /**
82 * Responds to the deletion of a Firebase Auth user.
83 *
84 * @param handler Event handler that responds to the deletion of a Firebase Auth user.
85 *
86 * @public
87 */
88 onDelete(handler) {
89 return this.onOperation(handler, "user.delete");
90 }
91 /**
92 * Blocks request to create a Firebase Auth user.
93 *
94 * @param handler Event handler that blocks creation of a Firebase Auth user.
95 *
96 * @public
97 */
98 beforeCreate(handler) {
99 return this.beforeOperation(handler, "beforeCreate");
100 }
101 /**
102 * Blocks request to sign-in a Firebase Auth user.
103 *
104 * @param handler Event handler that blocks sign-in of a Firebase Auth user.
105 *
106 * @public
107 */
108 beforeSignIn(handler) {
109 return this.beforeOperation(handler, "beforeSignIn");
110 }
111 onOperation(handler, eventType) {
112 return (0, cloud_functions_1.makeCloudFunction)({
113 handler,
114 provider: exports.provider,
115 eventType,
116 service: exports.service,
117 triggerResource: this.triggerResource,
118 // eslint-disable-next-line @typescript-eslint/unbound-method
119 dataConstructor: UserBuilder.dataConstructor,
120 legacyEventType: `providers/firebase.auth/eventTypes/${eventType}`,
121 options: this.options,
122 });
123 }
124 beforeOperation(handler, eventType) {
125 var _a, _b, _c, _d, _e, _f;
126 const accessToken = ((_b = (_a = this.userOptions) === null || _a === void 0 ? void 0 : _a.blockingOptions) === null || _b === void 0 ? void 0 : _b.accessToken) || false;
127 const idToken = ((_d = (_c = this.userOptions) === null || _c === void 0 ? void 0 : _c.blockingOptions) === null || _d === void 0 ? void 0 : _d.idToken) || false;
128 const refreshToken = ((_f = (_e = this.userOptions) === null || _e === void 0 ? void 0 : _e.blockingOptions) === null || _f === void 0 ? void 0 : _f.refreshToken) || false;
129 // Create our own function that just calls the provided function so we know for sure that
130 // handler takes two arguments. This is something common/providers/identity depends on.
131 const wrappedHandler = (user, context) => handler(user, context);
132 const func = (0, identity_1.wrapHandler)(eventType, wrappedHandler);
133 const legacyEventType = `providers/cloud.auth/eventTypes/user.${eventType}`;
134 func.__trigger = {
135 labels: {},
136 ...(0, cloud_functions_1.optionsToTrigger)(this.options),
137 blockingTrigger: {
138 eventType: legacyEventType,
139 options: {
140 accessToken,
141 idToken,
142 refreshToken,
143 },
144 },
145 };
146 func.__endpoint = {
147 platform: "gcfv1",
148 labels: {},
149 ...(0, manifest_1.initV1Endpoint)(this.options),
150 ...(0, cloud_functions_1.optionsToEndpoint)(this.options),
151 blockingTrigger: {
152 eventType: legacyEventType,
153 options: {
154 accessToken,
155 idToken,
156 refreshToken,
157 },
158 },
159 };
160 func.__requiredAPIs = [
161 {
162 api: "identitytoolkit.googleapis.com",
163 reason: "Needed for auth blocking functions",
164 },
165 ];
166 func.run = handler;
167 return func;
168 }
169}
170exports.UserBuilder = UserBuilder;