@aws-amplify/cli-internal
Version:
Amplify CLI
79 lines • 3.78 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthGenerator = void 0;
const node_path_1 = __importDefault(require("node:path"));
const promises_1 = __importDefault(require("node:fs/promises"));
const ts_1 = require("../../ts");
const auth_renderer_1 = require("./auth.renderer");
class AuthGenerator {
constructor(gen1App, backendGenerator, outputDir, resource, logger) {
this.access = [];
this.triggers = [];
this.gen1App = gen1App;
this.backendGenerator = backendGenerator;
this.outputDir = outputDir;
this.resource = resource;
this.defineAuth = new auth_renderer_1.AuthRenderer();
this.logger = logger;
}
addFunctionAuthAccess(access) {
this.access.push(access);
}
addTrigger(trigger) {
this.triggers.push(trigger);
}
async plan() {
const userPoolId = this.gen1App.resourceMetaOutput(this.resource, 'UserPoolId');
const userPool = await this.gen1App.aws.fetchUserPool(userPoolId);
const appClientIdWeb = this.gen1App.resourceMetaOutput(this.resource, 'AppClientIDWeb');
const appClientIdNative = this.gen1App.resourceMetaOutput(this.resource, 'AppClientID');
const identityPoolId = this.gen1App.resourceMetaOutput(this.resource, 'IdentityPoolId');
this.logger.debug(`Fetching auth resources for user pool '${userPoolId}'`);
const [mfaConfig, webClient, nativeClient, identityProviders, identityGroups, identityPool] = await Promise.all([
this.gen1App.aws.fetchMfaConfig(userPoolId),
this.gen1App.aws.fetchUserPoolClient(userPoolId, appClientIdWeb),
this.gen1App.aws.fetchUserPoolClient(userPoolId, appClientIdNative),
this.gen1App.aws.fetchIdentityProviders(userPoolId),
this.gen1App.aws.fetchIdentityGroups(userPoolId),
this.gen1App.aws.fetchIdentityPool(identityPoolId),
]);
const renderOptions = {
userPool,
identityPool,
identityProviders,
identityGroups,
webClient,
mfaConfig,
nativeClient,
triggers: this.triggers,
access: this.access,
};
const authDir = node_path_1.default.join(this.outputDir, 'amplify', 'auth');
return [
{
resource: this.resource,
validate: () => undefined,
describe: async () => ['Generate amplify/auth/resource.ts'],
execute: async () => {
this.logger.info('Rendering auth/resource.ts');
const nodeArray = this.defineAuth.render(renderOptions);
let content = ts_1.TS.printNodes(nodeArray);
content = content.replace(/\(allow, _unused\)/g, '(allow)');
await promises_1.default.mkdir(authDir, { recursive: true });
await promises_1.default.writeFile(node_path_1.default.join(authDir, 'resource.ts'), content, 'utf-8');
this.backendGenerator.addNamespaceImport('auth', './auth/resource');
this.backendGenerator.addDefineBackendEntry('auth', 'auth', 'auth');
this.backendGenerator.addApplyEscapeHatchesCall({ alias: 'auth', extraArgs: [] });
if (userPool.Domain) {
this.backendGenerator.addPostRefactorCall('auth.postRefactor(backend)');
}
},
},
];
}
}
exports.AuthGenerator = AuthGenerator;
//# sourceMappingURL=auth.generator.js.map