Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User service #5

Merged
merged 4 commits into from
May 1, 2020
Merged
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ Running npm link in the root of your project will symlink your binary file to th

### Commands

node bin/index.js version - return the current status of package.json
node bin/index.js help - TODO
npm run cognito <command>

### Add config to ~/.cognito

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"bin": {
"cognito": "bin/index"
},
"author": "Ly Sung",
"author": "Ly Sung, Rocella Jimenez",
"license": "ISC",
"bugs": {
"url": "https://github.com/sungly/cognito-cli/issues"
Expand Down
56 changes: 53 additions & 3 deletions src/cmds/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import minimist from 'minimist';

import init from './init';
import login from './login';
import createUser from './user/create';
import UserCmd from './user';

const userCmd = new UserCmd();

const cli = () => {
const args = minimist(process.argv.slice(2));
Expand All @@ -20,11 +22,59 @@ const cli = () => {
console.log('help todo');
break;
case 'login':
console.log('@NOTE: Only user password auth is supported atm');
console.log('@NOTE: Only user password auth is supported ATM.');
login();
break;
case 'create-user':
createUser();
console.log('Creating user');

userCmd.createUser();
break;
case 'confirm-user':
console.log('Confirming user registration with confirmation code');

userCmd.confirmUserSignup();
break;
case 'resend-confirmation-code':
console.log('Resending registration confirmation code');

userCmd.resendConfirmationCode();
break;
case 'forgot-password':
console.log('Send forgot password');

userCmd.forgotPassword();
break;
case 'set-user-password':
console.log('Setting user password');
console.log('*** Requires admin permission ***');

userCmd.setUserPassword();
break;

case 'get-user-profile':
console.log('Getting user profile');
console.log('*** Requires admin permission ***');

userCmd.getUserProfile();
break;
case 'verify-user-email':
console.log('Verifying user email');
console.log('*** Requires admin permission ***');

userCmd.verifyUserEmail();
break;
case 'disable-user':
console.log('Disabling User');
console.log('*** Requires admin permission ***');

userCmd.disableUser();
break;
case 'enable-user':
console.log('Enabling User');
console.log('*** Requires admin permission ***');

userCmd.enableUser();
break;
default:
console.error(`"${cmd}" is not a valid command!`);
Expand Down
41 changes: 0 additions & 41 deletions src/cmds/user/create.js

This file was deleted.

205 changes: 205 additions & 0 deletions src/cmds/user/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import prompt from 'prompt';

import config from '../../config';
import util from '../../util';

class UserCmd {
createUser() {
config.requiredAttributeList.push('password');

const requiredAttributes = config.requiredAttributeList.map((attr) => {
return {
name: attr,
required: true,
};
});

prompt.get(requiredAttributes, async (err, result) => {
let username = result.username ? result.username : result.email;
const password = result.password;

delete result.password;

const attributes = Object.keys(result).map((key) => {
return {
Name: key,
Value: result[key],
};
});

const res = await util.cognitoService.createUser({
username,
password,
attributes,
});

console.log(res);
});
}

confirmUserSignup() {
const requiredAttributes = [
{
name: 'username',
required: true,
},
{
name: 'code',
required: true,
},
];

prompt.get(requiredAttributes, async (err, result) => {
const { username, code } = result;

const res = await util.cognitoService.confirmSignUp({
username,
code,
});

console.log(res);
});
}

resendConfirmationCode() {
const requiredAttributes = [
{
name: 'username',
required: true,
},
];

prompt.get(requiredAttributes, async (err, result) => {
const { username } = result;

const res = await util.cognitoService.resendConfirmationCode({
username,
});

console.log(res);
});
}

forgotPassword() {
const requiredAttributes = [
{
name: 'username',
required: true,
},
];

prompt.get(requiredAttributes, async (err, result) => {
const { username } = result;

const res = await util.cognitoService.forgotPassword({
username,
});

console.log(res);
});
}

setUserPassword() {
const requiredAttributes = [
{
name: 'username',
required: true,
},
{
name: 'password',
required: true,
},
];

prompt.get(requiredAttributes, async (err, result) => {
const { username, password } = result;

const res = await util.cognitoService.setUserPassword({
username,
newPassword: password,
});

console.log(res);
});
}

getUserProfile() {
const requiredAttributes = [
{
name: 'username',
required: true,
},
];

prompt.get(requiredAttributes, async (err, result) => {
const { username } = result;

const res = await util.cognitoService.getUserProfile({
username,
});

console.log(res);
});
}

verifyUserEmail() {
const requiredAttributes = [
{
name: 'email',
required: true,
},
];

prompt.get(requiredAttributes, async (err, result) => {
const { email } = result;

const res = await util.cognitoService.verifyUserEmail({
username: email,
});

console.log(res);
});
}

disableUser() {
const requiredAttributes = [
{
name: 'username',
required: true,
},
];

prompt.get(requiredAttributes, async (err, result) => {
const { username } = result;

const res = await util.cognitoService.disableUser({
username,
});

if (!res) console.log(`User is disabled.`);
else console.log(res);
});
}

enableUser() {
const requiredAttributes = [
{
name: 'username',
required: true,
},
];

prompt.get(requiredAttributes, async (err, result) => {
const { username } = result;

const res = await util.cognitoService.enableUser({
username,
});

if (!res) console.log(`User is enabled.`);
else console.log(res);
});
}
}

export default UserCmd;
Loading