autologin as separate component

This commit is contained in:
Dario Guarascio 2021-10-09 12:10:29 +02:00
parent 6bd01aaa45
commit e849d55b3f
3 changed files with 30 additions and 16 deletions

View file

@ -0,0 +1,24 @@
import React from 'react';
import { useRouter } from 'next/router';
import usePost from 'hooks/usePost';
export default function AutoLogin({ hash = null }) {
const post = usePost();
const router = useRouter();
const handleAutologin = async ({ hash }) => {
const autologin = await post('/api/auth/hash', {
hash,
});
if (autologin.ok) {
return router.push('/');
}
};
if (hash) {
handleAutologin({ hash });
}
return <div />;
}

View file

@ -27,25 +27,11 @@ const validate = ({ username, password }) => {
return errors;
};
export default function LoginForm({ hash = null }) {
export default function LoginForm() {
const post = usePost();
const router = useRouter();
const [message, setMessage] = useState();
const handleAutologin = async ({ hash }) => {
const autologin = await post('/api/auth/hash', {
hash,
});
if (autologin.ok) {
return router.push('/');
}
};
if (hash) {
handleAutologin({ hash });
}
const handleSubmit = async ({ username, password }) => {
const { ok, status, data } = await post('/api/auth/login', {
username,