Skip to content

Commit

Permalink
fix: already logged in (#77)
Browse files Browse the repository at this point in the history
* fix: already logged in

* format fix and test add
  • Loading branch information
imrishabh18 authored Feb 14, 2025
1 parent 44fc97a commit 15cd180
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cli/auth/login/register.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import type { Command } from "commander"
import { setSessionToken } from "lib/cli-config"
import { setSessionToken, getSessionToken } from "lib/cli-config"
import delay from "delay"
import { getKy } from "lib/registry-api/get-ky"
import type { EndpointResponse } from "lib/registry-api/endpoint-types"

export const registerAuthLogin = (program: Command) => {
// Define the login action once to share between both commands
const loginAction = async () => {
const sessionToken = getSessionToken()
if (sessionToken) {
console.log(
"Already logged in! Use 'tsci logout' if you need to switch accounts.",
)
return
}

const ky = getKy()

const { login_page } = await ky
Expand Down
4 changes: 4 additions & 0 deletions lib/cli-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const cliConfig: TypedConfigstore<CliConfig> = new Configstore(
"tscircuit",
)

export const getSessionToken = (): string | undefined => {
return cliConfig.get("sessionToken")
}

export const setSessionToken = (token: string) => {
cliConfig.set("sessionToken", token)
const decoded = jwtDecode<{
Expand Down
23 changes: 23 additions & 0 deletions tests/cli/auth/login.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect, test } from "bun:test"
import { setSessionToken } from "lib/cli-config"
import { getCliTestFixture } from "../../fixtures/get-cli-test-fixture"

// A dummy valid JWT token (header.payload.signature)
const dummyJwtToken =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ." +
"SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"

test("login command: already logged in", async () => {
const { runCommand } = await getCliTestFixture()

// Simulate an already logged in state by setting a session token.
setSessionToken(dummyJwtToken)

const { stdout, stderr } = await runCommand("tsci login")

expect(stderr).toBe("")
expect(stdout).toContain(
"Already logged in! Use 'tsci logout' if you need to switch accounts.",
)
})

0 comments on commit 15cd180

Please sign in to comment.