Skip to content

Commit

Permalink
fix(platform): fix login/logout control flow
Browse files Browse the repository at this point in the history
Before this fix, the logout command would require the user to be logged
in, which prevented the user from logging out when they had an
invalid/expired auth token persisted in their local store.
  • Loading branch information
thsig committed Apr 21, 2020
1 parent 7bf3191 commit 8f3defd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion garden-service/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function makeDummyGarden(root: string, gardenOpts: GardenOpts = {})
}
gardenOpts.config = config

return DummyGarden.factory(root, gardenOpts)
return DummyGarden.factory(root, { ...gardenOpts, noPlatform: true })
}

// The help text for these commands is only displayed when calling `garden options`.
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/cloud/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function login(platformUrl: string, log: LogEntry): Promise<string>
/**
* Checks with the backend whether the provided client auth token is valid.
*/
async function checkClientAuthToken(token: string, platformUrl: string, log: LogEntry): Promise<boolean> {
export async function checkClientAuthToken(token: string, platformUrl: string, log: LogEntry): Promise<boolean> {
let valid
try {
await got({
Expand Down
14 changes: 11 additions & 3 deletions garden-service/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import { deline, naturalList } from "./util/string"
import { ensureConnected } from "./db/connection"
import { DependencyValidationGraph } from "./util/validate-dependencies"
import { Profile } from "./util/profiling"
import { readAuthToken, login } from "./cloud/auth"
import { readAuthToken, checkClientAuthToken } from "./cloud/auth"
import { ResolveModuleTask, getResolvedModules } from "./tasks/resolve-module"
import username from "username"

Expand Down Expand Up @@ -96,6 +96,7 @@ export interface GardenOpts {
log?: LogEntry
plugins?: RegisterPluginParam[]
sessionId?: string
noPlatform?: boolean
}

export interface GardenParams {
Expand Down Expand Up @@ -291,15 +292,22 @@ export class Garden {

const clientAuthToken = await readAuthToken(log)
// If a client auth token exists in local storage, we assume that the user wants to be logged in to the platform.
if (clientAuthToken && sessionId) {
if (clientAuthToken && !opts.noPlatform) {
if (!platformUrl) {
const errMsg = deline`
GARDEN_CLOUD environment variable is not set. Make sure it is set to the appropriate API
backend endpoint (e.g. myusername-cloud-api.cloud.dev.garden.io, without an http/https
prefix).`
throw new InternalError(errMsg, {})
} else {
await login(platformUrl, log)
const tokenIsValid = await checkClientAuthToken(clientAuthToken, platformUrl, log)
if (!tokenIsValid) {
log.warn(deline`
You were previously logged in to the platform, but your session has expired or is invalid. Please run
${chalk.bold("garden login")} to continue using platform features, or run ${chalk.bold("garden logout")}
to suppress this message.
`)
}
}
}

Expand Down

0 comments on commit 8f3defd

Please sign in to comment.