-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathContext.tsx
More file actions
29 lines (25 loc) · 973 Bytes
/
Context.tsx
File metadata and controls
29 lines (25 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { createContext, useContext, type ParentProps } from "solid-js";
import { type AccessorWithLatest, useLocation, createAsync, useAction } from "@solidjs/router";
import { querySession, logout } from ".";
import type { Session } from "./server";
const Context = createContext<{
session: AccessorWithLatest<Session | null | undefined>;
signedIn: () => boolean;
signOut: () => Promise<never>;
}>();
export default function Session(props: ParentProps) {
const location = useLocation();
const session = createAsync(() => querySession(location.pathname), {
deferStream: true
});
const signOut = useAction(logout);
const signedIn = () => Boolean(session()?.id);
return (
<Context.Provider value={{ session, signedIn, signOut }}>{props.children}</Context.Provider>
);
}
export function useSession() {
const context = useContext(Context);
if (!context) throw new Error("useSession must be used within Session context");
return context;
}