Create A Simple End-User Login System With User-Based Licensing

Published on: August 10, 2022
LicenseSpring Guide
Table of Contents:

Unlike using a key-based license, user-based licenses depend on an email and password to activate a license. This tutorial will exhibit how to implement a login feature using the LicenseSpring SDK.

By the end of this tutorial, you should understand how to create a login feature by activating a license, how to create a logout feature by deactivating a license, and how to routinely perform online checks on your user-based license from your program. We will also consider the best practices for using a login mechanism within your application, creating a password reset feature, as well as LicenseSpring particularities between a trial and normal user-based licenses.

For illustration, we will create a ChatBot that operates as a simple login application. This tutorial will not cover Installing the SDK, Issuing Licenses, Offline activation, de-activation or checks.

Here is the repo for our login chatbot using the LicenseSpring C++ SDK.

Note: Since this article is a continuation of the initial tutorial, it is assumed that readers meet the prerequisites previously stated. Readers should also have a thorough understanding of license activation, deactivation, and checking.

Here are the steps we will go through:

  • Implementing the Login/Logout Functionality
  • Best Practices for Opening the Application
  • How to Reset a Password
  • Differences between Standard and Trial Licenses

Implementing the Login/Logout Functionality

Logging In

Creating a login system with the user-based license is straightforward. You can ask the user to input their email and password combination. Once we have the user's credentials, we can then take that email/password combination and use it to activate our user-based license. How the user inputs their credentials is up to you.

cpp
csharp
java
1 2 3 4 std::string email = "UserInputtedEmail@example.com"; std::string password = "UserInputtedPassword"; auto licenseId = LicenseID::fromUser( email, password ); auto license = licenseManager->activateLicense( licenseId );

Logging Out

Once a user decides to log out, we can deactivate their license.

cpp
csharp
java
1 license->deactivate( true );

Note: deactivating a license on most SDKs will delete the local license file from your device. Some SDKs will have an optional parameter where you can decide whether to keep the local license file after deactivation. For information on where the local copy is stored on your device, see the Getting Started Tutorial.

Best Practices for Opening the Application

License Checks

It is recommended to perform a local license check at application open to confirm that the local license file belongs to the current device and has not been transferred. It is also useful to check whether the local license file has been tampered with and whether the local license is still valid.  For more information on performing local license checks, see the Getting Started Tutorial.

cpp
csharp
java
1 2 license->localCheck(); //Perform a local (offline) check license->check(); //Perform an online check

How to Store User Data Safely

User data such as email address, first name, last name, license ID, order ID, and order store ID can all be retrieved through the SDK. Initial passwords are also available on your local license file, but once the password has been changed, you will no longer be able to access that data using the LicenseSpring SDK. All this user data is stored and encrypted on your local license file. The SDK allows you to retrieve the data and decrypt it. For more on license encryption/decryption see our tutorial on Safety/Security Considerations.

Here is how to retrieve a "User Data" object that contains such information

cpp
csharp
java
1 license->licenseUser();

Logging off Idle Users

Because activation spots on your license are limited, you may want to set a grace period on your application for users who are logged in, but are idle. To do this, the SDK provides offline tools that retrieve the date a license was last checked.

cpp
csharp
java
1 2 tm last_checked_date = license->lastCheckDate(); //Returns the date the license was last checked int days = license->daysPassedSinceLastCheck(); //Returns the number of days since the last check

You can combine this with any method that routinely runs on an interval to see if a user has performed a license check within that interval. If they have not, you can log that user off by deactivating their license, freeing that activation spot for another user.

How to Reset a Password

We can allow users to change their password. This can be done by the License Manager through the platform, but we can also allow end-users to change their password using our SDK. To change the password, we will require the original/old password and a new, replacement password. If the old password is correct, and the new password is valid, then the user's password will be changed. Note: this method requires internet connection.

cpp
csharp
java
1 license->changePassword( "old_passord", "new_passord" );

Prompting for Password Reset on First Login

New accounts are created with a temporary, initial password. We can check if a license user is still using an initial password and prompt them to change their password using the following code:

cpp
csharp
java
1 2 3 4 5 auto user = license->licenseUser(); if ( user->isInitialPassword() ) { //Prompt user to change password, use the change password method from the previous code block. }

Differences between Trial and Non-Trial User-Based Licenses

User-based trial licenses work differently from key-based trial licenses. Unlike key-based licenses, user-based trial licenses do not generate a new trial username/password combination like how key-based trial licenses generate new trial keys. User-based trial licenses require a valid email to be inputted before creating a trial license. If the account associated with that email is still using its initial password, then the trial license will automatically use that initial password to activate the trial license. If that account is not using its initial password, i.e. it has changed its password, then you will also need to set the password within the code using the SDK.

cpp
csharp
java
1 2 3 4 5 6 7 auto licenseId = licenseManager->getTrialLicense( "email@example.com" ); //The password field will be empty if the email account is not using an initial password. if ( licenseId.password().empty() ) { licenseId.setPassword( "password associated with email account" ); } auto license = licenseManager->activateLicense( licenseId ); //returns a trial license

To learn more about Trial Licenses see the Trial License Tutorial.

Conclusion

You now have the basic tools and knowledge to set up your own login system using user-based licensing. It is important to note that this tutorial has only given you the bare minimum required to create a login system. It will be up to you to choose how you want to implement your login application, and how you will integrate LicenseSpring into said application. For a look at how a simple console login program can work using LicenseSpring and user-based licenses, take a look at our chatbot login sample code.

FAQ

Q: Why am I getting an error/exception when trying to activate my license?

There are multiple reasons why you cannot activate your license. The most common would be you inputted your configuration (API key, Shared key, Product code) or your key/username-password combination incorrectly. It could also fail if your license is disabled on the platform, if it has expired, or if there are no available activation spots. If it's not any of those issues, it is most likely a connectivity issue. Check your internet connection and try again. The C++, .NET, and Java SDKs offer very useful exceptions for each possible error. Using the documentation on the exception you are getting will be very useful.

Q: I deactivated my license (and deleted my local license file), but the SDK still says I have a license.

This may occur on your C++ SDK when you deactivate a license and delete the local license file and you use LicenseManager::getCurrentLicense(). Although the local license file is deleted, a local license copy still exists within your memory while the application is still running. If there is a license object loaded into memory, LicenseManager::getCurrentLicense() will return that license object, instead of going into your files and looking for your local license file. This might be why you are returning a license object that is "inactive", which could cause issues later on. You can fix this by either, restarting the application, thus wiping the license object from memory, using LicenseManager::clearLocalStorage() which deletes the license object from memory as well, or LicenseManager::reloadLicense() instead of LicenseManager::getCurrentLicense(), which only grabs the license from your license file, not memory.

Q: What is the difference between a local (offline) check and an online check?

An online check will do everything a local check does, but it will also sync up your local license with the LicenseSpring servers. For example, if one license user increases their consumption count, and they run an online check, the platform will reflect this new consumption count, and if another license user runs an online check, their local license will now also reflect this updated consumption count. A local offline check is useful for cases where the user and device does not have a guaranteed internet connection, and it also helps to limit the number of API calls to the LicenseSpring servers, while maintaining the validity of the license.

Q: How do I know if my change password was successful?

Similar to license activation, if an error occurred, such as an incorrect or invalid previous/new password, the SDK will throw an exception. These will inform you if the password change was successful.

Q: My user-based trial license is not able to activate.

There are a few possible reasons for this. As stated above, if the account associated with the email applying for a trial license has changed their password before, they will need to manually add their password to the trial license, or it will simply pass an empty password and fail the activation. Another possible reason is that the license does not allow trial licenses. To see more about how to set up a trial license, see our Trial License Tutorial.