Skip to content

Commit 712e621

Browse files
committed
Add error handling for optional field when left empty
1 parent 62ca0aa commit 712e621

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

passkeys-backend/functions/registration/start.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ exports.handler = async (context, event, callback) => {
2222

2323
const { username, password } = context.getTwilioClient();
2424

25+
const androidOrigins = (keys) => {
26+
if (!keys || keys.trim() === '""') return [];
27+
return keys.split(',');
28+
};
29+
2530
// Request body sent to passkeys verify URL call
2631
/* eslint-disable camelcase */
2732
const requestBody = {
@@ -35,7 +40,7 @@ exports.handler = async (context, event, callback) => {
3540
name: 'PasskeySample',
3641
origins: [
3742
`https://${DOMAIN_NAME}`,
38-
...(ANDROID_APP_KEYS?.split(',') ?? []),
43+
...androidOrigins(ANDROID_APP_KEYS),
3944
],
4045
},
4146
user: {

passkeys-backend/tests/registration-start.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ describe('registration/start', () => {
107107
);
108108
});
109109

110+
// This is how the CodeExchange is populating the optional field if left empty
111+
it('works with ANDROID_APP_KEYS empty string', (done) => {
112+
const callback = (_, { _body }) => {
113+
expect(axios.post).toHaveBeenCalledWith(
114+
'https://api.com/Factors',
115+
mockRequestBody,
116+
{ auth: { password: 'mockPassword', username: 'mockUsername' } }
117+
);
118+
done();
119+
};
120+
121+
const mockContextWithoutAndroidKeys = {
122+
API_URL: 'https://api.com',
123+
ANDROID_APP_KEYS: '""',
124+
DOMAIN_NAME: 'example.com',
125+
getTwilioClient: () => ({
126+
username: 'mockUsername',
127+
password: 'mockPassword',
128+
}),
129+
};
130+
131+
handlerFunction(
132+
mockContextWithoutAndroidKeys,
133+
{ username: 'user001' },
134+
callback
135+
);
136+
});
137+
110138
it('calls the API with the expected request body', (done) => {
111139
const modifiedRequest = structuredClone(mockRequestBody);
112140
modifiedRequest.content.relying_party.origins.push('key1', 'key2', 'key3');

0 commit comments

Comments
 (0)