kriasoft / react-starter-kit

The web's most popular Jamstack front-end template (boilerplate) for building web applications with React
https://reactstarter.com
MIT License
22.69k stars 4.16k forks source link

refactor: Sign in / sign out via Social #1972

Closed koistya closed 2 years ago

koistya commented 2 years ago

Add AuthProvider component and useAuth() Rreact hook for handling authentication. Usage example:

improt { useAuth, LoginMethod } from "../core";

function Example(): JSX.Element {
  const auth = useAuth();

  function signIn() {
    auth.signIn({ method: LoginMethod.Google }).then(user => { ... });
  }

  return (
    <div>
      <button onClick={signIn}> Continue with Google </button>
    </div>
  );
}
improt { useAuth } from "../core";

function Example(): JSX.Element {
  const { me, ...auth } = useAuth();

  function handleClick() {
    auth.signOut().then(()=> { ... });
  }

  return (
    <div>
      <span>Welcome, {me.name}!</span> <button onClick={signOut}> Sign Out </button>
    </div>
  );
}