Skip to content

Commit

Permalink
Merge pull request #13 from SySagar/fix
Browse files Browse the repository at this point in the history
🚀 feat : authentication added
  • Loading branch information
SySagar authored Jul 4, 2024
2 parents cdc1cf8 + e55cb15 commit acc6304
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-apes-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitemo-cli': minor
---

authentication for cli
2 changes: 2 additions & 0 deletions .github/workflows/build_and_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
run: |
export CLIENT_URL=${{ secrets.CLIENT_URL }}
export API_KEY=${{ secrets.API_KEY }}
export KEY_FILENAME=${{ secrets.KEY_FILENAME }}
npm install --frozen-lockfile
npm run build --if-present
- name: Create Release Pull Request or Publish
Expand All @@ -31,6 +32,7 @@ jobs:
with:
publish: npm run release
title: "📦 Version packages"
commit: "📦 package: Version packages"
env:
GITHUB_TOKEN: ${{ secrets.REPO_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
15 changes: 15 additions & 0 deletions src/utils/findGitemoCommands.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import FLAGS from '@constants/flag.js';
import requireLogin from './requireLogin';
import chalk from 'chalk';

const isSupportedCommand = (command, options) => {
return Object.keys(options).includes(command);
Expand Down Expand Up @@ -38,6 +40,19 @@ const getOptionsForCommand = (command, flags, input, type) => {
const findGitemoCommand = (cli, options) => {
const { command, type } = determineCommand(cli.flags, cli.input, options);

var key = requireLogin(command);

if (command === FLAGS.LOGIN) {
if (key && key !== 1) {
console.log(chalk.bold.green(`\n\nYou are already logged in\n\n`));
return;
}
}

if (!key || key === undefined) {
return;
}

if (!command || !isSupportedCommand(command, options)) {
return cli.showHelp();
}
Expand Down
25 changes: 25 additions & 0 deletions src/utils/requireLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os from 'os';
import path from 'path';
import chalk from 'chalk';
import { readFileSync } from 'fs';
import dotenv from 'dotenv';
dotenv.config();

function requireLogin(command) {
try {
const FILENAME = process.env.KEY_FILENAME;
const homeDir = os.homedir();
const filePath = path.join(homeDir, FILENAME);
const fileData = readFileSync(filePath, 'utf8');
const { key } = JSON.parse(fileData);
return key;
} catch (error) {

Check warning on line 16 in src/utils/requireLogin.js

View workflow job for this annotation

GitHub Actions / publish

'error' is defined but never used
if (command === 'login') {
return 1;
}
console.log(chalk.red(`No key found! Please login first\n\n`));
console.log(`use ${chalk.cyan('gitemo login')} to login\n`);
}
}

export default requireLogin;

0 comments on commit acc6304

Please sign in to comment.