-
Notifications
You must be signed in to change notification settings - Fork 1
feat(core): add credentials sign-in provider #136
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
4 commits
Select commit
Hold shift + click to select a range
833ef1a
feat(core): add credentials sign-in provider
halvaradop 62f9e81
feat(core): add experimental signInCredentials
halvaradop e189097
feat(core): implement `redirectTo` option in `signInCredentials`
halvaradop 313b92d
chore(core): improve code
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
39 changes: 39 additions & 0 deletions
39
packages/core/src/actions/signInCredentials/signInCredentials.ts
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,39 @@ | ||
| import { z } from "zod/v4" | ||
| import { createEndpoint, createEndpointConfig } from "@aura-stack/router" | ||
| import { signInCredentials } from "@/api/credentials.ts" | ||
|
|
||
| const config = createEndpointConfig({ | ||
| schemas: { | ||
| body: z.object({ | ||
| username: z.string(), | ||
| password: z.string(), | ||
| }), | ||
| searchParams: z.object({ | ||
| redirectTo: z.string().optional(), | ||
| }), | ||
| }, | ||
| }) | ||
|
|
||
| /** | ||
| * Handles the credentials-based sign-in flow. | ||
| * It extracts credentials from the request body, calls the provider's `authorize` function, | ||
| * validates the returned user object, and creates a session. | ||
| * | ||
| * @returns The signed-in user and session cookies. | ||
| */ | ||
| export const signInCredentialsAction = createEndpoint( | ||
| "POST", | ||
| "/signIn/credentials", | ||
| async (ctx) => { | ||
| const payload = ctx.body | ||
| const { headers, success, redirectURL } = await signInCredentials({ | ||
| ctx: ctx.context, | ||
| payload, | ||
| request: ctx.request, | ||
| headers: ctx.request.headers, | ||
| redirectTo: ctx.searchParams.redirectTo, | ||
| }) | ||
| return Response.json({ success, redirectURL }, { headers, status: success ? 200 : 401 }) | ||
| }, | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { HeadersBuilder } from "@aura-stack/router" | ||
| import { secureApiHeaders } from "@/shared/headers.ts" | ||
| import { AuthValidationError } from "@/shared/errors.ts" | ||
| import { createCSRF, hashPassword, verifyPassword } from "@/shared/security.ts" | ||
| import { createRedirectTo, getBaseURL, getOriginURL } from "@/actions/signIn/authorization.ts" | ||
| import type { SignInCredentialsOptions, SignInCredentialsReturn } from "@/@types/session.ts" | ||
|
|
||
| export const signInCredentials = async ({ | ||
| ctx, | ||
| payload, | ||
| request: requestInit, | ||
| headers: headerInit, | ||
| redirectTo, | ||
| }: SignInCredentialsOptions): Promise<SignInCredentialsReturn> => { | ||
| const { cookies, credentials, sessionStrategy, logger } = ctx | ||
| try { | ||
| let request = requestInit | ||
| if (!request) { | ||
| const origin = await getBaseURL({ ctx, headers: headerInit }) | ||
| const url = `${origin}${ctx.basePath}/signIn/credentials` | ||
| request = new Request(url, { headers: headerInit }) | ||
| } | ||
| await getOriginURL(request, ctx) | ||
|
|
||
| const session = await credentials?.authorize({ | ||
| credentials: payload, | ||
| deriveSecret: credentials?.hash ?? hashPassword, | ||
| verifySecret: credentials?.verify ?? verifyPassword, | ||
| }) | ||
| if (!session) { | ||
| throw new AuthValidationError("INVALID_CREDENTIALS", "The provided credentials are invalid.") | ||
| } | ||
| const sessionToken = await sessionStrategy.createSession(session) | ||
| const csrfToken = await createCSRF(ctx.jose) | ||
| logger?.log("CREDENTIALS_SIGN_IN_SUCCESS") | ||
| const redirectURL = await createRedirectTo(request, redirectTo, ctx) | ||
|
|
||
| const headers = new HeadersBuilder(secureApiHeaders) | ||
| .setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes) | ||
| .setCookie(cookies.sessionToken.name, sessionToken, cookies.sessionToken.attributes) | ||
| .toHeaders() | ||
| return { | ||
| success: true, | ||
| headers, | ||
| redirectURL, | ||
| } | ||
| } catch (error) { | ||
| if (error instanceof AuthValidationError) { | ||
| logger?.log("INVALID_CREDENTIALS", { | ||
| severity: "warning", | ||
| structuredData: { path: "/signIn/credentials" }, | ||
| }) | ||
| return { | ||
| success: false, | ||
| headers: new Headers(secureApiHeaders), | ||
| redirectURL: null, | ||
| } | ||
| } | ||
| logger?.log("CREDENTIALS_SIGN_IN_FAILED", { | ||
| severity: "error", | ||
| structuredData: { path: "/signIn/credentials" }, | ||
| }) | ||
| return { | ||
| success: false, | ||
| headers: new Headers(secureApiHeaders), | ||
| redirectURL: null, | ||
| } | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| export { createAuthAPI } from "@/api/createApi.ts" | ||
| export { signIn } from "@/api/signIn.ts" | ||
| export { signInCredentials } from "@/api/credentials.ts" | ||
| export { signOut } from "@/api/signOut.ts" | ||
| export { getSession } from "@/api/getSession.ts" | ||
| export { updateSession } from "@/api/updateSession.ts" |
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.