Skip to content

Commit

Permalink
fix(clerk-js): Handle the construction of zxcvbn errors with informat…
Browse files Browse the repository at this point in the history
…ion from FAPI

fix(clerk-js): Add changeset

fix(clerk-js): Remove type

fix(clerk-js): Handle the construction of zxcvbn errors with information from FAPI
  • Loading branch information
raptisj committed Jul 28, 2023
1 parent 973e1fa commit 24a46ae
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/brave-steaks-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/clerk-js': minor
'@clerk/shared': minor
'@clerk/types': minor
---

Handle the construction of zxcvbn errors with information from FAPI
65 changes: 65 additions & 0 deletions packages/clerk-js/src/ui/utils/passwordUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,69 @@ describe('createPasswordError() constructs error that password', () => {
'Your password must contain 8 or more characters, a special character, a number, a lowercase letter, and an uppercase letter.',
);
});

//
// zxcvbn
//
//
it('is not strong enough', async () => {
const { wrapper: Wrapper } = await createFixtures();

const wrapperBefore = ({ children }) => (
<Wrapper>
<OptionsProvider value={{ localization: {} }}>{children}</OptionsProvider>
</Wrapper>
);

const { result } = renderHook(() => useLocalizations(), { wrapper: wrapperBefore });

const res = createPasswordError(
[
{
code: 'form_password_not_strong_enough',
message: '',
meta: {
zxcvbn: {
suggestions: [{ code: 'anotherWord', message: '' }],
},
},
},
],
createLocalizationConfig(result.current.t),
);
expect(res).toBe('Your password is not strong enough. Add more words that are less common.');
});

it('is not strong enough and has repeated characters', async () => {
const { wrapper: Wrapper } = await createFixtures();

const wrapperBefore = ({ children }) => (
<Wrapper>
<OptionsProvider value={{ localization: {} }}>{children}</OptionsProvider>
</Wrapper>
);

const { result } = renderHook(() => useLocalizations(), { wrapper: wrapperBefore });

const res = createPasswordError(
[
{
code: 'form_password_not_strong_enough',
message: '',
meta: {
zxcvbn: {
suggestions: [
{ code: 'anotherWord', message: '' },
{ code: 'repeated', message: '' },
],
},
},
},
],
createLocalizationConfig(result.current.t),
);
expect(res).toBe(
'Your password is not strong enough. Add more words that are less common. Avoid repeated words and characters.',
);
});
});
11 changes: 11 additions & 0 deletions packages/clerk-js/src/ui/utils/passwordUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,24 @@ type LocalizationConfigProps = {
locale: string;
passwordSettings: Pick<PasswordSettingsData, 'max_length' | 'min_length'>;
};

export const createPasswordError = (errors: ClerkAPIError[], localizationConfig: LocalizationConfigProps) => {
if (!localizationConfig) {
return errors[0].longMessage;
}

const { t, locale, passwordSettings } = localizationConfig;

if (errors?.[0]?.code === 'form_password_not_strong_enough') {
const message = errors[0].meta?.zxcvbn?.suggestions
?.map(s => {
return t(localizationKeys(`unstable__errors.zxcvbn.suggestions.${s.code}` as any));
})
.join(' ');

return `${t(localizationKeys('unstable__errors.zxcvbn.notEnough'))} ${message}`;
}

const message = errors.map((s: any) => {
const localizedKey = (mapComplexityErrors(passwordSettings) as any)[s.code];

Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/errors/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function parseError(error: ClerkAPIErrorJSON): ClerkAPIError {
paramName: error?.meta?.param_name,
sessionId: error?.meta?.session_id,
emailAddresses: error?.meta?.email_addresses,
zxcvbn: error?.meta?.zxcvbn,
},
};
}
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export interface ClerkAPIError {
paramName?: string;
sessionId?: string;
emailAddresses?: string[];
zxcvbn?: {
suggestions: {
code: string;
message: string;
}[];
};
};
}

Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ export interface ClerkAPIErrorJSON {
param_name?: string;
session_id?: string;
email_addresses?: string[];
zxcvbn?: {
suggestions: {
code: string;
message: string;
}[];
};
};
}

Expand Down

0 comments on commit 24a46ae

Please sign in to comment.