-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add verify and authenticate command
- Loading branch information
1 parent
6dae058
commit 9f033de
Showing
4 changed files
with
132 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": ["oclif", "oclif-typescript", "prettier"] | ||
"extends": ["oclif", "prettier"] | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Command } from "@oclif/core"; | ||
import axios from "axios"; | ||
import chalk from "chalk"; | ||
import * as fs from "node:fs"; | ||
import * as path from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const configPath = path.join(__dirname, "..", "..", "config.json"); | ||
|
||
export default class Verify extends Command { | ||
static description = "Verify if the saved authentication token is valid"; | ||
|
||
static examples = ["$ <%= config.bin %> <%= command.id %>"]; | ||
|
||
async run() { | ||
console.log(chalk.blue.bold("\nVerifying Authentication Token")); | ||
|
||
if (!fs.existsSync(configPath)) { | ||
this.error( | ||
chalk.red( | ||
"No configuration file found. Please authenticate first using `authenticate` command.", | ||
), | ||
); | ||
} | ||
|
||
const configFileContent = fs.readFileSync(configPath, "utf8"); | ||
const config = JSON.parse(configFileContent); | ||
const { token, url } = config; | ||
|
||
if (!url || !token) { | ||
this.error( | ||
chalk.red( | ||
"Incomplete authentication details. Please authenticate again using `authenticate` command.", | ||
), | ||
); | ||
} | ||
|
||
try { | ||
console.log(`\n${chalk.blue("Validating token...")}`); | ||
|
||
const response = await axios.post( | ||
`${url}/api/trpc/auth.verifyToken`, | ||
{ | ||
json: { | ||
token, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}, | ||
); | ||
|
||
if (response.data.result.data.json) { | ||
this.log(chalk.green("Token is valid.")); | ||
} else { | ||
this.error( | ||
chalk.red( | ||
"Invalid token. Please authenticate again using `authenticate` command.", | ||
), | ||
); | ||
} | ||
} catch (error) { | ||
this.error( | ||
chalk.red( | ||
// @ts-ignore | ||
`Failed to verify token: ${error.message}. Please authenticate again using 'authenticate' command.`, | ||
), | ||
); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {runCommand} from '@oclif/test' | ||
import {expect} from 'chai' | ||
|
||
describe('verify', () => { | ||
it('runs verify cmd', async () => { | ||
const {stdout} = await runCommand('verify') | ||
expect(stdout).to.contain('hello world') | ||
}) | ||
|
||
it('runs verify --name oclif', async () => { | ||
const {stdout} = await runCommand('verify --name oclif') | ||
expect(stdout).to.contain('hello oclif') | ||
}) | ||
}) |