Garrett Floyd
Explore posts from serversBABetter Auth
•Created by Tuzemec on 4/14/2025 in #help
Anyone managed to create a server side protected route?
No problem. Using authClient is definitely an option. My reason for using a server action is that I don't know how to catch and display any potential login errors to the user using authClient. With server actions you can use the useSubmission() hook to get all the info about an action you'll need to keep the user in the loop about what's going on in a convenient way. If your interested in an example, I recently made a simple dummy project to showcase a possible bug I'm experiencing with better-auth that does this. Here is a link to the project: https://github.com/Garrett-Floyd/better_auth_error. Here is a link to the bug report in case I'm doing it wrong and leading you astray lol: https://github.com/better-auth/better-auth/issues/2303
12 replies
BABetter Auth
•Created by Tuzemec on 4/14/2025 in #help
Anyone managed to create a server side protected route?
Keep in mind for this last code I'm experiencing a bug myself currently, but I don't think its related to retrieving the session cookie using getRequestEvent()
12 replies
BABetter Auth
•Created by Tuzemec on 4/14/2025 in #help
Anyone managed to create a server side protected route?
import { action } from "@solidjs/router";
import { redirect } from "@solidjs/router";
import { getRequestEvent } from "solid-js/web";
import { auth } from "~/lib/auth";
export const signInAction = action( async (formData: FormData) => {
"use server";
const email = formData.get("email") as string;
const password = formData.get("password") as string;
const response = await auth.api.signInEmail({ asResponse: true, headers: getRequestEvent()?.request.headers, body: { email: email, password: password } }); if (response.ok) { const event = getRequestEvent(); event?.response.headers.set("set-cookie", response.headers.get("set-cookie") as string); throw redirect("/test"); } else { const result = await response.json(); throw new Error(
const response = await auth.api.signInEmail({ asResponse: true, headers: getRequestEvent()?.request.headers, body: { email: email, password: password } }); if (response.ok) { const event = getRequestEvent(); event?.response.headers.set("set-cookie", response.headers.get("set-cookie") as string); throw redirect("/test"); } else { const result = await response.json(); throw new Error(
Error code : ${response.status}, ${result.message}
);
}
});12 replies
BABetter Auth
•Created by Tuzemec on 4/14/2025 in #help
Anyone managed to create a server side protected route?
Heres the code using getRequestEvent which you'll have to use for actions:
12 replies
BABetter Auth
•Created by Tuzemec on 4/14/2025 in #help
Anyone managed to create a server side protected route?
import { type APIEvent } from "@solidjs/start/server";
import { auth } from "~/lib/auth";
export async function GET({request, params}: APIEvent) {
const headers = request.headers;
console.log("recieved headers are:")
headers?.forEach((value, key) => console.log(
${key}: ${value}
));
console.log(" ");
const session = await auth.api.getSession({
headers: headers as Headers,
});
let returnVal: Response | undefined;
if (session === null) {
returnVal = new Response(null, {
status: 401,
statusText: "couldn't get session",
});
} else {
returnVal = new Response(null, {
status: 200,
statusText: String(headers?.get("cookie") as string),
})
}
return returnVal;
};12 replies
BABetter Auth
•Created by Tuzemec on 4/14/2025 in #help
Anyone managed to create a server side protected route?
Here's the sample code for the API route:
12 replies
BABetter Auth
•Created by Tuzemec on 4/14/2025 in #help
Anyone managed to create a server side protected route?
In an API route you use can get it from the request parameter passed in to the method. More generally you can get it anywhere on the server by using solidjs's getRequestEvent(). I will include some sample code below.
12 replies
How do you get createAsync to refetch or mutate?
Thanks for your help again. I have been suspicious that I have been messing up the preload syntax somehow, so that what is preloaded is not connected to what I'm using in my createAsync resource. However, as I looked over my code to prepare a simplified example to get your opinion, I realized I had a "use server" directive at the top of the file I declared my queries in. That seem like a likely source for the problem I'm having and sure enough after removing it everything works as intend.
12 replies
How do you get createAsync to refetch or mutate?
Ends up that the function body within createAsync is a reactive context so calling a signal there will trigger a refetch. Thankfully useSubmission outputs a signal based entity, so when my action resolves and gets a result it reads it into the useSubmission resource which then retriggers the createAsync resource as a derived signal, refetching the data.
12 replies
How do you get createAsync to refetch or mutate?
const refreshTable = useSubmission(delEntryAction);
let tableEntries = createAsync(() => { let data; if (refreshTable.result === undefined){ data = getInitFetch(props.apiURL, limit(), pageNum()); } else { data = refreshTable.result; } return data; });
let tableEntries = createAsync(() => { let data; if (refreshTable.result === undefined){ data = getInitFetch(props.apiURL, limit(), pageNum()); } else { data = refreshTable.result; } return data; });
12 replies
How do you get createAsync to refetch or mutate?
Thanks for the help everyone. The consensus seems to be that the helper functions like revalidate and reload should automatically cause the the createAsync based resource to refetch. However, this is not what I'm seeing. I did however find another solution. Consider the following code snippet
12 replies
How do you get createAsync to refetch or mutate?
I just wanted to add that my initial assumption was that using one of the helper functions to invalidate the cache of the query would cause the createAsync based resource to update the UI but I've tried using reload, revalidate, and redirect for good measure in the return of my action and it hasn't been working so far.
12 replies