Skip to content

Commit

Permalink
refactor: license check
Browse files Browse the repository at this point in the history
  • Loading branch information
june07 committed Aug 27, 2024
1 parent af79982 commit 4134dc0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/analytics.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const JUNE07_ANALYTICS_URL = 'https://analytics.june07.com';

(async function(analytics) {
(async function (analytics) {
analytics.push = async (options) => {
const userInfo = (await chrome.storage.local.get('userInfo')).userInfo || await getUserInfo();
const userInfo = (await chrome.storage.local.get('userInfo')).userInfo || await getUserInfo()

await fetch(`${JUNE07_ANALYTICS_URL}${options.event === 'install' ? '/install' : '/'}`, {
method: 'POST',
headers: {
Expand All @@ -13,6 +14,6 @@ const JUNE07_ANALYTICS_URL = 'https://analytics.june07.com';
userInfo,
onInstalledReason: options.onInstalledReason
})
});
})
}
})(typeof module !== 'undefined' && module.exports ? module.exports : (self.analytics = self.analytics || {}));
})(typeof module !== 'undefined' && module.exports ? module.exports : (self.analytics = self.analytics || {}))
13 changes: 5 additions & 8 deletions src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,16 @@ async function openTab(host = 'localhost', port = 9229, manual) {
delete cache.tabs[cacheId]
}
}
async function getLicenseStatus(id) {
async function getLicenseStatus() {
try {
const userInfo = (await chrome.storage.local.get('userInfo'))?.userInfo || await getUserInfo()
const response = await fetch(`https://${LICENSE_HOST}/v1/license/nim`, {
method: "POST",
headers: {
'Accept': "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({ userId: id })
body: JSON.stringify({ userInfo })
})
if (response.status !== 200) {
return { error: new Error('unable to get license status') }
Expand All @@ -435,7 +436,7 @@ async function getLicenseStatus(id) {
}
async function checkLicenseStatus() {
const { checkedLicenseOn } = await chrome.storage.local.get('checkedLicenseOn')
const notificationDuration = 1000 * 60 * 60 * 24
const notificationDuration = /production|test/.test(ENV) ? 1000 * 60 * 60 * 24 : 60000

// Debouncing
if (cache.isCheckingLicense) {
Expand All @@ -447,11 +448,7 @@ async function checkLicenseStatus() {
if (!checkedLicenseOn || checkedLicenseOn < Date.now() - (notificationDuration / 2)) {
await chrome.storage.local.set({ checkedLicenseOn: Date.now() })

const { id } = await chrome.identity.getProfileUserInfo()

// send the id to brakecode and see what the license status is for the user... if not paid for this month, show the stripe pay link

const { oUserId, license, error } = await getLicenseStatus(id)
const { oUserId, license, error } = await getLicenseStatus()

if (license?.valid || error) {
return
Expand Down

0 comments on commit 4134dc0

Please sign in to comment.