Skip to content

Commit

Permalink
fix(clerk-js): Add resetPassword method as a core resource
Browse files Browse the repository at this point in the history
fix(clerk-js): Add resetPassword method as a core resource
  • Loading branch information
raptisj committed Apr 13, 2023
1 parent 9524b38 commit fa70749
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/clerk-js/src/core/resources/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type {
PhoneCodeConfig,
PrepareFirstFactorParams,
PrepareSecondFactorParams,
ResetPasswordCodeFactorConfig,
ResetPasswordParams,
SignInCreateParams,
SignInFirstFactor,
SignInIdentifier,
Expand Down Expand Up @@ -61,6 +63,13 @@ export class SignIn extends BaseResource implements SignInResource {
});
};

resetPassword = (params: ResetPasswordParams): Promise<SignInResource> => {
return this._basePost({
body: params,
path: `${this.pathRoot}/${this.id}/reset_password`,
});
};

prepareFirstFactor = (factor: PrepareFirstFactorParams): Promise<SignInResource> => {
let config;
switch (factor.strategy) {
Expand All @@ -82,6 +91,16 @@ export class SignIn extends BaseResource implements SignInResource {
case 'web3_metamask_signature':
config = { web3WalletId: factor.web3WalletId } as Web3SignatureConfig;
break;
case 'reset_password_code':
if (factor.emailAddressId) {
config = { emailAddressId: factor.emailAddressId } as ResetPasswordCodeFactorConfig;
}
if (factor.phoneNumberId) {
config = {
phoneNumberId: factor.phoneNumberId,
} as ResetPasswordCodeFactorConfig;
}
break;
default:
clerkInvalidStrategy('SignIn.prepareFirstFactor', factor.strategy);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/factors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
OAuthStrategy,
PasswordStrategy,
PhoneCodeStrategy,
ResetPasswordCode,
TOTPStrategy,
Web3Strategy,
} from './strategies';
Expand Down Expand Up @@ -53,6 +54,12 @@ export type BackupCodeFactor = {
strategy: BackupCodeStrategy;
};

export type ResetPasswordCodeFactorConfig = {
strategy: ResetPasswordCode;
emailAddressId?: string;
phoneNumberId?: string;
};

export type EmailCodeConfig = Omit<EmailCodeFactor, 'safeIdentifier'>;
export type EmailLinkConfig = Omit<EmailLinkFactor, 'safeIdentifier'> & {
redirectUrl: string;
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/signIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
PhoneCodeConfig,
PhoneCodeFactor,
PhoneCodeSecondFactorConfig,
ResetPasswordCodeFactorConfig,
TOTPAttempt,
TOTPFactor,
Web3Attempt,
Expand Down Expand Up @@ -62,6 +63,8 @@ export interface SignInResource extends ClerkResource {

create: (params: SignInCreateParams) => Promise<SignInResource>;

resetPassword: (params: ResetPasswordParams) => Promise<SignInResource>;

prepareFirstFactor: (params: PrepareFirstFactorParams) => Promise<SignInResource>;

attemptFirstFactor: (params: AttemptFirstFactorParams) => Promise<SignInResource>;
Expand Down Expand Up @@ -110,6 +113,7 @@ export type PrepareFirstFactorParams =
| EmailLinkConfig
| PhoneCodeConfig
| Web3SignatureConfig
| ResetPasswordCodeFactorConfig
| OAuthConfig;

export type AttemptFirstFactorParams = EmailCodeAttempt | PhoneCodeAttempt | PasswordAttempt | Web3Attempt;
Expand Down Expand Up @@ -148,6 +152,10 @@ export type SignInCreateParams = (
| { transfer?: boolean }
) & { transfer?: boolean };

export type ResetPasswordParams = {
new_password: string;
};

export interface SignInStartMagicLinkFlowParams extends StartMagicLinkFlowParams {
emailAddressId: string;
}
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type EmailLinkStrategy = 'email_link';
export type TicketStrategy = 'ticket';
export type TOTPStrategy = 'totp';
export type BackupCodeStrategy = 'backup_code';
export type ResetPasswordCode = 'reset_password_code';

export type OAuthStrategy = `oauth_${OAuthProvider}`;
export type Web3Strategy = `web3_${Web3Provider}_signature`;

0 comments on commit fa70749

Please sign in to comment.