After reading this article you'll learn:
How to create a register page
How to send an email to a newly registered user
How to activate a new user
how to create a login for the webuser
Starting off
First things first, we'll start with an empty application. In this new application, the first thing that we need to do is create a WebUser model. Add the following properties to this model:
Checkbox 'Active', default unchecked
Text (single line) 'First name', required
Text (single line) 'Last name', required
Password 'Password'
Email address 'Email address', has to be unique & required
Text (single line), 'Uuid'
Check if the WebUser model has a relation with the Role model, it should add the relation automatically if it doesn't then add the 'has and belongs to many' relation yourself.
Register page
Next up, let's create the register page. In this example, I'll use the header and footer page template for simplicity, make sure this page is public.
Drag a column component onto the page and add a create form component on top of that. Add the following properties to the form:
Note that we didn't add the active nor the uuid property. Hit 'Save' to finish up the page.
The create action
When we now navigate to our action tab we can see that the create form has automatically generated an action. Let's add some more steps and create a bit more functionality to this initial action.
The first thing that we should do is install two blocks from the block store, namely the 'generate random hex' & the 'send e-mail via smtp' block. The first block will generate a random text based on a number of characters that you can specify yourself. The second one will allow us to send an email to a webuser. Install these blocks to your organizations before continuing.
Navigate back to the actions tab and refresh the page if you don't see the 'generate random hex' or the 'send e-mail via smtp' function. When they appear drag the 'generate random hex' above the create step on the canvas, configure it like so:
This will cause the step to generate a 36-long character uuid that we will use later.
The next step is to save this uuid to the new webuser, open the create step and add 1 more property, select the webuser's uuid and then select the uuid variable we just created.
We'll also need to add a role to the new webuser. In order to do this add a new collection variable on the 'create' step. Name the variable webuser_role and select the Role model. Create a filter that retrieves the desired role, in my case I want the webuser to get the application role, so I'll filter is the name is equal to my application name.
Hit 'Save' and add it to the new webuser like this:
Alright, before we continue with the email function there's something to note. For this you need an email provider, your company might have one in that case feel free to use it. But for the sake of this example and the chance that someone might not have this available use Ethereal Email. This website is a fake smtp service that we can use to send emails, simply click the 'create ethereal account' button, your account will be created and a temporary email will be set up for you that we can use.
Alright, let's get back to our action and drag the 'send e-mail via smtp' function onto our canvas after the create step. Configure it according to the smtp configurations of your company or from ethereal. For our template we'll need two template variables, one is the uuid we created earlier and the other variable is the first name of our newly created webuser, this will make the email a bit more personal.
We can now create our template, you can copy the template below or create your own template. Note that you need to manually add the template variables.
Hello {{first_name}},
<br>
<br>
Welcome to the family! You can activate your account by <a href="https://[APPLICATIOND_IDENTIFIER].betty.app/activate/{{uuid}}"><b>clicking here</b></a>.
<br>
<br>
With kind regards,
<br>
[YOUR_NAME or COMPANY_NAME]
The step will look like this:
Another note is that you can see that we use a URL in the body template above that doesn't exist yet. Let's fix that!
Activating account
Create a new page, use the header and footer template and name the page as followed:
The ':uuid' part of the URL will be the unique uuid that we create for our webuser. If we would send the email to a new webuser the URL that he has to click to activate his account will be something like '[APPLICATION_NAME].betty.app/activate/9b69784e-9672-4ff1-97b5-064a549554da'. Using a colon :
in the URL means that it'll contain an input variable in our case the input variable is the uuid.
And since we're already creating pages, quickly add a login page and a home page as well before we continue with the activating account functionality. Both pages use the header and footer template. For the home page make sure that the type is 'authenticated', the rest of the pages are public.
Head back to the activate page, drag a column component on the canvas and an update form on top of it. Configure the update form as followed:
The next thing that'll do is adding our uuid in a page variable so that we can use it in our filter. Go to the variables tab of your page, delete any variables that were already present, and create a new one. Select 'Text' and make sure that the variable has the same name as the input in this case 'uuid'.
We can now use our input in the filter of the update form. Select the update form and change the filter so that it'll filter on the uuid of the webuser.
Since we don't need anything besides the uuid value; delete everything in the form except for the alert message and the hidden input. For our action we need the input variable so make sure the hidden input is made like this:
Now, let's make sure the action is updated.
Navigate the action of the form, select the Start step and delete any input and action variables that are resent except the uuid variable.
We'll use the uuid input variable to retrieve the webuser. Go to the action variables and create the following webuser object.
Now head to the update step. Select the web_user_object and make sure the active property is checked.
This will make sure that our user will be activated when this action is triggered.
Now we can do some magic, we don't want the webuser to do anything on this page. All we want to do is make sure that the active checkbox is checked once he clicks the link in the email that we sent.
We'll use interactions to automatically submit this form and update the active status of the user after which he'll automatically get redirected to the login page and he'll receive a small alert informing him that his account activation has been successful.
The first interaction that we'll add is while the page is rendering we'll submit the update form.
When the form has been submitted we want to redirect the webuser to the login page. It'd be nice to show an alert informing the user his activation has succeeded. Before we continue add an input text variable on the login page called 'activated'.
Go back to the activate page and add the second interaction as followed:
Because we just added the 'activated' variable we can now give it a value, in this case, I'll use 'Y' to indicate that the activation was successful.
Login page
Navigate. to the login page. Drag a column component on the canvas and inside that a login form component. Select Web User as the authentication profile and the 'Home' page as the page the user gets redirected to.
To inform the user his activation has succeeded; add a conditional component above the alert of the login form. Configure it like so:
Inside the conditional component drag an alert component and configure it to make it look similar to this:
And of course, we only want activated users to be able to log in. Go to the action of the login form. And on the Start step add an action variable like this:
We'll use this in a conditional step to check if the webuser object is activated. Drag a conditional step above the authenticate user step that's already there.
Drag the authenticate user step in the success flow, this will only allow users who've activated their account to be eligible to log in to the application.
Select our active webuser object variable we just created to be true. Now ideally we'd drag the authenticate user step into the true flow but during the writing of this article variables used inside the conditional step aren't passed through and available as output for the finish step. To work around this simply install the raise error step from the block store.
With this step installed drag it into the 'else' flow. This'll ensure that an error will be raised and it will stop the action. Keep the authenticate user step after the condition step, it'll trigger when the flow is true since if it'd be false the action will stop. We can now use the output 'jwt' from the authenticate user step in the finish step. In the future this step will likely be redundant and it should be possible to use the authenticate user in the true flow of the conditional step.
And that's it for the login part.
Home page and wrapping up
Once the user has logged in he'll land on the home page. Let's add a welcome message for the user and call it a day.
On the home page, add a column component and inside of it a data container. Configure the data container like this:
Now inside the data container component add a title component and put a welcome message in there and select the webuser his name.
To finalize the application go over each page and hit the Play button in the top left corner to compile all the pages we've created.
You'll likely get an error while rendering:
To fix this, navigate to your authentication profile tab (found in the tools section), or click on the arrow in the error message. Select the login page, and now render the page again.
After that make sure the great of the pages are compiled by clicking the Play button and test the application by going to your register page and register a user.
After registering head to your mailbox, either ethereal mail or the one of your organization. If you use ethereal mail you might have to create a new inbox if it has expired. Simply create a new mailbox and add the username and password to the register action.
When we click the link we'll get redirected to the activate webuser page which will activate the account and redirect the user to the login page.
Now we can login!
And there we go, we're logged in.
We've just successfully registered a user, send an out an email to confirm the account, activated the account and logged the user in.