Skip to content

Commit

Permalink
feat(ui): Implement use reset password factor logic (#3750)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcarpenter authored Jul 18, 2024
1 parent 1066eaa commit aa0efec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .changeset/purple-seahorses-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
26 changes: 15 additions & 11 deletions packages/ui/src/components/sign-in/sign-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useDisplayConfig } from '~/hooks/use-display-config';
import { useEnabledConnections } from '~/hooks/use-enabled-connections';
import { useEnvironment } from '~/hooks/use-environment';
import { useLocalizations } from '~/hooks/use-localizations';
import { useResetPasswordFactor } from '~/hooks/use-reset-password-factor';
import { Alert } from '~/primitives/alert';
import { Button } from '~/primitives/button';
import * as Card from '~/primitives/card';
Expand Down Expand Up @@ -48,6 +49,7 @@ export function SignInComponentLoaded() {
const hasConnection = enabledConnections.length > 0;
const hasIdentifier = emailAddressEnabled || usernameEnabled || phoneNumberEnabled;
const isDev = isDevelopmentOrStaging();
const isPasswordResetSupported = useResetPasswordFactor();

return (
<Common.Loading>
Expand Down Expand Up @@ -223,18 +225,20 @@ export function SignInComponentLoaded() {
<Card.Body>
<PasswordField
alternativeFieldTrigger={
<SignIn.Action
navigate='forgot-password'
asChild
>
<LinkButton
size='sm'
disabled={isGlobalLoading}
type='button'
isPasswordResetSupported ? (
<SignIn.Action
navigate='forgot-password'
asChild
>
{t('formFieldAction__forgotPassword')}
</LinkButton>
</SignIn.Action>
<LinkButton
size='sm'
disabled={isGlobalLoading}
type='button'
>
{t('formFieldAction__forgotPassword')}
</LinkButton>
</SignIn.Action>
) : null
}
/>

Expand Down
14 changes: 14 additions & 0 deletions packages/ui/src/hooks/use-reset-password-factor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useClerk } from '@clerk/clerk-react';
import type { ResetPasswordCodeFactor, SignInStrategy } from '@clerk/types';

const resetPasswordStrategies: SignInStrategy[] = ['reset_password_phone_code', 'reset_password_email_code'];
export const isResetPasswordStrategy = (strategy: SignInStrategy | string | null | undefined) =>
!!strategy && resetPasswordStrategies.includes(strategy as SignInStrategy);

export function useResetPasswordFactor() {
const clerk = useClerk();

return clerk.client.signIn.supportedFirstFactors.find(({ strategy }) => isResetPasswordStrategy(strategy)) as
| ResetPasswordCodeFactor
| undefined;
}

0 comments on commit aa0efec

Please sign in to comment.