fix: setup to allow login to Medusa Cloud

This commit is contained in:
Sebastian Rindom
2021-04-22 18:44:55 +02:00
parent 7343a4e841
commit bbd2f02d54
13 changed files with 355 additions and 6966 deletions

View File

@@ -0,0 +1,56 @@
const axios = require("axios").default
const resolveCwd = require(`resolve-cwd`)
const Netrc = require("netrc-parser").default
module.exports = {
link: async argv => {
const apiHost =
process.env.MEDUSA_API_HOST || "https://api.medusa-commerce.com"
const hostMachine =
process.env.MEDUSA_HOST_MACHINE || "api.medusa-commerce.com"
function resolveLocalCommand(command) {
try {
const cmdPath = resolveCwd.silent(
`@medusajs/medusa/dist/commands/${command}`
)
return require(cmdPath).default
} catch (err) {
console.log("Could not find local user command.")
process.exit(1)
}
}
await Netrc.load()
if (!Netrc.machines[hostMachine]) {
console.log(
"You must login to Medusa Cloud first. Please run medusa login."
)
process.exit(1)
}
const { login, password } = Netrc.machines[hostMachine]
const { data: auth } = await axios
.get(`${apiHost}/auth`, {
auth: {
username: login,
password: password,
},
})
.catch(err => {
console.log(err)
process.exit(1)
})
if (auth.user) {
const localCmd = resolveLocalCommand(`user`)
return localCmd({
directory: argv.directory,
id: auth.user.id,
email: auth.user.email,
})
}
},
}

View File

@@ -0,0 +1,82 @@
const axios = require("axios").default
const open = require("open")
const Netrc = require("netrc-parser").default
const inquirer = require("inquirer")
module.exports = {
login: async _ => {
const apiHost =
process.env.MEDUSA_API_HOST || "https://api.medusa-commerce.com"
const authHost = process.env.MEDUSA_AUTH_HOST || `${apiHost}/cli-auth`
const loginHost =
process.env.MEDUSA_LOGIN_HOST || "https://admin.medusa-commerce.com"
const { data: urls } = await axios.post(authHost)
const qs = [
{
type: "input",
name: "open",
message: "Press enter key to open browser for login or n to exit",
},
]
await inquirer.prompt(qs).then(async a => {
if (a.open === "n") {
process.exit(0)
}
const bo = await open(`${loginHost}${urls.browser_url}`, {
app: "browser",
wait: false,
})
bo.on("error", err => {
console.warn(err)
console.log(
`Could not open browser go to: ${loginHost}${urls.browser_url}`
)
})
const fetchAuth = async (retries = 3) => {
try {
const { data: auth } = await axios.get(`${authHost}${urls.cli_url}`, {
headers: { authorization: `Bearer ${urls.cli_token}` },
})
return auth
} catch (err) {
if (retries > 0 && err.http && err.http.statusCode > 500)
return fetchAuth(retries - 1)
throw err
}
}
const auth = await fetchAuth()
const { data: user } = await axios
.get(`${apiHost}/auth`, {
auth: {
username: auth.username,
password: auth.password,
},
})
.catch(err => {
console.log(err)
process.exit(1)
})
await Netrc.load()
if (user) {
const hostMachine =
process.env.MEDUSA_HOST_MACHINE || "api.medusa-commerce.com"
if (!Netrc.machines[hostMachine]) {
Netrc.machines[hostMachine] = {}
}
Netrc.machines[hostMachine].login = auth.username
Netrc.machines[hostMachine].password = auth.password
}
await Netrc.save()
})
},
}

View File

@@ -0,0 +1,43 @@
const axios = require("axios").default
const resolveCwd = require(`resolve-cwd`)
const Netrc = require("netrc-parser").default
module.exports = {
whoami: async argv => {
const apiHost =
process.env.MEDUSA_API_HOST || "https://api.medusa-commerce.com"
const hostMachine =
process.env.MEDUSA_HOST_MACHINE || "api.medusa-commerce.com"
await Netrc.load()
if (!Netrc.machines[hostMachine]) {
console.log(
"You are not logged into Medusa Cloud. Please run medusa login."
)
process.exit(0)
}
const { login, password } = Netrc.machines[hostMachine]
const { data: auth } = await axios
.get(`${apiHost}/auth`, {
auth: {
username: login,
password: password,
},
})
.catch(err => {
console.log(err)
process.exit(1)
})
if (auth.user) {
console.log(`Hi, ${auth.user.first_name}! Here are your details:`)
console.log(`id: ${auth.user.id}`)
console.log(`email: ${auth.user.email}`)
console.log(`first_name: ${auth.user.first_name}`)
console.log(`last_name: ${auth.user.last_name}`)
}
},
}

View File

@@ -1,9 +1,14 @@
const path = require(`path`)
const resolveCwd = require(`resolve-cwd`)
const yargs = require(`yargs`)
const existsSync = require(`fs-exists-cached`).sync
const { getLocalMedusaVersion } = require(`./util/version`)
const { didYouMean } = require(`./did-you-mean`)
const existsSync = require(`fs-exists-cached`).sync
const { whoami } = require("./commands/whoami")
const { login } = require("./commands/login")
const { link } = require("./commands/link")
const handlerP = fn => (...args) => {
Promise.resolve(fn(...args)).then(
@@ -77,6 +82,30 @@ function buildLocalCommands(cli, isLocalProject) {
})
),
})
.command({
command: `whoami`,
desc: `View the details of the currently logged in user.`,
handler: handlerP(whoami),
})
.command({
command: `link`,
desc: `Creates your Medusa Cloud user in your local database for local testing.`,
handler: handlerP(argv => {
if (!isLocalProject) {
console.log("must be a local project")
cli.showHelp()
}
const args = { ...argv, ...projectInfo, useYarn }
return link(args)
}),
})
.command({
command: `login`,
desc: `Logs you into Medusa Cloud.`,
handler: handlerP(login),
})
.command({
command: `develop`,
desc: `Start development server. Watches file and rebuilds when something changes`,