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

Signup custom attr #160

Merged
merged 9 commits into from
Jan 18, 2018
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
9 changes: 8 additions & 1 deletion media/authentication_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ Auth.confirmSignIn(user, code)
```js
import { Auth } from 'aws-amplify';

Auth.signUp(username, password, email, phone)
Auth.signUp({
username,
password,
email, // optional
phone, // optional
// other custom attributes if has been set in Cognito
// myAttr: ...
})
.then(data => console.log(data))
.catch(err => console.log(err));

Expand Down
42 changes: 29 additions & 13 deletions packages/aws-amplify-react-native/dist/Auth/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,46 @@ class AuthClass {

/**
* Sign up with username, password and other attrbutes like phone, email
* @param {String} username - The username to be signed up
* @param {String} password - The password of the user
* @param {String} email - The email of the user
* @param {String} phone_number - the phone number of the user
* @return {Promise} - A promise resolves callback data if success
* @param {String | object} attrs - The user attirbutes used for signin
* @param {String[]} restOfAttrs - for the backward compatability
* @return - A promise resolves callback data if success
*/
signUp(username, password, email, phone_number) {
signUp(attrs, ...restOfAttrs) {
if (!this.userPool) {
return Promise.reject('No userPool');
}

let username = null;
let password = null;
const attributes = [];
if (attrs && typeof attrs === 'string') {
username = attrs;
password = restOfAttrs ? restOfAttrs[0] : null;
const email = restOfAttrs ? restOfAttrs[1] : null;
const phone_number = restOfAttrs ? restOfAttrs[2] : null;
if (email) attributes.push({ Name: 'email', Value: email });
if (phone_number) attributes.push({ Name: 'phone_number', Value: phone_number });
} else if (attrs && typeof attrs === 'object') {
username = attrs['username'];
password = attrs['password'];
Object.keys(attrs).map(key => {
if (key === 'username' || key === 'password') return;
const ele = { Name: key, Value: attrs[key] };
attributes.push(ele);
});
} else {
return Promise.reject('The first parameter should either be non-null string or object');
}

if (!username) {
return Promise.reject('Username cannot be empty');
}
if (!password) {
return Promise.reject('Password cannot be empty');
}

const attributes = [];
if (email) {
attributes.push({ Name: 'email', Value: email });
}
if (phone_number) {
attributes.push({ Name: 'phone_number', Value: phone_number });
}
logger.debug('signUp attrs:');
logger.debug(attributes);

return new Promise((resolve, reject) => {
this.userPool.signUp(username, password, attributes, null, function (err, data) {
Expand Down
46 changes: 35 additions & 11 deletions packages/aws-amplify-react-native/src/Auth/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,48 @@ class AuthClass {

/**
* Sign up with username, password and other attrbutes like phone, email
* @param {String} username - The username to be signed up
* @param {String} password - The password of the user
* @param {String} email - The email of the user
* @param {String} phone_number - the phone number of the user
* @return {Promise} - A promise resolves callback data if success
* @param {String | object} attrs - The user attirbutes used for signin
* @param {String[]} restOfAttrs - for the backward compatability
* @return - A promise resolves callback data if success
*/
signUp(username, password, email, phone_number) {
signUp(attrs, ...restOfAttrs) {
if (!this.userPool) { return Promise.reject('No userPool'); }
if (!username) { return Promise.reject('Username cannot be empty'); }
if (!password) { return Promise.reject('Password cannot be empty'); }

let username = null;
let password = null;
const attributes = [];
if (email) { attributes.push({Name: 'email', Value: email}); }
if (phone_number) { attributes.push({Name: 'phone_number', Value: phone_number}); }
if (attrs && typeof attrs === 'string') {
username = attrs;
password = restOfAttrs? restOfAttrs[0] : null;
const email = restOfAttrs? restOfAttrs[1] : null;
const phone_number = restOfAttrs? restOfAttrs[2] : null;
if (email) attributes.push({Name: 'email', Value: email});
if (phone_number) attributes.push({Name: 'phone_number', Value: phone_number});
} else if (attrs && typeof attrs === 'object') {
username = attrs['username'];
password = attrs['password'];
Object.keys(attrs).map(key => {
if (key === 'username' || key === 'password') return;
const ele = { Name: key, Value: attrs[key] };
attributes.push(ele);
});
} else {
return Promise.reject('The first parameter should either be non-null string or object');
}

if (!username) { return Promise.reject('Username cannot be empty'); }
if (!password) { return Promise.reject('Password cannot be empty'); }

logger.debug('signUp attrs:');
logger.debug(attributes);

return new Promise((resolve, reject) => {
this.userPool.signUp(username, password, attributes, null, function(err, data) {
if (err) { reject(err); } else { resolve(data); }
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
Expand Down
52 changes: 49 additions & 3 deletions packages/aws-amplify/__tests__/Auth/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const session = new CognitoUserSession({

describe('auth unit test', () => {
describe('signUp', () => {
test('happy case', async () => {
test('happy case with string attrs', async () => {
const spyon = jest.spyOn(CognitoUserPool.prototype, "signUp");
const auth = new Auth(authOptions);

Expand All @@ -170,6 +170,41 @@ describe('auth unit test', () => {
spyon.mockClear();
});

test('happy case with object attr', async () => {
const spyon = jest.spyOn(CognitoUserPool.prototype, "signUp");
const auth = new Auth(authOptions);

const attrs = {
username: 'username',
password: 'password',
email: 'email',
phone_number: 'phone_number',
otherAttrs: 'otherAttrs'
}
expect.assertions(1);
expect(await auth.signUp(attrs)).toBe('signUpResult');

spyon.mockClear();
});

test('object attr with null username', async () => {
const auth = new Auth(authOptions);

const attrs = {
username: null,
password: 'password',
email: 'email',
phone_number: 'phone_number',
otherAttrs: 'otherAttrs'
}
expect.assertions(1);
try {
await auth.signUp(attrs);
} catch (e) {
expect(e).not.toBeNull();
}
});

test('callback error', async () => {
const spyon = jest.spyOn(CognitoUserPool.prototype, "signUp")
.mockImplementationOnce((username, password, signUpAttributeList, validationData, callback) => {
Expand Down Expand Up @@ -200,7 +235,7 @@ describe('auth unit test', () => {
});

test('no username', async () => {
const auth = new Auth(authOptionsWithNoUserPoolId);
const auth = new Auth(authOptions);

expect.assertions(1);
try {
Expand All @@ -211,7 +246,7 @@ describe('auth unit test', () => {
});

test('no password', async () => {
const auth = new Auth(authOptionsWithNoUserPoolId);
const auth = new Auth(authOptions);

expect.assertions(1);
try {
Expand All @@ -220,6 +255,17 @@ describe('auth unit test', () => {
expect(e).not.toBeNull();
}
});

test('only username', async () => {
const auth = new Auth(authOptions);

expect.assertions(1);
try {
await auth.signUp('username');
} catch (e) {
expect(e).not.toBeNull();
}
});
});

describe('confirmSignUp', () => {
Expand Down
Loading