kriasoft / graphql-starter-kit

💥 Monorepo template (seed project) pre-configured with GraphQL API, PostgreSQL, React, and Joy UI.
https://graphqlstart.com
MIT License
3.88k stars 553 forks source link

Convert CommonJS to ESM; Auth/Theme providers #309

Closed koistya closed 2 years ago

koistya commented 2 years ago

Accessing the currently logged in user object from anywhere in the app

import { useAuth } from "../core";

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

  // auth.me → { id: "xxx", username: "john", ... } | null | undefined
  // auth.signIn() → opens login dialog
  // auth.signIn({ method: "Google" }) → opens Google login screen (in a popup window)
  // auth.signOut() → clears authentication session

  return (
    <p>{me ? `Hello, ${me.name}!` : <button onClick={auth.signIn}>SignIn</button>}
  )
}

Show login dialog for some user actions requiring authentication

import { useAuthCallback } from "../core";

function Example(): JSX.Element {
  const handleClick = useAuthCallback(() => {
    alert("You will see this message after signing in.");
  }, []);

  return <button onClick={handleClick}>Click me!</button>
}

Toggle between light/dark themes

import { useTheme } from "../core";

function Example(): JSX.Element {
  const [theme, toggleTheme] = useTheme();

  return (
    <button onClick={toggleTheme}>
      Dark mode: {theme.palette.mode === "light" ? "ON" : "OFF"}
    </button>
  )
}

Marking some routes as requiring authentication (e.g. https://example.com/settings)

{
  path: "/settings",
  query: graphql`...`,
  authorize: true, // <-- only authenticated users can access this page
  render(data) { ... }
}