Created
October 12, 2023 16:41
-
-
Save zacjones93/d7900f726305507bf85f9fc77909c94f to your computer and use it in GitHub Desktop.
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
// Menu: egghead Admin Login As User | |
// Author: Ian Jones | |
// Cache: true | |
// Keyword: admin | |
import _ from "lodash" | |
let eggheadUserToken = await env("EGGHEAD_AUTH_TOKEN"); | |
const eggheadAuthHeaders = { | |
Authorization: `Bearer ${eggheadUserToken}`, | |
}; | |
export const siteData = { | |
"egghead.io": { | |
siteName: "egghead.io", | |
name: "[e]gghead.io", | |
id: "d25ada502a252a89c5f80fbbae70320785cd73cd54e46a00226ddcf6a446df0c", | |
httpUrl: "https://egghead.io", | |
}, | |
epic_react: { | |
siteName: "epic_react", | |
name: "EpicReact.dev", | |
id: "4118545974333dd5a03999d7b141ec809b9e83725630934e907e7205a9ac83cf", | |
httpUrl: "https://epicreact.dev/", | |
}, | |
pro_testing: { | |
siteName: "pro_testing", | |
name: "TestingJavascript.com", | |
id: "870edf7cfb5f6a3088fa8580452f90e111df42f7b46de9c969832bb8205ac38d", | |
httpUrl: "https://testingjavascript.com", | |
}, | |
pure_react: { | |
siteName: "pure_react", | |
name: "PureReact.com", | |
id: "fce86213683a8809c54687be20a2d7f5683213f6e45a024445e8a1c45f418104", | |
httpUrl: "https://purereact.com", | |
}, | |
tech_interviews: { | |
siteName: "tech_interviews", | |
name: "TechnicalInterviews.dev", | |
id: "870e164f049d1ea513c54bc6ff97b14fb2b22403a65e0a71770cea4ec96642c9", | |
httpUrl: "https://technicalinterviews.dev", | |
}, | |
just_javascript: { | |
siteName: "just_javascript", | |
name: "JustJavascript.com", | |
id: "3a75acdd3214c8b2b7fef0c3f1972bc833305bfa3686cc62dcb504c8e22fbc56", | |
httpUrl: "https://justjavascript.com", | |
}, | |
}; | |
export const siteIdChoices = Object.keys(siteData).map((key) => ({ | |
name: siteData[key].name, | |
value: siteData[key].id, | |
})); | |
export const siteChoices = Object.keys(siteData).map((key) => ({ | |
name: siteData[key].name, | |
value: siteData[key], | |
})); | |
const site = await arg("Which site do you want to log into?", siteChoices); | |
const siteUrl = site.httpUrl; | |
const siteName = site.siteName; | |
const email = await arg({ | |
placeholder: "Whats the email of the user you want to view as?", | |
ignoreBlur: true | |
}); | |
//const email = "[email protected]"; | |
const userData = await get("https://app.egghead.io/api/v1/users/search", { | |
params: { q: email }, | |
headers: eggheadAuthHeaders, | |
}).then((r) => r.data); | |
console.log({userData}) | |
const purchasedFromSite = _.chain(userData) | |
.get("users.[0].purchased", []) | |
.filter({ site: siteName }) | |
.value(); | |
if (purchasedFromSite.length === 0 && siteName !== "egghead.io") { | |
setPlaceholder(`This user does not have a purchase from ${site.name}`); | |
await wait(3000); | |
exit(0); | |
} | |
let continueScript = true; | |
if (siteName !== "egghead.io") { | |
const pppCheck = _.reduce( | |
purchasedFromSite, | |
(pppObject, currentSellablePurchase) => { | |
const currentPurchasePPPCountry = _.get( | |
currentSellablePurchase, | |
"coupon.restricted_to_country_name", | |
false | |
); | |
const upgradedFromPurchase = _.get( | |
currentSellablePurchase, | |
"upgraded_from_purchase_id" | |
); | |
if (currentPurchasePPPCountry) { | |
pppObject.country = currentPurchasePPPCountry; | |
} | |
if (upgradedFromPurchase) { | |
pppObject.upgraded = true; | |
} | |
return pppObject; | |
}, | |
{ country: undefined, upgraded: false } | |
); | |
if (pppCheck.country && !pppCheck.upgraded) { | |
continueScript = await arg( | |
`This user is from ${pppCheck.country}, are you connected to a vpn there?`, | |
[ | |
{ name: "Yes", value: true }, | |
{ name: "No", value: false }, | |
] | |
); | |
} | |
} | |
if (!continueScript) { | |
exit(0); | |
} | |
const signInUrl = `${siteUrl}?show-as-user=${email}#access_token=${eggheadUserToken}`; | |
copy(signInUrl); | |
await applescript(String.raw` | |
activate application "Google Chrome" | |
tell application "System Events" to ¬ | |
click menu item "New Incognito Window" of ¬ | |
menu "File" of menu bar 1 of ¬ | |
application process "Google Chrome" | |
tell application "Google Chrome" to ¬ | |
set URL of active tab of ¬ | |
front window to "${signInUrl}" | |
`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment