From e849d55b3f73f1617b9ca6801132f720c34ed500 Mon Sep 17 00:00:00 2001 From: Dario Guarascio Date: Sat, 9 Oct 2021 12:10:29 +0200 Subject: [PATCH] autologin as separate component --- components/forms/AutoLogin.js | 24 ++++++++++++++++++++++++ components/forms/LoginForm.js | 16 +--------------- pages/login.js | 6 +++++- 3 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 components/forms/AutoLogin.js diff --git a/components/forms/AutoLogin.js b/components/forms/AutoLogin.js new file mode 100644 index 00000000..cbcde345 --- /dev/null +++ b/components/forms/AutoLogin.js @@ -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
; +} diff --git a/components/forms/LoginForm.js b/components/forms/LoginForm.js index 7f74c95a..8ac3a556 100644 --- a/components/forms/LoginForm.js +++ b/components/forms/LoginForm.js @@ -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, diff --git a/pages/login.js b/pages/login.js index f7ec7a25..c7e1f28b 100644 --- a/pages/login.js +++ b/pages/login.js @@ -1,13 +1,17 @@ import React from 'react'; import Layout from 'components/layout/Layout'; import LoginForm from 'components/forms/LoginForm'; +import AutoLogin from 'components/forms/AutoLogin'; import { useRouter } from 'next/router'; export default function LoginPage() { const { query } = useRouter(); + if (query.hash) { + return ; + } return ( - + ); }