UNPKG

@darksnyder/license-key

Version:

License key Generator

113 lines (95 loc) 3.4 kB
function checkAndAddUserEmailAndLicenseKey() { const email = Session.getActiveUser().getEmail(); const licenseKey = generateLicenseKey(); const endpoint = `${NEXT_PUBLIC_SUPABASE_URL}/rest/v1/users`; // First, check if the email already exists in Supabase const checkOptions = { method: "get", headers: { apikey: NEXT_PUBLIC_SUPABASE_KEY, Authorization: `Bearer ${NEXT_PUBLIC_SUPABASE_KEY}`, }, muteHttpExceptions: true, }; const checkResponse = UrlFetchApp.fetch( `${endpoint}?email=eq.${email}`, checkOptions ); const checkData = JSON.parse(checkResponse.getContentText()); // If email already exists, return early if (checkData.length > 0) { console.log("User already exists. Skipping addition."); return; } // If email doesn't exist, add the user const payload = { email: email, licenseKey: licenseKey, }; const addOptions = { method: "post", headers: { "Content-Type": "application/json", apikey: NEXT_PUBLIC_SUPABASE_KEY, Authorization: `Bearer ${NEXT_PUBLIC_SUPABASE_KEY}`, Prefer: "return=minimal", }, payload: JSON.stringify(payload), muteHttpExceptions: true, }; const addResponse = UrlFetchApp.fetch(endpoint, addOptions); // Check if add operation was successful if (addResponse.getResponseCode() === 201) { console.log("Email and license key successfully added to Supabase"); } else { console.error( "Failed to add email and license key:", addResponse.getContentText() ); } } function generateLicenseKey() { const keyLength = 16; // Total characters in the license key const keySegments = 4; // Number of segments const segmentLength = keyLength / keySegments; // Length of each segment // Characters allowed in the license key (uppercase letters and numbers) const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; let licenseKey = ""; // Generate each segment of the license key for (let i = 0; i < keyLength; i++) { // Add a random character from the allowed set licenseKey += characters.charAt( Math.floor(Math.random() * characters.length) ); // Add a dash after each segment (except the last one) if ((i + 1) % segmentLength === 0 && i < keyLength - 1) { licenseKey += "-"; } } return licenseKey; } function getLicenseKeyByEmail() { const email = Session.getActiveUser().getEmail(); // Get the user email const endpoint = `${NEXT_PUBLIC_SUPABASE_URL}/rest/v1/users?email=eq.${email}&select=licenseKey`; const options = { method: "get", headers: { apikey: NEXT_PUBLIC_SUPABASE_KEY, Authorization: `Bearer ${NEXT_PUBLIC_SUPABASE_KEY}`, }, muteHttpExceptions: true, }; try { const response = UrlFetchApp.fetch(endpoint, options); const data = JSON.parse(response.getContentText()); if (response.getResponseCode() === 200 && data.length > 0) { return data[0].licenseKey; // Return the license key if found } else { console.log("No license key found for this email."); return null; // Return null if no license key is found } } catch (error) { console.error("Error fetching license key:", error); return null; // Return null in case of an error } }