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

Add support for signup with root level attributes #1656

Merged
merged 6 commits into from
Jun 25, 2019
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ var options = {
}]
};
```

If you don't provide a `validator` function a default validator is applied, which requires the text field to contain some value (be non-empty). You can make a field optional by using a validator that always return `true`:

```js
Expand All @@ -436,6 +437,17 @@ var options = {
};
```

If you want to save the value of the attribute in the root of your profile, use `storage: 'root'`. Only a subset of values can be stored this way. The list of attributes that can be added to your root profile is [here](https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id). By default, every additional sign up field is stored inside the `user_metadata` object.

```js
var options = {
additionalSignUpFields: [{
name: "name",
storage: "root"
}]
};
```

##### Select field

To specify a select field `type: "select"` needs to be provided along with the `options` property.
Expand Down
84 changes: 84 additions & 0 deletions src/__tests__/connection/database/actions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Immutable, { List, Map } from 'immutable';
import { signUp } from '../../../connection/database/actions';
import { swap, setEntity } from '../../../store';

const webApiMock = () => require('core/web_api');
const coreActionsMock = () => require('core/actions');
jest.mock('core/actions', () => ({
validateAndSubmit: jest.fn()
}));

jest.mock('core/web_api', () => ({
signUp: jest.fn()
}));

describe('database/actions.js', () => {
it('signUp splits root attributes correctly', () => {
const id = 1;
require('connection/database/index').databaseConnectionName = () => 'test-connection';
require('connection/database/index').shouldAutoLogin = () => true;
const m = Immutable.fromJS({
field: {
email: {
value: 'test@email.com'
},
password: {
value: 'testpass'
},
family_name: {
value: 'test-family-name'
},
given_name: {
value: 'test-given-name'
},
name: {
value: 'test-name'
},
nickname: {
value: 'test-nickname'
},
picture: {
value: 'test-pic'
},
other_prop: {
value: 'test-other'
}
},
database: {
additionalSignUpFields: [
{ name: 'family_name', storage: 'root' },
{ name: 'given_name', storage: 'root' },
{ name: 'name', storage: 'root' },
{ name: 'nickname', storage: 'root' },
{ name: 'picture', storage: 'root' },
{ name: 'other_prop' }
]
}
});
swap(setEntity, 'lock', id, m);
signUp(id);
const { validateAndSubmit: { mock: validateAndSubmitMock } } = coreActionsMock();
expect(validateAndSubmitMock.calls.length).toBe(1);
expect(validateAndSubmitMock.calls[0][0]).toBe(id);
expect(validateAndSubmitMock.calls[0][1]).toContain('email');
expect(validateAndSubmitMock.calls[0][1]).toContain('password');
validateAndSubmitMock.calls[0][2](m);
const { signUp: { mock: signUpMock } } = webApiMock();
expect(signUpMock.calls.length).toBe(1);
expect(signUpMock.calls[0][0]).toBe(id);
expect(signUpMock.calls[0][1]).toMatchObject({
connection: 'test-connection',
email: 'test@email.com',
password: 'testpass',
autoLogin: true,
family_name: 'test-family-name',
given_name: 'test-given-name',
name: 'test-name',
nickname: 'test-nickname',
picture: 'test-pic',
user_metadata: {
other_prop: 'test-other'
}
});
});
});
22 changes: 22 additions & 0 deletions src/__tests__/connection/database/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ describe('database/index.js', () => {
});
describe('initDatabase', () => {
describe('calls initNS with the correct additionalSignUpFields', () => {
describe('uses the `storage` attribute', () => {
const model = Immutable.fromJS({});
const modelOut = initDatabase(model, {
additionalSignUpFields: [
{
type: 'hidden',
name: 'hidden_field',
value: 'hidden_value',
storage: 'root'
}
]
});
const modelOutJS = modelOut.toJS();
expect(modelOutJS.database.additionalSignUpFields).toEqual([
{
type: 'hidden',
name: 'hidden_field',
value: 'hidden_value',
storage: 'root'
}
]);
});
describe('with a valid hidden field', () => {
const model = Immutable.fromJS({});
const modelOut = initDatabase(model, {
Expand Down
15 changes: 14 additions & 1 deletion src/connection/database/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ export function signUp(id) {
if (!additionalSignUpFields(m).isEmpty()) {
params.user_metadata = {};
additionalSignUpFields(m).forEach(x => {
params.user_metadata[x.get('name')] = c.getFieldValue(m, x.get('name'));
const storage = x.get('storage');
const fieldName = x.get('name');
const fieldValue = c.getFieldValue(m, x.get('name'));
switch (storage) {
case 'root':
params[fieldName] = fieldValue;
break;
default:
if (!params.user_metadata) {
params.user_metadata = {};
}
params.user_metadata[fieldName] = fieldValue;
break;
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/connection/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function processDatabaseOptions(opts) {
additionalSignUpFields = undefined;
} else if (additionalSignUpFields) {
additionalSignUpFields = additionalSignUpFields.reduce((r, x) => {
let { icon, name, options, placeholder, prefill, type, validator, value } = x;
let { icon, name, options, placeholder, prefill, type, validator, value, storage } = x;
let filter = true;

const reservedNames = ['email', 'username', 'password'];
Expand Down Expand Up @@ -195,7 +195,7 @@ function processDatabaseOptions(opts) {
}

return filter
? r.concat([{ icon, name, options, placeholder, prefill, type, validator, value }])
? r.concat([{ icon, name, options, placeholder, prefill, type, validator, value, storage }])
: r;
}, []);

Expand Down