2022-01-15 15:45:46 +00:00
|
|
|
import {
|
2022-01-17 07:33:28 +00:00
|
|
|
Button,
|
|
|
|
FormControl,
|
|
|
|
FormLabel,
|
|
|
|
Input,
|
|
|
|
useToast,
|
|
|
|
VStack,
|
2022-01-19 16:50:25 +00:00
|
|
|
Text,
|
2022-01-17 07:33:28 +00:00
|
|
|
} from '@chakra-ui/react';
|
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { useMutation } from 'urql';
|
|
|
|
|
|
|
|
import { AuthLayout } from '../layouts/AuthLayout';
|
|
|
|
import { AdminLogin, AdminSignup } from '../graphql/mutation';
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
|
import { useAuthContext } from '../contexts/AuthContext';
|
|
|
|
import { capitalizeFirstLetter, hasAdminSecret } from '../utils';
|
2022-01-15 15:45:46 +00:00
|
|
|
|
2022-01-19 16:50:25 +00:00
|
|
|
export default function Auth() {
|
2022-01-17 07:33:28 +00:00
|
|
|
const [loginResult, login] = useMutation(AdminLogin);
|
|
|
|
const [signUpResult, signup] = useMutation(AdminSignup);
|
|
|
|
const { setIsLoggedIn } = useAuthContext();
|
2022-01-15 15:45:46 +00:00
|
|
|
|
2022-01-17 07:33:28 +00:00
|
|
|
const toast = useToast();
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const isLogin = hasAdminSecret();
|
2022-01-15 15:45:46 +00:00
|
|
|
|
2022-01-17 07:33:28 +00:00
|
|
|
const handleSubmit = (e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const formValues = [...e.target.elements].reduce((agg: any, elem: any) => {
|
|
|
|
if (elem.id) {
|
|
|
|
return {
|
|
|
|
...agg,
|
|
|
|
[elem.id]: elem.value,
|
|
|
|
};
|
|
|
|
}
|
2022-01-15 15:45:46 +00:00
|
|
|
|
2022-01-17 07:33:28 +00:00
|
|
|
return agg;
|
|
|
|
}, {});
|
2022-01-15 15:45:46 +00:00
|
|
|
|
2022-01-17 07:33:28 +00:00
|
|
|
(isLogin ? login : signup)({
|
|
|
|
secret: formValues['admin-secret'],
|
|
|
|
}).then((res) => {
|
|
|
|
if (res.data) {
|
2022-01-19 16:50:25 +00:00
|
|
|
setIsLoggedIn(true);
|
2022-01-17 07:33:28 +00:00
|
|
|
navigate('/', { replace: true });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2022-01-15 15:45:46 +00:00
|
|
|
|
2022-01-17 07:33:28 +00:00
|
|
|
const errors = isLogin ? loginResult.error : signUpResult.error;
|
2022-01-15 15:45:46 +00:00
|
|
|
|
2022-01-17 07:33:28 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (errors?.graphQLErrors) {
|
|
|
|
(errors?.graphQLErrors || []).map((error: any) => {
|
|
|
|
toast({
|
|
|
|
title: capitalizeFirstLetter(error.message),
|
|
|
|
isClosable: true,
|
|
|
|
status: 'error',
|
|
|
|
position: 'bottom-right',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [errors]);
|
2022-01-15 15:45:46 +00:00
|
|
|
|
2022-01-17 07:33:28 +00:00
|
|
|
return (
|
|
|
|
<AuthLayout>
|
2022-01-19 16:50:25 +00:00
|
|
|
<Text
|
|
|
|
fontSize="large"
|
|
|
|
textAlign="center"
|
|
|
|
color="gray.600"
|
|
|
|
fontWeight="bold"
|
|
|
|
mb="2"
|
|
|
|
>
|
2022-02-05 03:45:36 +00:00
|
|
|
Hello Admin 👋 <br />
|
2022-01-19 16:50:25 +00:00
|
|
|
</Text>
|
|
|
|
<Text fontSize="large" textAlign="center" color="gray.500" mb="8">
|
2022-02-05 03:45:36 +00:00
|
|
|
Welcome to Admin Dashboard
|
2022-01-19 16:50:25 +00:00
|
|
|
</Text>
|
2022-01-17 07:33:28 +00:00
|
|
|
<form onSubmit={handleSubmit}>
|
2022-01-19 16:50:25 +00:00
|
|
|
<VStack spacing="5" justify="space-between">
|
2022-01-17 07:33:28 +00:00
|
|
|
<FormControl isRequired>
|
2022-02-05 03:45:36 +00:00
|
|
|
<FormLabel htmlFor="admin-username">Username</FormLabel>
|
|
|
|
<Input
|
|
|
|
size="lg"
|
|
|
|
id="admin-username"
|
|
|
|
placeholder="Username"
|
|
|
|
disabled
|
|
|
|
value="admin"
|
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
<FormControl isRequired>
|
|
|
|
<FormLabel htmlFor="admin-secret">Password</FormLabel>
|
2022-01-17 07:33:28 +00:00
|
|
|
<Input
|
|
|
|
size="lg"
|
|
|
|
id="admin-secret"
|
|
|
|
placeholder="Admin secret"
|
|
|
|
type="password"
|
|
|
|
minLength={!isLogin ? 6 : 1}
|
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
<Button
|
|
|
|
isLoading={signUpResult.fetching || loginResult.fetching}
|
|
|
|
colorScheme="blue"
|
|
|
|
size="lg"
|
|
|
|
w="100%"
|
|
|
|
d="block"
|
|
|
|
type="submit"
|
|
|
|
>
|
|
|
|
{isLogin ? 'Login' : 'Sign up'}
|
|
|
|
</Button>
|
2022-01-19 16:50:25 +00:00
|
|
|
{isLogin ? (
|
|
|
|
<Text color="gray.600" fontSize="sm">
|
|
|
|
<b>Note:</b> In case if you have forgot your admin secret, you can
|
|
|
|
reset it by updating <code>ADMIN_SECRET</code> environment
|
|
|
|
variable. For more information, please refer to the{' '}
|
|
|
|
<a href="https://docs.authorizer.dev/core/env/">documentation</a>.
|
|
|
|
</Text>
|
|
|
|
) : (
|
|
|
|
<Text color="gray.600" fontSize="sm">
|
2022-02-05 03:45:36 +00:00
|
|
|
<b>Note:</b> Configure the password to start using your dashboard.
|
2022-01-19 16:50:25 +00:00
|
|
|
</Text>
|
|
|
|
)}
|
2022-01-17 07:33:28 +00:00
|
|
|
</VStack>
|
|
|
|
</form>
|
|
|
|
</AuthLayout>
|
|
|
|
);
|
2022-01-19 16:50:25 +00:00
|
|
|
}
|