Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 1603x 1603x 1603x 495x 541x 541x 541x 541x 541x 524x 2x 1x 1x 1x 1x 1x 1x 524x 524x 1603x | import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { AUTH } from "i18n/namespaces";
import { useRouter } from "next/router";
import React, { Context, ReactNode, useContext, useEffect } from "react";
import { jailRoute, loginRoute } from "routes";
import { setUnauthenticatedErrorHandler } from "service/client";
import useStablePush from "utils/useStablePush";
import { JAILED_ERROR_MESSAGE } from "./constants";
import useAuthStore, { AuthStoreType } from "./useAuthStore";
const AuthContext = React.createContext<null | AuthStoreType>(null);
function useAppContext<T>(context: Context<T | null>) {
const contextValue = useContext(context);
Iif (contextValue === null) {
throw Error("No context provided!");
}
return contextValue;
}
export default function AuthProvider({ children }: { children: ReactNode }) {
const { t } = useTranslation(AUTH);
const store = useAuthStore();
const router = useRouter();
const push = useStablePush();
useEffect(() => {
setUnauthenticatedErrorHandler(async (e: RpcError) => {
// the backend will return "Permission denied" if you're just jailed, and "Unauthorized" otherwise
if (e.message === JAILED_ERROR_MESSAGE) {
const isJailRouteException = router.pathname.includes("delete-account");
await store.authActions.updateJailStatus();
if (!isJailRouteException) {
// if the user is jailed, redirect them to the jail route
push(jailRoute);
}
} else {
// completely logged out
await store.authActions.logout();
push(loginRoute);
}
});
return () => {
setUnauthenticatedErrorHandler(async () => {});
};
}, [store.authActions, push, t, router.pathname]);
return <AuthContext.Provider value={store}>{children}</AuthContext.Provider>;
}
export const useAuthContext = () => useAppContext(AuthContext);
|