UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

67 lines 2.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const schema_1 = require("@sprucelabs/schema"); const spruce_event_utils_1 = require("@sprucelabs/spruce-event-utils"); const AbstractAction_1 = __importDefault(require("../../AbstractAction")); class LoginAction extends AbstractAction_1.default { optionsSchema = optionsSchema; commandAliases = ['login']; invocationMessage = 'Logging in... 🤝'; async execute(options) { const { phone, pin: suppliedPin } = this.validateAndNormalizeOptions(options); let loggedIn = false; const client = await this.connectToApi({ shouldAuthAsLoggedInPerson: false, }); const requestPinResults = await client.emit('request-pin::v2020_12_25', { payload: { phone }, }); const { challenge } = spruce_event_utils_1.eventResponseUtil.getFirstResponseOrThrow(requestPinResults); const response = {}; do { const pin = suppliedPin ?? (await this.ui.prompt({ type: 'text', label: 'Pin', isRequired: true, })); const confirmPinResults = await client.emit('confirm-pin::v2020_12_25', { payload: { challenge, pin }, }); try { const { person, token } = spruce_event_utils_1.eventResponseUtil.getFirstResponseOrThrow(confirmPinResults); const loggedInPerson = { ...person, token }; this.Service('auth').setLoggedInPerson(loggedInPerson); loggedIn = true; response.meta = { loggedInPerson }; response.summaryLines = [ `Logged in as ${loggedInPerson.casualName}`, ]; } catch (err) { this.ui.renderWarning('Oops, bad pin. Try again please! 🙏'); } } while (!loggedIn); return response; } } exports.default = LoginAction; const optionsSchema = (0, schema_1.buildSchema)({ id: 'loginAction', description: 'Authenticate as a person. From here, you can begin creating skill, organizations, conversation topics, etc.', fields: { phone: { type: 'phone', label: 'Phone number', isRequired: true, }, pin: { type: 'text', label: 'Pin', }, }, }); //# sourceMappingURL=LoginAction.js.map