Last active
July 7, 2022 22:08
-
-
Save ezekg/18d251ddf09f8e9082f2b1bc502f4eb9 to your computer and use it in GitHub Desktop.
How to add license key validation and automatic updates to an Electron app (more here: https://github.com/keygen-sh/example-electron-license-activation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use electron-builder's Keygen integration for automatic updates | |
const { autoUpdater } = require('electron-updater') | |
const { ipcMain } = require('electron') | |
// Listen for a signal from renderer to start auto-updates | |
ipcMain.on('license:valid', async (_event, license) => { | |
// Pass in a license key to authenticate with the API | |
autoUpdater.addAuthHeader(`License ${license.attributes.key}`) | |
// Check for updates | |
autoUpdater.checkForUpdatesAndNotify() | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ipcRenderer } from 'electron' | |
import fetch from 'node-fetch' | |
const res = await fetch(`https://api.keygen.sh/v1/accounts/demo/licenses/${encodeURIComponent(key)}/actions/validate`, { | |
method: 'POST', | |
headers: { | |
'content-type': 'application/vnd.api+json', | |
accept: 'application/vnd.api+json', | |
authorization: `License ${key}`, | |
}, | |
body: JSON.stringify({ | |
meta: { | |
scope: { fingerprint }, | |
} | |
}) | |
}) | |
const { meta, data, errors } = await res.json() | |
if (errors) { | |
const [err] = errors | |
throw new Error(`${err.title}: ${err.detail}`) | |
} | |
if (meta.valid) { | |
ipcRenderer.send('license:valid', data) | |
} else { | |
ipcRenderer.send('license:invalid') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment