Creating Account Activation Links for User Registration in Vaadin Applications
In this article, I'll show you how to generate activation links that you can send to the users so that they can complete the registration process.
Join the DZone community and get the full member experience.Join For Free
In a previous article, I explained how to implement a sign-up view to register new users within the application. In this article, I'll show you how to generate activation links that you can send to the users so that they can complete the registration process.
Generating a Random Activation Code
The general idea for implementing account activation codes is to generate a random string and store it in the database. You use that string to create a link to your application and send it to the user, for example via e-mail (covered in the next article). When the user clicks the link, you compare the string (or code) with the one you stored in the database, and if they match, you activate the user. So, you need to store two things per user:
- a hard-to-guess random code, and,
- a boolean that indicates whether the user has activated the account or not.
The activation code can be generated in the constructor:
Implementing the Logic to Activate Users/Accounts
In the service layer, you need a method to activate a user if the codes match. You can query the database and search for a user with the given code, and if it exists, you update the active flag and save it back to the database:
You should either make sure that the activation codes are unique or query the user by user name plus activation code. I'll let that as an exercise.
Implementing the Authentication Logic
You also need to update the authentication logic. You cannot authenticate a user unless it is active:
Defining the Activation Link (URL)
For now, and to make testing simpler, we'll send the URL to the standard output. You need to concatenate the URL of the activation view (implemented in the next step) and pass the activation code as a parameter:
Implementing the Activation View
Finally, you have to create the activation view mapped to the URL used in the previous step:
This view reads the parameters to get the activation code and attempts to activate the user.
We learned how to generate a random code that can be used in a URL to activate an account. In the next article, I'll show you how to send that URL via email.
Opinions expressed by DZone contributors are their own.