import React, { ReactNode, useState } from "react";

const AuthContext = React.createContext<any>(undefined);

AuthContext.displayName = "AuthContext";

interface AuthForm {
  userName: string;
  pwd: string;
}
export const AuthProvider = ({ children }: { children: ReactNode }) => {
  const [user, serUser] = useState<AuthForm | null>(null);
  const login = (form: AuthForm) => serUser(form);
  const loginout = () => serUser(null);
  return (
    <AuthContext.Provider
      children={children}
      value={{ user, login, loginout }}
    />
  );
};

export const useAuth = () => {
  const context = React.useContext(AuthContext);
  if (!context) {
    throw new Error("not login");
  }
  return context;
};
