52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { PropsWithChildren, createContext } from "react";
|
|
import { IAuthActions } from "./auth-actions";
|
|
|
|
export interface IAuthContextState extends Partial<IAuthActions> {}
|
|
|
|
export const AuthContext = createContext<Partial<IAuthContextState>>({});
|
|
|
|
export const AuthProvider = ({
|
|
children,
|
|
authActions,
|
|
}: PropsWithChildren<{ authActions: Partial<IAuthActions> }>) => {
|
|
const handleLogin = (params: unknown) => {
|
|
try {
|
|
return Promise.resolve(authActions.login?.(params));
|
|
} catch (error) {
|
|
console.error(error);
|
|
return Promise.reject(error);
|
|
}
|
|
};
|
|
|
|
const handleLogout = (params: unknown) => {
|
|
try {
|
|
return Promise.resolve(authActions.logout?.(params));
|
|
} catch (error) {
|
|
console.error(error);
|
|
return Promise.reject(error);
|
|
}
|
|
};
|
|
|
|
const handleCheck = async () => {
|
|
try {
|
|
return Promise.resolve(authActions.check?.());
|
|
} catch (error) {
|
|
console.error(error);
|
|
return Promise.reject(error);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider
|
|
value={{
|
|
...authActions,
|
|
login: handleLogin as IAuthActions["login"],
|
|
logout: handleLogout as IAuthActions["logout"],
|
|
check: handleCheck as IAuthActions["check"],
|
|
}}
|
|
>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
};
|