Skip to content

Instantly share code, notes, and snippets.

@alancasagrande
Last active November 16, 2020 13:21
Show Gist options
  • Select an option

  • Save alancasagrande/71a2dcede48c94d0fb22ec2e0c5b5f8b to your computer and use it in GitHub Desktop.

Select an option

Save alancasagrande/71a2dcede48c94d0fb22ec2e0c5b5f8b to your computer and use it in GitHub Desktop.
import React, { useCallback, useState } from 'react';
import { login } from './api';
import Input from './Input';
export default function Login({ onLogin }) {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [invalidCredentials, setInvalidCredentials] = useState(false);
const handleSubmit = useCallback(
async (e) => {
e.preventDefault();
const res = await login(username, password);
// For simplicity, we refresh the page after authenticating
// and let app handle the flow
if (res.user) return (window.location = '/');
setInvalidCredentials(true);
},
[username, password]
);
return (
<form onSubmit={handleSubmit}>
<Input
id="username"
label="Username"
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<Input
id="password"
label="Password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button type="submit">Log in</button>
{invalidCredentials && <p>Invalid credentials</p>}
</form>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment