saget-auth-middleware
Version:
A comprehensive authentication middleware for Node.js applications with SSO integration, JWT validation, and role-based access control
128 lines (107 loc) ⢠4.01 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { generateJSMiddleware } from '../generators/js-generator.js';
import { generateTSMiddleware } from '../generators/ts-generator.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export async function initCommand(options) {
console.log(chalk.blue.bold('\nš SAGET Auth Middleware Generator\n'));
let config = { ...options };
// Interactive mode
if (options.interactive || (!options.type && !options.framework)) {
const answers = await inquirer.prompt([
{
type: 'list',
name: 'type',
message: 'What type of project are you using?',
choices: [
{ name: 'JavaScript', value: 'js' },
{ name: 'TypeScript', value: 'ts' }
],
default: options.type || 'js'
},
{
type: 'list',
name: 'framework',
message: 'Which framework are you using?',
choices: [
{ name: 'Next.js', value: 'next' },
{ name: 'Express.js', value: 'express' }
],
default: options.framework || 'next'
},
{
type: 'input',
name: 'output',
message: 'Where should we create the middleware files?',
default: options.output || '.'
},
{
type: 'confirm',
name: 'createEnv',
message: 'Create example environment file?',
default: true
}
]);
config = { ...config, ...answers };
}
try {
// Create output directory
const outputDir = path.resolve(config.output);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
console.log(chalk.yellow(`\nš Creating middleware in: ${outputDir}\n`));
// Generate middleware based on type
if (config.type === 'ts') {
await generateTSMiddleware(outputDir, config);
} else {
await generateJSMiddleware(outputDir, config);
}
// Create environment file if requested
if (config.createEnv) {
createEnvFile(outputDir);
}
console.log(chalk.green.bold('\nā
Middleware generated successfully!\n'));
// Show next steps
showNextSteps(config);
} catch (error) {
console.error(chalk.red.bold('\nā Error generating middleware:'), error.message);
process.exit(1);
}
}
function createEnvFile(outputDir) {
const envContent = `# SAGET SSO Configuration
SSO_BASE_URL=https://your-sso-server.com
SSO_CLIENT_ID=your-client-id
SSO_CLIENT_SECRET=your-client-secret
SSO_REDIRECT_URI=http://localhost:3000/auth/callback
# Optional: Custom configuration
# SSO_SCOPE=openid profile email
# SSO_RESPONSE_TYPE=code
# SSO_GRANT_TYPE=authorization_code
`;
const envPath = path.join(outputDir, '.env.example');
fs.writeFileSync(envPath, envContent);
console.log(chalk.green('š Created .env.example'));
}
function showNextSteps(config) {
console.log(chalk.cyan.bold('š Next Steps:\n'));
console.log(chalk.white('1. Install the middleware package:'));
console.log(chalk.gray(' npm install saget-auth-middleware\n'));
console.log(chalk.white('2. Configure your environment variables:'));
console.log(chalk.gray(` Copy ${config.output}/.env.example to .env and update values\n`));
if (config.framework === 'next') {
console.log(chalk.white('3. Add middleware to your Next.js app:'));
console.log(chalk.gray(` Import and use the middleware from ${config.output}/\n`));
} else {
console.log(chalk.white('3. Add middleware to your Express app:'));
console.log(chalk.gray(` Import and use the middleware from ${config.output}/\n`));
}
console.log(chalk.white('4. Test your authentication:'));
console.log(chalk.gray(' Start your application and test the auth flow\n'));
console.log(chalk.blue('š Documentation: https://github.com/your-org/saget-auth-middleware\n'));
}