Skip to content

Instantly share code, notes, and snippets.

@pazel-io
Created September 13, 2022 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pazel-io/b496fa29176f9e6fa1f5f770e817f804 to your computer and use it in GitHub Desktop.
Save pazel-io/b496fa29176f9e6fa1f5f770e817f804 to your computer and use it in GitHub Desktop.
/**
* Handler that will be called during the execution of a PreUserRegistration flow.
*
* @param {Event} event - Details about the context and user that is attempting to register.
* @param {PreUserRegistrationAPI} api - Interface whose methods can be used to change the behavior of the signup.
*/
const axios = require('axios');
exports.onExecutePreUserRegistration = async (event, api) => {
const deviceSerialNumber = event.user?.user_metadata?.deviceSerialNumber;
let isValidSN = false;
const checkDeviceSN = async (deviceSerialNumber) => {
const apiKey = event.secrets.API_KEY;
const url = `https://api.example.com/v1/devices/${deviceSerialNumber}`;
const headers = {
'api-key': apiKey,
'Content-Type': 'application/json'
};
const response = await axios.get(url, { headers });
return response.data.isValid;
}
if(deviceSerialNumber){
// make a http call to your api and check the device SN
isValidSN = await checkDeviceSN(deviceSerialNumber);
}
if(!isValidSN){
return api.access.deny('Invalid device serial number', 'You need to provide a valid device serial number')
}
api.user.setAppMetadata('deviceCheckPassed', true)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment