autologin as separate component
parent
6bd01aaa45
commit
e849d55b3f
|
@ -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 />;
|
||||||
|
}
|
|
@ -27,25 +27,11 @@ const validate = ({ username, password }) => {
|
||||||
return errors;
|
return errors;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function LoginForm({ hash = null }) {
|
export default function LoginForm() {
|
||||||
const post = usePost();
|
const post = usePost();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [message, setMessage] = useState();
|
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 handleSubmit = async ({ username, password }) => {
|
||||||
const { ok, status, data } = await post('/api/auth/login', {
|
const { ok, status, data } = await post('/api/auth/login', {
|
||||||
username,
|
username,
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Layout from 'components/layout/Layout';
|
import Layout from 'components/layout/Layout';
|
||||||
import LoginForm from 'components/forms/LoginForm';
|
import LoginForm from 'components/forms/LoginForm';
|
||||||
|
import AutoLogin from 'components/forms/AutoLogin';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
|
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const { query } = useRouter();
|
const { query } = useRouter();
|
||||||
|
if (query.hash) {
|
||||||
|
return <AutoLogin hash={query.hash} />;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<Layout title="login" header={false} footer={false} center>
|
<Layout title="login" header={false} footer={false} center>
|
||||||
<LoginForm hash={query.hash} />
|
<LoginForm />
|
||||||
</Layout>
|
</Layout>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue