Skip to content

Instantly share code, notes, and snippets.

@ezekg
Last active July 7, 2022 22:08
Show Gist options
  • Save ezekg/18d251ddf09f8e9082f2b1bc502f4eb9 to your computer and use it in GitHub Desktop.
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)
// 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()
})
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