Skip to content
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

fix(qwik-auth): define basePath option #6435

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/qwik-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { AuthConfig } from '@auth/core';
import { Auth, skipCSRFCheck } from '@auth/core';
import type { AuthAction, Session } from '@auth/core/types';
import type { AuthConfig } from '@auth/core';
import { implicit$FirstArg, type QRL } from '@builder.io/qwik';
import {
globalAction$,
routeLoader$,
type RequestEvent,
type RequestEventCommon,
z,
zod$,
type RequestEvent,
type RequestEventCommon,
} from '@builder.io/qwik-city';
import { isServer } from '@builder.io/qwik/build';
import { parseString, splitCookiesString } from 'set-cookie-parser';
Expand Down Expand Up @@ -39,7 +39,7 @@ export function serverAuthQrl(authOptions: QRL<(ev: RequestEventCommon) => QwikA

const isCredentials = providerId === 'credentials';

const auth = await authOptions(req);
const auth = await patchAuthOptions(authOptions, req);
const body = new URLSearchParams({ callbackUrl: callbackUrl as string });
Object.entries(rest).forEach(([key, value]) => {
body.set(key, String(value));
Expand Down Expand Up @@ -80,7 +80,7 @@ export function serverAuthQrl(authOptions: QRL<(ev: RequestEventCommon) => QwikA
const useAuthSignout = globalAction$(
async ({ callbackUrl }, req) => {
callbackUrl ??= defaultCallbackURL(req);
const auth = await authOptions(req);
const auth = await patchAuthOptions(authOptions, req);
const body = new URLSearchParams({ callbackUrl });
await authAction(body, req, `/api/auth/signout`, auth);
},
Expand All @@ -99,7 +99,7 @@ export function serverAuthQrl(authOptions: QRL<(ev: RequestEventCommon) => QwikA

const action = req.url.pathname.slice(prefix.length + 1).split('/')[0] as AuthAction;

const auth = await authOptions(req);
const auth = await patchAuthOptions(authOptions, req);
if (actions.includes(action) && req.url.pathname.startsWith(prefix + '/')) {
// Casting to `Response` because, something is off with the types in `@auth/core` here:
// Without passing `raw`, it should know it's supposed to return a `Response` object, but it doesn't.
Expand Down Expand Up @@ -223,3 +223,11 @@ async function getSessionData(req: Request, options: AuthConfig): GetSessionResu

throw new Error(data.message);
}

const patchAuthOptions = async (
authOptions: QRL<(ev: RequestEventCommon) => QwikAuthConfig>,
req: RequestEventCommon
) => {
const options = await authOptions(req);
return { ...options, basePath: '/api/auth' };
};
Loading