import React, { useEffect } from 'react'; import { navigate, RouteComponentProps } from '@reach/router'; import * as yup from 'yup'; import clsx from 'clsx'; import { Controller, useForm } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; import { LockClosedIcon } from '@heroicons/react/solid'; import { useAuth } from 'src/services/auth'; import { isValid } from 'src/services/api'; import { VALIDATION_MESSAGES } from 'src/common/const'; import { showToast, ToastType } from 'src/common/util/show-toast'; const validationSchema = yup.object({ email: yup.string().required(VALIDATION_MESSAGES.required('Email')), password: yup.string().required(VALIDATION_MESSAGES.required('Password')), }); type LoginProps = RouteComponentProps; // eslint-disable-next-line @typescript-eslint/no-unused-vars export function Login(_: LoginProps) { const { auth, logIn } = useAuth(); const { handleSubmit, formState, control } = useForm<{ email: string; password: string; remember: boolean; }>({ resolver: yupResolver(validationSchema), mode: 'all', defaultValues: { email: process.env.REACT_APP_USERNAME, password: process.env.REACT_APP_PASSWORD }, }); const onSubmit = handleSubmit(async (values) => { const res = await logIn(values.email, values.password); // @ts-ignore if (res.ok) { navigate('/dashboard'); showToast('Logged in!'); } else { showToast('Username or password incorrect', ToastType.Error); } }); useEffect(() => { if (isValid(auth)) { navigate('/dashboard'); } }, [auth]); if (isValid(auth)) { return null; } return (
Stackspin

Sign in

{ return ( <> ); }} />
{ return ( <> ); }} />
); }