-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 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 } = await signInCredentials({ | ||
| ctx: ctx.context, | ||
| payload, | ||
| }) | ||
| return Response.json({ success }, { headers, status: success ? 200 : 401 }) | ||
| }, | ||
| config | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { AuthValidationError } from "@/shared/errors.ts" | ||
| import { secureApiHeaders } from "@/shared/headers.ts" | ||
| import { createCSRF, hashPassword, verifyPassword } from "@/shared/security.ts" | ||
| import { HeadersBuilder } from "@aura-stack/router" | ||
| import type { SignInCredentialsOptions } from "@/@types/session.ts" | ||
|
|
||
| export const signInCredentials = async ({ ctx, payload }: SignInCredentialsOptions) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
At Line 7, 🤖 Prompt for AI Agents |
||
| const { cookies, credentials, sessionStrategy, logger } = ctx | ||
| try { | ||
| 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 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, | ||
| } | ||
| } catch { | ||
| logger?.log("INVALID_CREDENTIALS", { | ||
| severity: "warning", | ||
| structuredData: { | ||
| path: "/signIn/credentials", | ||
| }, | ||
| }) | ||
| return { | ||
| success: false, | ||
| headers: new Headers(secureApiHeaders), | ||
| } | ||
|
Comment on lines
+31
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not collapse all runtime failures into At Line 31, broad catch masks internal errors (e.g., session/cookie/crypto failures) as auth failures, which hurts reliability and observability. Suggested error-splitting approach- } catch {
+ } catch (error) {
+ if (error instanceof AuthValidationError) {
+ logger?.log("INVALID_CREDENTIALS", {
+ severity: "warning",
+ structuredData: { path: "/signIn/credentials" },
+ })
+ return {
+ success: false,
+ headers: new Headers(secureApiHeaders),
+ }
+ }
+ logger?.log("CREDENTIALS_SIGN_IN_FAILED", {
+ severity: "error",
+ structuredData: { path: "/signIn/credentials" },
+ })
+ throw error
- logger?.log("INVALID_CREDENTIALS", {
- severity: "warning",
- structuredData: {
- path: "/signIn/credentials",
- },
- })
- return {
- success: false,
- headers: new Headers(secureApiHeaders),
- }
}🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
| 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" |
Uh oh!
There was an error while loading. Please reload this page.