Checking licenses in the application

So far, we have generated a new license, and we've received the license ID in our confirmation email. Now, it's time to validate this ID in the application code:

  1. Switch to the main.js file and append the following code to the createWindow function:
Nucleus.checkLicense('556e3bb5e9f5e230d884', (err, license) => {
if (err) return console.error(err);

if (license.valid) {
console.log('License is valid :) Using policy ' +
license.policy);
} else {
console.log('License is invalid :(');
}
});

Here, we're using the Nucleus.checkLicense function to get a callback with two parameters: an err, which provides any error details, and license, which provides details about the license.

For the sake of simplicity, we're going to redirect the check to the console output.

  1. Run the application with the npm start script and check the console's output. You should see the following line in your console:
License is valid :) Using policy a409f54f-b799-48e6-99ec-4d46bc4101a6
  1. For testing purposes, try to change the license ID to some other value and restart the application once again. This time, the console's output should be as follows:
License is invalid :(

Congratulations on integrating license checking into your Electron application.

In a real-life project, you will probably see a dialog asking for a license key. After doing so, you should store it somewhere and provide the different behaviors of the application based on the license's validation checks.

Please note that we have only touched on the basics of licensing and policies. You may want to harden storage and security, as well as encryption for your locally stored license values. Check out the Keygen (https://keygen.sh/) service if you want to find out more about sophisticated services and APIs.

Now, let's summarize what we have learned in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.155.58