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 prefill support to Auth0LockPasswordless #1505

Merged
merged 6 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
52 changes: 52 additions & 0 deletions src/__tests__/engine/passwordless.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { mockComponent } from 'testUtils';
const getEngine = () => require('engine/passwordless').default;

jest.mock('core/error_screen', () => mockComponent('error_screen'));
jest.mock('core/loading_screen', () => mockComponent('loading_screen'));
jest.mock('engine/passwordless/social_or_email_login_screen', () =>
mockComponent('social_or_email_login_screen')
);
jest.mock('engine/passwordless/social_or_phone_number_login_screen', () =>
mockComponent('social_or_phone_number_login_screen')
);
jest.mock('connection/passwordless/ask_vcode', () => mockComponent('ask_vcode'));
jest.mock('core/sso/last_login_screen', () => mockComponent('last_login_screen'));

describe('Passwordless Engine', () => {
describe('didReceiveClientSettings calls setPrefill', () => {
beforeEach(() => {
jest.resetModules();
jest.mock('core/index', () => ({
hasSomeConnections: () => true,
prefill: () => ({
toJS() {
return {
email: 'prefill@example.com',
phoneNumber: '12354'
};
}
})
}));
jest.mock('field/email', () => ({
setEmail: jest.fn(m => m)
}));
jest.mock('field/phone_number', () => ({
setPhoneNumber: jest.fn(m => m)
}));
});
it('when prefill options has `email` value', () => {
const engine = getEngine();
engine.didReceiveClientSettings('model');
expect(require('field/email').setEmail.mock.calls.length).toBe(1);
luisrudge marked this conversation as resolved.
Show resolved Hide resolved
expect(require('field/email').setEmail.mock.calls[0][0]).toBe('model');
expect(require('field/email').setEmail.mock.calls[0][1]).toBe('prefill@example.com');
});
it('when prefill options has `phoneNumber` value', () => {
const engine = getEngine();
engine.didReceiveClientSettings('model');
expect(require('field/phone_number').setPhoneNumber.mock.calls.length).toBe(1);
expect(require('field/phone_number').setPhoneNumber.mock.calls[0][0]).toBe('model');
expect(require('field/phone_number').setPhoneNumber.mock.calls[0][1]).toBe('12354');
});
});
});
23 changes: 14 additions & 9 deletions src/engine/passwordless.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ import { isDone, isSuccess } from '../sync';
import * as l from '../core/index';
import { hasSkippedQuickAuth } from '../quick_auth';
import * as sso from '../core/sso/index';
import { setEmail } from '../field/email';
import { setPhoneNumber } from '../field/phone_number';

const setPrefill = m => {
const { email, phoneNumber } = l.prefill(m).toJS();
if (typeof email === 'string') {
m = setEmail(m, email);
}
if (typeof phoneNumber === 'string') {
m = setPhoneNumber(m, phoneNumber);
luisrudge marked this conversation as resolved.
Show resolved Hide resolved
}
return m;
};

class Passwordless {
didInitialize(m, opts) {
Expand All @@ -36,6 +49,7 @@ class Passwordless {
error.code = 'no_connection';
m = l.stop(m, error);
}
m = setPrefill(m);

return m;
}
Expand Down Expand Up @@ -80,15 +94,6 @@ class Passwordless {
} else {
return passwordlessStarted(m) ? new VcodeScreen() : new SocialOrPhoneNumberLoginScreen();
}

setTimeout(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was unreachable code

const stopError = new Error('Internal error');
stopError.code = 'internal_error';
stopError.description = "Couldn't find a screen to render";
swap(updateEntity, 'lock', l.id(m), l.stop, stopError);
}, 0);

return new ErrorScreen();
}
}

Expand Down
3 changes: 2 additions & 1 deletion support/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ <h1 class="navbar-brand">
usernameStyle: 'email',
defaultDatabaseConnection: 'acme',
prefill: {
email: 'johnfoo@gmail.com'
email: 'johnfoo@gmail.com',
phoneNumber: '48999999999'
},
passwordlessMethod: 'code',
allowedConnections: ['email', 'acme', 'twitter']
Expand Down