Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
fix(login): Fixed --key option
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jan 20, 2018
1 parent b77d2c8 commit 5b8cc06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cli/packages/prisma-cli-core/src/commands/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export default class Login extends Command {
async run() {
const { key } = this.flags

await this.client.login()
await this.client.login(key)
}
}
67 changes: 34 additions & 33 deletions cli/packages/prisma-cli-engine/src/Client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,20 @@ export class Client {
token?: string,
workspaceSlug?: string,
): Promise<any> {
const result = await fetch(
this.env.activeCluster.getImportEndpoint(
serviceName,
stage,
workspaceSlug,
),
{
method: 'post',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: exportData,
},
const endpoint = this.env.activeCluster.getImportEndpoint(
serviceName,
stage,
workspaceSlug,
)
debug(`Uploading to endpoint ${endpoint}`)
const result = await fetch(endpoint, {
method: 'post',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: exportData,
})

const text = await result.text()
try {
Expand Down Expand Up @@ -378,39 +377,41 @@ export class Client {
}

async ensureAuth(): Promise<void> {
let authenticated
try {
authenticated = Boolean(await this.getAccount())
} catch (e) {
//
}
const authenticated = await this.isAuthenticated()

if (!authenticated) {
await this.login()
}
}

async login(key?: string): Promise<void> {
const secret = await this.requestCloudToken()

const url = `${this.config.consoleEndpoint}/cli-auth?secret=${secret}`
let token = key
this.out.action.start(`Authenticating`)
if (!token) {
const secret = await this.requestCloudToken()

this.out.log(`Opening ${url} in the browser\n`)
const url = `${this.config.consoleEndpoint}/cli-auth?secret=${secret}`

opn(url)
this.out.log(`Opening ${url} in the browser\n`)

this.out.action.start(`Authenticating`)
opn(url)

let token
while (!token) {
const cloud = await this.cloudTokenRequest(secret)
if (cloud.token) {
token = cloud.token
while (!token) {
const cloud = await this.cloudTokenRequest(secret)
if (cloud.token) {
token = cloud.token
}
await new Promise(r => setTimeout(r, 500))
}
this.env.globalRC.cloudSessionKey = token
} else {
this.env.globalRC.cloudSessionKey = token
const authenticated = await this.isAuthenticated()
if (!authenticated) {
throw new Error('The provided key is invalid')
}
await new Promise(r => setTimeout(r, 500))
}

this.env.globalRC.cloudSessionKey = token
this.env.saveGlobalRC()
debug(`Saved token ${token}`)

Expand Down

0 comments on commit 5b8cc06

Please sign in to comment.