mirror of
https://github.com/kremalicious/umami.git
synced 2024-06-28 16:57:52 +02:00
69 lines
1.7 KiB
JavaScript
69 lines
1.7 KiB
JavaScript
import React, { useState } from 'react';
|
|
import { Formik, Form, Field } from 'formik';
|
|
import Router from 'next/router';
|
|
import { post } from 'lib/web';
|
|
import Button from '../common/Button';
|
|
import FormLayout, { FormButtons, FormError, FormMessage, FormRow } from '../layout/FormLayout';
|
|
|
|
const validate = ({ username, password }) => {
|
|
const errors = {};
|
|
|
|
if (!username) {
|
|
errors.username = 'Required';
|
|
}
|
|
if (!password) {
|
|
errors.password = 'Required';
|
|
}
|
|
|
|
return errors;
|
|
};
|
|
|
|
export default function LoginForm() {
|
|
const [message, setMessage] = useState();
|
|
|
|
const handleSubmit = async ({ username, password }) => {
|
|
const response = await post('/api/auth/login', { username, password });
|
|
|
|
if (response?.token) {
|
|
await Router.push('/');
|
|
} else {
|
|
setMessage('Incorrect username/password');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<FormLayout>
|
|
<Formik
|
|
initialValues={{
|
|
username: '',
|
|
password: '',
|
|
}}
|
|
validate={validate}
|
|
onSubmit={handleSubmit}
|
|
>
|
|
{() => (
|
|
<Form>
|
|
<h1 className="center">umami</h1>
|
|
<FormRow>
|
|
<label htmlFor="username">Username</label>
|
|
<Field name="username" type="text" />
|
|
<FormError name="username" />
|
|
</FormRow>
|
|
<FormRow>
|
|
<label htmlFor="password">Password</label>
|
|
<Field name="password" type="password" />
|
|
<FormError name="password" />
|
|
</FormRow>
|
|
<FormButtons>
|
|
<Button type="submit" variant="action">
|
|
Login
|
|
</Button>
|
|
</FormButtons>
|
|
<FormMessage>{message}</FormMessage>
|
|
</Form>
|
|
)}
|
|
</Formik>
|
|
</FormLayout>
|
|
);
|
|
}
|