Repository navigation
feat(core): add forgotPassword and resetPassword APIs
#293
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
Merged
halvaradop
merged 6 commits into
feat/add-send-verification-email-api
from
feat/add-forgot-password-api
Nov 6, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dcd9fe4
feat(core): add forgotPassword and resetPassword APIs
halvaradop 016f013
chore: apply suggested changes from code review
halvaradop 7543bf8
test(core): add forgotPassword server-side tests
halvaradop b0c521b
test(core): add forgotPassword endpoint tests
halvaradop 12a0b94
chore(core): clean up forgot password and reset password APIs
halvaradop 13ef310
chore(core): apply suggested changes from code review
halvaradop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { createEndpoint, createEndpointConfig } from "@aura-stack/router" | ||
| import { forgotPassword } from "@/api/forgot-password.ts" | ||
| import { ForgotPasswordBodySchema, ForgotPasswordResponseSchema, RedirectOptionsSchema } from "@/shared/schemas/actions.ts" | ||
|
|
||
| const forgotPasswordConfig = () => { | ||
| return createEndpointConfig({ | ||
| schemas: { | ||
| body: ForgotPasswordBodySchema, | ||
| searchParams: RedirectOptionsSchema, | ||
| response: ForgotPasswordResponseSchema, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| export const forgotPasswordAction = () => { | ||
| return createEndpoint( | ||
| "POST", | ||
| "/password/forgot", | ||
| async (ctx) => { | ||
| const { toResponse } = await forgotPassword(ctx.body.email, { | ||
| ctx: ctx.context, | ||
| request: ctx.request, | ||
| headers: ctx.request.headers, | ||
| redirect: ctx.searchParams.redirect, | ||
| redirectTo: ctx.searchParams.redirectTo, | ||
| }) | ||
| return toResponse() | ||
| }, | ||
| forgotPasswordConfig() | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { createEndpoint, createEndpointConfig } from "@aura-stack/router" | ||
| import { resetPassword } from "@/api/reset-password.ts" | ||
| import { RedirectOptionsSchema, ResetPasswordBodySchema, ResetPasswordResponseSchema } from "@/shared/schemas/actions.ts" | ||
|
|
||
| const resetPasswordConfig = () => { | ||
| return createEndpointConfig({ | ||
| schemas: { | ||
| body: ResetPasswordBodySchema, | ||
| searchParams: RedirectOptionsSchema, | ||
| response: ResetPasswordResponseSchema, | ||
| }, | ||
| hooks: { | ||
| onError: (ctx) => { | ||
| return ctx.json( | ||
| { | ||
| type: "VALIDATION", | ||
| code: "INVALID_RESET_PASSWORD_TOKEN", | ||
| message: "Invalid or expired reset password token", | ||
| }, | ||
| { status: 422 } | ||
| ) | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| export const resetPasswordAction = () => { | ||
| return createEndpoint( | ||
| "POST", | ||
| "/password/reset", | ||
| async (ctx) => { | ||
| const { toResponse } = await resetPassword({ | ||
| ctx: ctx.context, | ||
| payload: ctx.body, | ||
| request: ctx.request, | ||
| headers: ctx.request.headers, | ||
| redirect: ctx.searchParams.redirect, | ||
| redirectTo: ctx.searchParams.redirectTo, | ||
| }) | ||
| return toResponse() | ||
| }, | ||
| resetPasswordConfig() | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { HeadersBuilder } from "@aura-stack/router" | ||
| import { secureApiHeaders, toStandardizedHeaders } from "@/shared/http/headers.ts" | ||
| import { createValidation, errorToLogMessage, handleApiError, resolveApiRedirect } from "@/shared/utils/api.ts" | ||
| import type { FunctionAPIContext } from "@/@types/internal.ts" | ||
| import type { ForgotPasswordAPIOptions, ForgotPasswordAPIReturn } from "@/@types/api.ts" | ||
|
|
||
| export const forgotPassword = async ( | ||
| email: string, | ||
| { | ||
| ctx, | ||
| headers: headersInit, | ||
| request: requestInit, | ||
| redirectTo, | ||
| redirect: redirectInit = true, | ||
| }: FunctionAPIContext<ForgotPasswordAPIOptions> | ||
| ): Promise<ForgotPasswordAPIReturn> => { | ||
| const { logger, sessionStrategy, passwordReset } = ctx | ||
| try { | ||
| logger?.log("FORGOT_PASSWORD_INITIATED") | ||
| const { request, rateLimit, originURL } = await createValidation( | ||
| ctx, | ||
| toStandardizedHeaders(headersInit ?? requestInit?.headers ?? {}) | ||
| ) | ||
| .buildRequest(requestInit, "/password/forgot") | ||
| .verifyRateLimit("forgotPassword") | ||
| .execute() | ||
| if (rateLimit) { | ||
| return rateLimit as ForgotPasswordAPIReturn | ||
| } | ||
| const { token, url } = await sessionStrategy.forgotPassword({ email, origin: originURL }) | ||
| await passwordReset?.sendPasswordResetEmail?.({ email, token, url }) | ||
|
|
||
|
halvaradop marked this conversation as resolved.
Resolved
|
||
| const builder = new HeadersBuilder(secureApiHeaders) | ||
| const { redirect, redirectURL } = await resolveApiRedirect(ctx, request, redirectInit, redirectTo, builder) | ||
|
|
||
| return { | ||
| success: true, | ||
| redirect, | ||
| redirectURL: redirect ? null : redirectURL, | ||
| headers: builder.toHeaders(), | ||
| toResponse: () => { | ||
| return Response.json( | ||
| { | ||
| success: true, | ||
| redirect, | ||
| redirectURL: redirect ? null : redirectURL, | ||
| }, | ||
| { headers: builder.toHeaders(), status: redirect ? 302 : 200 } | ||
| ) | ||
| }, | ||
| } as ForgotPasswordAPIReturn | ||
| } catch (error) { | ||
| errorToLogMessage(error, "FORGOT_PASSWORD_ERROR", logger) | ||
| const { errors, statusCode } = handleApiError( | ||
| error, | ||
| "FORGOT_PASSWORD_ERROR", | ||
| "An error occurred while processing the forgot password request." | ||
| ) | ||
|
|
||
| return { | ||
| success: false, | ||
| error: errors, | ||
| redirect: false, | ||
| redirectURL: null, | ||
| headers: new Headers(secureApiHeaders), | ||
| toResponse: () => { | ||
| return Response.json( | ||
| { | ||
| success: false, | ||
| redirect: false, | ||
| redirectURL: null, | ||
| }, | ||
| { headers: secureApiHeaders, status: statusCode } | ||
| ) | ||
| }, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.