feat: refactor directus and add response validation
parent
cb93157285
commit
00d540a627
@ -1,77 +0,0 @@
|
||||
import { useLocation } from "solid-app-router";
|
||||
import {
|
||||
createComputed,
|
||||
createContext,
|
||||
createEffect,
|
||||
createResource,
|
||||
useContext,
|
||||
} from "solid-js";
|
||||
import { createStore } from "solid-js/store";
|
||||
import server, { redirect } from "solid-start/server";
|
||||
import { useRefreshy } from "~/util/refreshy";
|
||||
import { awaitJson, withRC } from "~/util";
|
||||
import { getSessionData } from "./server";
|
||||
|
||||
export const authData = (
|
||||
options_: { redirectNoAuth?: string; redirectWithAuth?: string } = {}
|
||||
) =>
|
||||
function () {
|
||||
const [refreshy] = useRefreshy();
|
||||
|
||||
const fetcher = refreshy(
|
||||
server(
|
||||
withRC(async (rc, options: typeof options_) => {
|
||||
const data = await getSessionData(rc);
|
||||
|
||||
if (options.redirectNoAuth && !data?.username) {
|
||||
throw redirect(options.redirectNoAuth);
|
||||
}
|
||||
|
||||
if (options.redirectWithAuth && data?.username) {
|
||||
throw redirect(options.redirectWithAuth);
|
||||
}
|
||||
|
||||
return { username: data.username };
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return createResource(() => fetcher(options_), {
|
||||
name: "auth",
|
||||
deferStream: true,
|
||||
});
|
||||
};
|
||||
|
||||
/*
|
||||
const AuthContext = createContext();
|
||||
export const AuthProvider = (props) => {
|
||||
const [session, { refetch }] = createResource(server(() => getSessionData()))
|
||||
const [state, setState] = createStore([{
|
||||
session: {}
|
||||
}])
|
||||
|
||||
const location = useLocation();
|
||||
|
||||
createEffect(function() {
|
||||
const url = location.pathname;
|
||||
refetch()
|
||||
})
|
||||
|
||||
createEffect(function() {
|
||||
setState(0,'session',session())
|
||||
})
|
||||
//createComputed(session)
|
||||
|
||||
return <AuthContext.Provider value={state}>
|
||||
{props.children}
|
||||
</AuthContext.Provider>
|
||||
}
|
||||
|
||||
export const useAuth = () => {
|
||||
const authContext = useContext(AuthContext)
|
||||
|
||||
console.log(authContext)
|
||||
|
||||
return authContext[0];
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue