mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
38 lines
982 B
TypeScript
38 lines
982 B
TypeScript
import { type Session } from "@auth/core";
|
|
import { getSession } from "@solid-auth/next";
|
|
import { Component, Show } from "solid-js";
|
|
import { useRouteData } from "solid-start";
|
|
import { createServerData$, redirect } from "solid-start/server";
|
|
import { authOpts } from "~/routes/api/auth/[...solidauth]";
|
|
|
|
const Protected = (Comp: IProtectedComponent) => {
|
|
const routeData = () => {
|
|
return createServerData$(
|
|
async (_, event) => {
|
|
const session = await getSession(event.request, authOpts);
|
|
if (!session || !session.user) {
|
|
throw redirect("/");
|
|
}
|
|
return session;
|
|
},
|
|
{ key: () => ["auth_user"] }
|
|
);
|
|
};
|
|
|
|
return {
|
|
routeData,
|
|
Page: () => {
|
|
const session = useRouteData<typeof routeData>();
|
|
return (
|
|
<Show when={session()} keyed>
|
|
{(sess) => <Comp {...sess} />}
|
|
</Show>
|
|
);
|
|
},
|
|
};
|
|
};
|
|
|
|
type IProtectedComponent = Component<Session>;
|
|
|
|
export default Protected;
|