Skip to content

Instantly share code, notes, and snippets.

@martinseanhunt
Last active April 1, 2022 09:52
Show Gist options
  • Save martinseanhunt/61c328090a7be6d3729c2c6fa6741028 to your computer and use it in GitHub Desktop.
Save martinseanhunt/61c328090a7be6d3729c2c6fa6741028 to your computer and use it in GitHub Desktop.
Needs refactoring
const App = () => {
const [userId, setUserId] = useState(null);
return (
<div>
<header>
<AppBar
userId={userId}
onLogin={userId => setUserId(userId)}
onLogout={() => setUserId(null)}
/>
</header>
<main>
Hello {userId}
</main>
</div>
);
};
const LoginButton = ({ onLogin }) => {
// assume we use a 3rd party script to authenticate
useEffect(() => {
window.auth.addEventListener('user_present', userId => onLogin(userId));
}, []);
return (
<button onClick={() => window.auth.login()}>Login</button>
);
};
const LogoutButton = ({ onLogout }) => {
useEffect(() => {
window.auth.addEventListener('user_gone', () => onLogout());
}, []);
return (
<button onClick={() => window.auth.logout()}>Logout</button>
);
};
const AppBar = ({ userId, onLogin, onLogout }) => {
return (
<div>
<div id="logged-in-navigation" style={{ display: userId ? 'block' : 'none'}}>
<a href="/dashboard">Dashboard</a>
<a href="/my-account">My account</a>
<LogoutButton onLogout={onLogout} />
</div>
<div id="logged-out-navigation" style={{ display: userId ? 'none' : 'block'}}>
<LoginButton onLogin={onLogin}/>
</div>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment