Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
foo/
*.log
.invalid.json
.vscode
75 changes: 42 additions & 33 deletions commands/account.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
const { Command } = require('commander');
const { v4: uuidv4 } = require('uuid');
const CONSTANTS = require('../lib/constants');
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const auth = require('../lib/auth');
const chalk = require('chalk');

const accountCommand = new Command('account');

accountCommand
.description('Manage your account')
.action(() => {
const blessnetDir = path.join(os.homedir(), '.blessnet');
const tokenPath = path.join(blessnetDir, 'auth_token');
const isLoggedIn = fs.existsSync(tokenPath); // Check if auth_token file exists
.action(async () => {
const isLoggedIn = auth.isLoggedIn();
if (isLoggedIn) {
console.log(`You are ${chalk.green('logged in.')}`);
// Check if authentication is valid (includes auto-refresh)
const validJWT = await auth.getValidJWT();
if (validJWT) {
console.log(`You are ${chalk.green('logged in.')}`);
} else {
console.log(`You are ${chalk.red('logged out.')} Run: ${chalk.blue('blessnet options account login')}`);
}
} else {
console.log(`You are ${chalk.red('logged out.')}`);
console.log(`You are ${chalk.red('logged out.')} Run: ${chalk.blue('blessnet options account login')}`);
}
});

Expand All @@ -26,34 +28,43 @@ accountCommand
.description('Log in to your account')
.action(() => {
const guid = uuidv4();
console.log(`Please log in at: ${CONSTANTS.authHost}/?lid=${guid}&clientid=${CONSTANTS.blessAuthClientId}`);
console.log(`Please log in at: ${CONSTANTS.authHost}/?lid=${guid}&clientId=${CONSTANTS.blessAuthClientId}`);

const checkLoginStatus = async () => {
try {
const response = await fetch(`${CONSTANTS.authHost}/api/verify-activity`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
lid: guid,
clientid: CONSTANTS.blessAuthClientId
})
});
const response = await fetch(
`${CONSTANTS.authHost}/api/v1/auth/verify-activity`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
lid: guid,
clientId: CONSTANTS.blessAuthClientId
})
}
);
const text = await response.text();
if (text.length === 0) {
return;
}

const data = JSON.parse(text); // Parse the response text as JSON
if (data.token) {
console.log('Log in successful!');
const blessnetDir = path.join(os.homedir(), '.blessnet');
if (!fs.existsSync(blessnetDir)) {
fs.mkdirSync(blessnetDir);
const data = JSON.parse(text);

if (data.jwt && data.refreshToken) {
// Verify JWT before storing
const isValid = await auth.verifyJWT(data.jwt);
if (!isValid) {
console.error('JWT verification failed. Please try again.');
return;
}
fs.writeFileSync(path.join(blessnetDir, 'auth_token'), data.token);

console.log('Log in successful!');
auth.setAuth(data.jwt, data.refreshToken);
process.exit(0);
} else {
console.log('Response missing jwt or refreshToken');
}
} catch (error) {
console.error('Error checking log in status:', error);
Expand All @@ -71,13 +82,11 @@ accountCommand
.command('logout')
.description('Logout from your account')
.action(() => {
const blessnetDir = path.join(os.homedir(), '.blessnet');
const tokenPath = path.join(blessnetDir, 'auth_token');
if (fs.existsSync(tokenPath)) {
fs.unlinkSync(tokenPath);
console.log('Logout successful. Token destroyed.');
const success = auth.clearAuth();
if (success) {
console.log('Logout successful. Tokens cleared.');
} else {
console.log('No token found. You are not logged in.');
console.log('Error during logout.');
}
});

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const { parseTomlConfig } = require('./lib/config'); // Import parseTomlConfig
// checks
const isLinked = require('node:fs').existsSync(require('node:path').join(__dirname, 'node_modules', '.bin'));
const version = isLinked ? `${packageJson.version}-dev` : packageJson.version;
const auth = require('./lib/auth');
const blessnetDir = path.join(require('node:os').homedir(), '.blessnet');
const runtimePath = path.join(blessnetDir, 'bin', `bls-runtime${process.platform === 'win32' ? '.exe' : ''}`);
const authTokenPath = path.join(blessnetDir, 'auth_token');
const isLoggedIn = fs.existsSync(authTokenPath);
const isLoggedIn = auth.isLoggedIn();

// commands
const initCommand = require('./commands/init');
Expand Down
Loading