@shift-code/cli
Version:
Redeem Gearbox SHiFT codes automatically
50 lines • 2.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loginCommand = loginCommand;
const api_1 = require("@shift-code/api");
const chalk_1 = __importDefault(require("chalk"));
const shared_1 = require("../shared");
const cache_1 = require("../cache");
async function loginCommand(args) {
const tryPrompt = (0, shared_1.tryPromptArgs)(args);
const cache = await (0, cache_1.loadMetaCache)();
const email = await tryPrompt({
name: 'email',
type: 'text',
message: 'SHiFT email',
validate: (value) => !!value
});
// If user is already saved, switch to it without requiring another login
for (const accountID of cache.accounts ?? []) {
const existingAccount = await (0, cache_1.loadAccountCache)(accountID);
if (existingAccount.account?.email === email && existingAccount.session) {
const user = await (0, api_1.account)(existingAccount.session)
.catch(() => null);
if (user) {
await (0, cache_1.saveMetaActiveAccount)(user.id);
await (0, cache_1.saveAccount)(user.id, user);
console.log('Changed active account');
console.info(` Name: ${user.name}`);
console.info(` Email: ${user.email}`);
return;
}
}
}
const password = await tryPrompt({
name: 'password',
type: 'password',
message: 'SHiFT password',
validate: (value) => !!value
});
const session = await (0, api_1.login)({ email, password });
const user = await (0, api_1.account)(session);
await (0, cache_1.saveMetaActiveAccount)(user.id);
await (0, cache_1.saveMetaAccount)(user.id);
await (0, cache_1.saveAccountSession)(user.id, session);
await (0, cache_1.saveAccount)(user.id, user);
console.log(chalk_1.default.green('Login successful'));
}
//# sourceMappingURL=login.js.map