Add the slack-notify
npm package from the Actions editor to send a Slack message when a new user registers for your application.
Create a Post User Registration action.
Go to api.slack.com
create a new app, active Incoming Webhooks, and get your Slack Webhook URL.
Your webhook URL contains a secret. Don't share it online, including public Git repositories.
In the Action editor store the Slack Webhook URL as a secret and add the slack-notify
npm package.
In the Actions editor enter the follow code:
const initSlackNotify = require('slack-notify')
exports.onExecutePostUserRegistration = async (event) => {
const slack = initSlackNotify(event.secrets.SLACK_WEBHOOK_URL);
const message = `New User: ${event.user.email}`
slack.success({
text: message
})
};
Before deploying test out the Action by click the Test icon. Once test is successful click deploy.
Then click Add to flow and drag the Action into the Post User Registration flow.
Instructor: [0:00] Now, let's go a little deeper into Actions features. Let's create a new action. I'm going to Actions and library. We want to click on Build Custom, I'm going to put Send Slack Message. This will be a Post User Registration. This will be after the user is saved to the database is when this action will run. [0:22] Then we'll click create. When the user registers for a new account on our app, this Send Slack Message action will run and it will send a message to a Slack channel. In order to do this, you need a Slack workspace that you are the admin of. If you're not, you can easily make one for free.
[0:43] Once you have that created, go to api.slack.com and then click create app. This will bring up the modal for create app. Let's click from scratch and then the app name will be Auth0 Slack.
[1:04] Then I will pick my Will's Coding workspace. This is a workspace that I'm the admin of. Then I will click create app. Then scroll down to incoming webhooks and then click on for activate incoming webhooks.
[1:17] Then scroll down and click on add new webhook to workspace, and then the slack app will request permission to access your slack workspace. I will put general and put allow. I will allow the app to post to that channel.
[1:38] If you scroll down, you will see now you have a webhook URL. You want to copy this and keep this secret. We don't want anyone to have access to this. Now, head back to the send slack message action. Then on the left-hand side, let's click secrets then we will click add secret then the name will be slack webhook URL.
[2:04] Then we will put in the value.
[2:08] Next, click on the box icon, and now we can add a dependency. Now we can add a npm package. Let's click add dependency. Then we're going to go to slack notify and leave version blank if you don't want to specify a version number. It will default to the latest.
[2:27] Then click create, exit out of that menu. The first thing we'll do is we will require the slack notify npm package and we'll reference it as slack notify. Inside of the post user registration function, I'm going to paste in some code and then we will go over it.
[2:48] The first thing we're doing is passing to slack notify the slack webhook URL. We access that with the event object of going event.secrets.slackwebhook URL, and then we'll reference it as slack. We'll create a message variable that will contain a template string of new user and the user's email address that we get from event.user.email.
[3:15] We'll call slack notify with the success method and we'll path in an object with the key of text and the value of the message variable that we created up here. Before we deploy this new action, let's test it out. I have the wheels coding slack open as well as our action. Let's go to the left hand side and click the play button. This will simulate a call to the action. Let's go down to run.
[3:50] As you can see, the test was successful. There's no error messages. We see that our off zero slack app sent us the message of new user with an email address. This is the sample email address that you get here in the test. This is JaySmith@example.com. X that out and let's click deploy, then click add to flow.
[4:20] Let's add sense like message to the user registered and interrupts it before it's complete to send the slack message. Then we'll click apply. Now I have the demo application running. Let's click login, click sign up, and we'll use the same user I've been using. I deleted him out of user management. Let's click continue and authorize the app.
[4:54] As you can see, our RC roll slack app sent us a message of new user with the email address that I used to sign up. This lesson really lets you see the power that actions gives you. You're able to store secret securely. You're able to install a NPM package and run tests on the application and test the action, all without leaving the off zero dashboard...