-
Notifications
You must be signed in to change notification settings - Fork 1
feat(core): add experimental signUp flow API and endpoint #183
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
812271e
feat(core): add experimental signUp flow API and endpoint
halvaradop 9615d72
feat(core): support signUp schema type inference
halvaradop 7e6ef67
test(sign-up): add coverage for signUp server API
halvaradop a09208c
test(sign-up): add coverage for /signUp endpoint
halvaradop 9b6f6c4
docs(packages): update `CHANGELOG.md` files
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { z } from "zod" | ||
| import { signUp } from "@/api/signUp.ts" | ||
| import { createEndpoint, createEndpointConfig } from "@aura-stack/router" | ||
| import { RedirectOptionsSchema } from "@/schemas.ts" | ||
| import type { SignUpConfig } from "@/@types/config.ts" | ||
|
|
||
| const signUpConfig = (config: SignUpConfig<any, any>) => { | ||
| return createEndpointConfig({ | ||
| schemas: { | ||
| body: config?.schema ?? z.object({}), | ||
| searchParams: RedirectOptionsSchema, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Handles the user sign-up process. It validates the incoming request against the provided schema, | ||
| * creates a new user using the `onCreateUser` callback. | ||
| * | ||
| * @returns The signed-up user's session | ||
| */ | ||
| export const signUpAction = (config: SignUpConfig<any, any>) => { | ||
| return createEndpoint( | ||
| "POST", | ||
| "/signUp", | ||
| async (ctx) => { | ||
| const payload = ctx.body | ||
| const { toResponse } = await signUp({ | ||
| ctx: ctx.context, | ||
| payload, | ||
| request: ctx.request, | ||
| headers: ctx.request.headers, | ||
| redirect: ctx.searchParams.redirect, | ||
| redirectTo: ctx.searchParams.redirectTo, | ||
| }) | ||
| return toResponse() | ||
| }, | ||
| signUpConfig(config) | ||
| ) | ||
| } |
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,93 @@ | ||
| import { createRedirectTo, getBaseURL, getOriginURL } from "@/actions/signIn/authorization.ts" | ||
| import type { FunctionAPIContext, SignUpAPIOptions, SignUpAPIReturn } from "@/@types/api.ts" | ||
| import { AuthValidationError, isAuthErrorWithCode } from "@/shared/errors.ts" | ||
| import { createCSRF } from "@/shared/crypto.ts" | ||
| import { HeadersBuilder } from "@aura-stack/router" | ||
| import { secureApiHeaders } from "@/shared/headers.ts" | ||
|
|
||
| export const signUp = async <Payload extends Record<string, unknown> = Record<string, unknown>>({ | ||
| ctx, | ||
| payload, | ||
| headers: headersInit, | ||
| request: requestInit, | ||
| redirect = true, | ||
| redirectTo, | ||
| }: FunctionAPIContext<SignUpAPIOptions<Payload>>): Promise<SignUpAPIReturn> => { | ||
| const { signUp, cookies, sessionStrategy, logger } = ctx | ||
| try { | ||
| let request = requestInit | ||
| if (!request) { | ||
| const origin = await getBaseURL({ ctx, headers: headersInit }) | ||
| const url = `${origin}${ctx.basePath}/signUp` | ||
| request = new Request(url, { headers: headersInit }) | ||
| } | ||
| await getOriginURL(request, ctx) | ||
| const user = await signUp?.onCreateUser({ | ||
| payload, | ||
| }) | ||
| if (!user) { | ||
| throw new AuthValidationError("USER_CREATION_FAILED", "Failed to create user with the provided payload.") | ||
| } | ||
| const sessionToken = await sessionStrategy.createSession(user) | ||
| const csrfToken = await createCSRF(ctx.jose) | ||
| logger?.log("SIGN_UP_SUCCESS") | ||
|
|
||
| const headers = new HeadersBuilder(secureApiHeaders) | ||
| .setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes) | ||
| .setCookie(cookies.sessionToken.name, sessionToken, cookies.sessionToken.attributes) | ||
|
|
||
| let redirectURL: string | null = await createRedirectTo(request, redirectTo, ctx) | ||
| redirectURL = redirectTo ? redirectURL : redirectURL === "/" ? null : redirectURL | ||
|
|
||
| if (redirect && redirectURL) { | ||
| headers.setHeader("Location", redirectURL) | ||
| } | ||
|
|
||
| const shouldRedirectServer = redirect && !!redirectURL | ||
| const toHeaders = headers.toHeaders() | ||
| return { | ||
| success: true, | ||
| redirect: shouldRedirectServer, | ||
| redirectURL: redirect ? null : redirectURL, | ||
| headers: toHeaders, | ||
| toResponse: () => { | ||
| return Response.json( | ||
| { | ||
| success: true, | ||
| redirect: shouldRedirectServer, | ||
| redirectURL: shouldRedirectServer ? null : redirectURL, | ||
| }, | ||
| { headers: toHeaders, status: shouldRedirectServer ? 302 : 200 } | ||
| ) | ||
| }, | ||
| } as SignUpAPIReturn | ||
| } catch (error) { | ||
| let code = "SIGN_UP_ERROR" | ||
| let message = "An error occurred during sign-up." | ||
| if (isAuthErrorWithCode(error)) { | ||
| code = error.code | ||
| message = error.message | ||
| } | ||
|
|
||
| return { | ||
| success: false, | ||
| error: { | ||
| code, | ||
| message, | ||
| }, | ||
| redirect: false, | ||
| headers: new Headers(secureApiHeaders), | ||
| redirectURL: null, | ||
| toResponse: () => { | ||
| return Response.json( | ||
| { | ||
| success: false, | ||
| redirect: false, | ||
| redirectURL: null, | ||
| }, | ||
| { headers: secureApiHeaders, status: 400 } | ||
| ) | ||
| }, | ||
| } | ||
| } | ||
| } | ||
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.