Skip to content

Commit

Permalink
merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
powerful23 committed Jan 18, 2018
2 parents 101bfaa + be8db91 commit 722c688
Show file tree
Hide file tree
Showing 23 changed files with 961 additions and 78 deletions.
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
6 changes: 3 additions & 3 deletions packages/aws-amplify-react/__tests__/Storage/S3Album-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe('S3Album test', () => {

expect.assertions(2);
expect(spyon).toBeCalledWith({"file": "file", "name": "name", "size": "size", "type": "type"});
expect(spyon2).toBeCalledWith('path', 'file', {contentType: 'type'});
expect(spyon2).toBeCalledWith('path', 'file', {"contentType": "type", "level": "public", "track": undefined});

spyon.mockClear();
spyon2.mockClear();
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('S3Album test', () => {

expect.assertions(3);
expect(spyon).toBeCalledWith({"file": "file", "name": "name", "size": "size", "type": "type"});
expect(spyon2).toBeCalledWith('path', 'file', {contentType: 'type'});
expect(spyon2).toBeCalledWith('path', 'file', {"contentType": "type", "level": "public", "track": undefined});
expect(spyon3).toBeCalledWith([{key: 'path2'}, 'data']);

spyon.mockClear();
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('S3Album test', () => {

expect.assertions(2);
expect(spyon).toBeCalledWith({"file": "file", "name": "name", "size": "size", "type": "type"});
expect(spyon2).toBeCalledWith('path', 'file', {contentType: 'type'});
expect(spyon2).toBeCalledWith('path', 'file', {"contentType": "type", "level": "public", "track": undefined});

spyon.mockClear();
spyon2.mockClear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('S3Image', () => {
await s3Image.handlePick(data);

expect.assertions(2);
expect(spyon).toBeCalledWith('imgKey', 'file', {contentType: 'type'});
expect(spyon).toBeCalledWith('imgKey', 'file', {"contentType": "type", "level": "level", "track": undefined});
expect(spyon2).toBeCalled();

spyon.mockClear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('S3Text test', () => {
await s3Text.handlePick(data);

expect.assertions(2);
expect(spyon).toBeCalledWith('textKey', 'file', { contentType: 'type' });
expect(spyon).toBeCalledWith('textKey', 'file', {"contentType": "type", "level": "level", "track": undefined});
expect(spyon2).toBeCalled();

spyon.mockClear();
Expand Down
9 changes: 7 additions & 2 deletions packages/aws-amplify-react/dist/Storage/S3Album.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ var S3Album = function (_Component) {
onPick = _props.onPick,
onLoad = _props.onLoad,
onError = _props.onError,
track = _props.track;
track = _props.track,
level = _props.level;


if (onPick) {
Expand All @@ -129,7 +130,11 @@ var S3Album = function (_Component) {
type = data.type;

var key = path + this.getKey(data);
_awsAmplify.Storage.put(key, file, { contentType: type, track: track }).then(function (data) {
_awsAmplify.Storage.put(key, file, {
level: level ? level : 'public',
contentType: type,
track: track
}).then(function (data) {
logger.debug('handle pick data', data);
var items = _this2.state.items;

Expand Down
6 changes: 5 additions & 1 deletion packages/aws-amplify-react/dist/Storage/S3Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ var S3Image = function (_Component) {
type = data.type;

var key = imgKey || path + (0, _Common.calcKey)(data, fileToKey);
_awsAmplify.Storage.put(key, file, { contentType: type, track: track }).then(function (data) {
_awsAmplify.Storage.put(key, file, {
level: level ? level : 'public',
contentType: type,
track: track
}).then(function (data) {
logger.debug('handle pick data', data);
that.getImageSource(key, level, track);
})['catch'](function (err) {
Expand Down
6 changes: 5 additions & 1 deletion packages/aws-amplify-react/dist/Storage/S3Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ var S3Text = function (_Component) {
type = data.type;

var key = textKey || path + (0, _Common.calcKey)(data, fileToKey);
_awsAmplify.Storage.put(key, file, { contentType: type, track: track }).then(function (data) {
_awsAmplify.Storage.put(key, file, {
level: level ? level : 'public',
contentType: type,
track: track
}).then(function (data) {
logger.debug('handle pick data', data);
that.getText(key, level, track);
})['catch'](function (err) {
Expand Down
8 changes: 6 additions & 2 deletions packages/aws-amplify-react/src/Storage/S3Album.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ export default class S3Album extends Component {

handlePick(data) {
const that = this;
const { onPick, onLoad, onError, track } = this.props;
const { onPick, onLoad, onError, track, level } = this.props;

if (onPick) { onPick(data); }

const path = this.props.path || '';
const { file, name, size, type } = data;
const key = path + this.getKey(data);
Storage.put(key, file, { contentType: type, track })
Storage.put(key, file, {
level: level? level: 'public',
contentType: type,
track
})
.then(data => {
logger.debug('handle pick data', data);
const { items } = this.state;
Expand Down
6 changes: 5 additions & 1 deletion packages/aws-amplify-react/src/Storage/S3Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export default class S3Image extends Component {
const { imgKey, level, fileToKey, track } = this.props;
const { file, name, size, type } = data;
const key = imgKey || (path + calcKey(data, fileToKey));
Storage.put(key, file, { contentType: type, track })
Storage.put(key, file, {
level: level? level: 'public',
contentType: type,
track
})
.then(data => {
logger.debug('handle pick data', data);
that.getImageSource(key, level, track);
Expand Down
6 changes: 5 additions & 1 deletion packages/aws-amplify-react/src/Storage/S3Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ export default class S3Text extends Component {
const { textKey, level, fileToKey, track } = this.props;
const { file, name, size, type } = data;
const key = textKey || (path + calcKey(data, fileToKey));
Storage.put(key, file, { contentType: type, track })
Storage.put(key, file, {
level: level? level: 'public',
contentType: type,
track
})
.then(data => {
logger.debug('handle pick data', data);
that.getText(key, level, track);
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

0 comments on commit 722c688

Please sign in to comment.