node.jstypescriptgoogle-cloud-platformgoogle-identitygoogle-identity-toolkit

Identity platform auth blocking function deploy in typescript


I am trying to write an auth blocking function to prevent users creating accounts from the client side in google identity platform. I have written it in typescript but I am struggling to deploy it

Here is a the website about the blocking functions https://cloud.google.com/identity-platform/docs/blocking-functions

My actual function looks like this

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as gcipCloudFunctions from 'gcip-cloud-functions';
const authClient = new gcipCloudFunctions.Auth();


exports.beforeCreateBlockingFunction = authClient.functions().beforeCreateHandler((user, context) => {
    throw new gcipCloudFunctions.https.HttpsError('invalid-argument', `Unauthorized email "${user.email}"`);
});

  1. Have I got the method signature correct?

  2. I am trying to deploy it using the following command. I am not sure whether this works with typescript. Is this the correct way?

// Http trigger with Cloud Functions.
// gcloud functions deploy beforeCreateBlockingFunction --runtime nodejs10 --region europe-west1 --trigger-http --allow-unauthenticated

Any help is appreciated. Kind regards


Solution

  • Declaration of method for typescript is not correct.. name of function don't have to be beforeCreate.. you can name it as per your convention. Refer below TS code for beforeCreateHandler event

    import * as gcipCloudFunctions from 'gcip-cloud-functions';
    const authClient = new gcipCloudFunctions.Auth();
    
    export const beforeRegister = authClient.functions().beforeCreateHandler((user: gcipCloudFunctions.UserRecord, 
              context: gcipCloudFunctions.AuthEventContext) => {
      console.log("User Object: " + JSON.stringify(user));
      console.log("Context Object: " + JSON.stringify(context));
      
      return {
        customClaims: {
          verified: true
        },      
      };
    });