fix: route web auth through same origin
This commit is contained in:
@@ -1,12 +1,8 @@
|
|||||||
import { env } from "@code/env/web";
|
|
||||||
import { redirect } from "react-router";
|
import { redirect } from "react-router";
|
||||||
|
|
||||||
interface AuthLoaderData {
|
interface AuthLoaderData {
|
||||||
readonly token: string | null;
|
readonly token: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokenUrl = new URL("/api/auth/convex/token", env.VITE_AUTH_URL);
|
|
||||||
|
|
||||||
export const loadAuthToken = async (
|
export const loadAuthToken = async (
|
||||||
request: Request
|
request: Request
|
||||||
): Promise<AuthLoaderData> => {
|
): Promise<AuthLoaderData> => {
|
||||||
@@ -14,9 +10,12 @@ export const loadAuthToken = async (
|
|||||||
if (!cookie) {
|
if (!cookie) {
|
||||||
return { token: null };
|
return { token: null };
|
||||||
}
|
}
|
||||||
|
const tokenUrl = new URL(
|
||||||
|
"/api/auth/convex/token",
|
||||||
|
new URL(request.url).origin
|
||||||
|
);
|
||||||
|
|
||||||
const headers = new Headers({ cookie });
|
const headers = new Headers({ cookie });
|
||||||
headers.set("host", tokenUrl.host);
|
|
||||||
const response = await fetch(tokenUrl, { headers });
|
const response = await fetch(tokenUrl, { headers });
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
return { token: null };
|
return { token: null };
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { env } from "@code/env/web";
|
|
||||||
import { convexClient } from "@convex-dev/better-auth/client/plugins";
|
import { convexClient } from "@convex-dev/better-auth/client/plugins";
|
||||||
import { createAuthClient } from "better-auth/react";
|
import { createAuthClient } from "better-auth/react";
|
||||||
|
|
||||||
export const authClient = createAuthClient({
|
export const authClient = createAuthClient({
|
||||||
baseURL: env.VITE_AUTH_URL,
|
baseURL: typeof window === "undefined" ? undefined : window.location.origin,
|
||||||
plugins: [convexClient()],
|
plugins: [convexClient()],
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user