UNPKG

5.9 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2022 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.getOpts = exports.beforeOperation = exports.beforeSmsSent = exports.beforeEmailSent = exports.beforeUserSignedIn = exports.beforeUserCreated = exports.HttpsError = void 0;
25const identity_1 = require("../../common/providers/identity");
26Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return identity_1.HttpsError; } });
27const trace_1 = require("../trace");
28const manifest_1 = require("../../runtime/manifest");
29const options = require("../options");
30const onInit_1 = require("../../common/onInit");
31/**
32 * Handles an event that is triggered before a user is created.
33 * @param optsOrHandler - Either an object containing function options, or an event handler (run before user creation).
34 * @param handler? - If defined, an event handler which is run every time before a user is created.
35 */
36function beforeUserCreated(optsOrHandler, handler) {
37 return beforeOperation("beforeCreate", optsOrHandler, handler);
38}
39exports.beforeUserCreated = beforeUserCreated;
40/**
41 * Handles an event that is triggered before a user is signed in.
42 * @param optsOrHandler - Either an object containing function options, or an event handler (run before user signin).
43 * @param handler - Event handler which is run every time before a user is signed in.
44 */
45function beforeUserSignedIn(optsOrHandler, handler) {
46 return beforeOperation("beforeSignIn", optsOrHandler, handler);
47}
48exports.beforeUserSignedIn = beforeUserSignedIn;
49/**
50 * Handles an event that is triggered before an email is sent to a user.
51 * @param optsOrHandler- Either an object containing function options, or an event handler that is run before an email is sent to a user.
52 * @param handler - Event handler that is run before an email is sent to a user.
53 */
54function beforeEmailSent(optsOrHandler, handler) {
55 return beforeOperation("beforeSendEmail", optsOrHandler, handler);
56}
57exports.beforeEmailSent = beforeEmailSent;
58/**
59 * Handles an event that is triggered before an SMS is sent to a user.
60 * @param optsOrHandler - Either an object containing function options, or an event handler that is run before an SMS is sent to a user.
61 * @param handler - Event handler that is run before an SMS is sent to a user.
62 */
63function beforeSmsSent(optsOrHandler, handler) {
64 return beforeOperation("beforeSendSms", optsOrHandler, handler);
65}
66exports.beforeSmsSent = beforeSmsSent;
67/** @hidden */
68function beforeOperation(eventType, optsOrHandler, handler) {
69 if (!handler || typeof optsOrHandler === "function") {
70 handler = optsOrHandler;
71 optsOrHandler = {};
72 }
73 const { opts, ...blockingOptions } = getOpts(optsOrHandler);
74 // Create our own function that just calls the provided function so we know for sure that
75 // handler takes one argument. This is something common/providers/identity depends on.
76 const annotatedHandler = Object.assign(handler, { platform: "gcfv2" });
77 const func = (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)((0, identity_1.wrapHandler)(eventType, annotatedHandler)));
78 const legacyEventType = `providers/cloud.auth/eventTypes/user.${eventType}`;
79 /** Endpoint */
80 const baseOptsEndpoint = options.optionsToEndpoint(options.getGlobalOptions());
81 const specificOptsEndpoint = options.optionsToEndpoint(opts);
82 func.__endpoint = {
83 ...(0, manifest_1.initV2Endpoint)(options.getGlobalOptions(), opts),
84 platform: "gcfv2",
85 ...baseOptsEndpoint,
86 ...specificOptsEndpoint,
87 labels: {
88 ...baseOptsEndpoint === null || baseOptsEndpoint === void 0 ? void 0 : baseOptsEndpoint.labels,
89 ...specificOptsEndpoint === null || specificOptsEndpoint === void 0 ? void 0 : specificOptsEndpoint.labels,
90 },
91 blockingTrigger: {
92 eventType: legacyEventType,
93 options: {
94 ...((eventType === "beforeCreate" || eventType === "beforeSignIn") && blockingOptions),
95 },
96 },
97 };
98 func.__requiredAPIs = [
99 {
100 api: "identitytoolkit.googleapis.com",
101 reason: "Needed for auth blocking functions",
102 },
103 ];
104 func.run = handler;
105 return func;
106}
107exports.beforeOperation = beforeOperation;
108/** @hidden */
109function getOpts(blockingOptions) {
110 const accessToken = blockingOptions.accessToken || false;
111 const idToken = blockingOptions.idToken || false;
112 const refreshToken = blockingOptions.refreshToken || false;
113 const opts = { ...blockingOptions };
114 delete opts.accessToken;
115 delete opts.idToken;
116 delete opts.refreshToken;
117 return {
118 opts,
119 accessToken,
120 idToken,
121 refreshToken,
122 };
123}
124exports.getOpts = getOpts;