MCPcopy Create free account
hub / github.com/Botloader/botloader / SessionProvider

Function SessionProvider

frontend/src/modules/session/SessionContext.tsx:27–109  ·  view source on GitHub ↗
({ children }: { children: React.ReactNode })

Source from the content-addressed store, hash-verified

25export const SessionContext = createContext<Session>(defaultSession);
26
27export function SessionProvider({ children }: { children: React.ReactNode }) {
28 let [sessionData, setSessionData] = useState<SessionStateData>({
29 apiClient: new ApiClient(CreateFetcher(), BuildConfig.botloaderApiBase),
30 signingIn: false,
31 initialized: false,
32 });
33
34 async function validateAndUpdateSession(apiClient: ApiClient) {
35 let user = await apiClient.getCurrentUser();
36 if (!isErrorResponse(user)) {
37
38 setSessionData({
39 user: user,
40 apiClient: apiClient,
41 signingIn: false,
42 initialized: true,
43 });
44
45 console.log("session restored and valid: ", user);
46 } else {
47 localStorage.removeItem("botloader_token");
48 setSessionData({
49 apiClient: anonApiClient,
50 signingIn: false,
51 initialized: true,
52 });
53 console.log("session is invalid");
54 }
55 }
56
57 async function restoreSession() {
58 let token = localStorage.getItem("botloader_token");
59 if (token) {
60 // Got a token in storage, validate it and use it
61 let client = new ApiClient(CreateFetcher(), BuildConfig.botloaderApiBase, token);
62 await validateAndUpdateSession(client);
63 } else {
64 console.log("no token in local storage, no session to restore");
65 setSessionData({
66 apiClient: anonApiClient,
67 signingIn: false,
68 initialized: true,
69 });
70 }
71 }
72
73 async function logout() {
74 await sessionData.apiClient.logout();
75 setSessionData({
76 apiClient: anonApiClient,
77 signingIn: false,
78 initialized: true,
79 })
80 }
81
82 async function logoutAllSessions() {
83 await sessionData.apiClient.deleteAllSessions();
84 setSessionData({

Callers

nothing calls this directly

Calls 2

CreateFetcherFunction · 0.90
restoreSessionFunction · 0.85

Tested by

no test coverage detected