This commit is contained in:
Timur Ercan
2023-01-13 19:01:58 +01:00
parent ef59ab1688
commit 3baed98d08
4 changed files with 70 additions and 34 deletions

View File

@ -34,20 +34,30 @@ export default function Login() {
const onSubmit = async (values: LoginValues) => { const onSubmit = async (values: LoginValues) => {
setErrorMessage(null); setErrorMessage(null);
const res = await signIn<"credentials">("credentials", { const res = await toast.promise(
...values, signIn<"credentials">("credentials", {
callbackUrl, ...values,
redirect: false, callbackUrl,
}); redirect: false,
}),
{
loading: "Loggin in...",
success: "Login successful.",
error: "Could not log in :/",
},
{
style: {
minWidth: "200px",
},
}
);
if (!res) { if (!res) {
setErrorMessage("error"); setErrorMessage("error");
toast.error("Something went wrong."); toast.error("Something went wrong.");
} }
// we're logged in! let's do a hard refresh to the desired url // we're logged in! let's do a hard refresh to the desired url
else if (!res.error) { else if (!res.error) {
router.push(callbackUrl).then(() => { router.push(callbackUrl);
toast.success("Login successful");
});
// toast.error("error"); // toast.error("error");
} }
// fallback if error not found // fallback if error not found
@ -89,7 +99,7 @@ export default function Login() {
autoComplete="email" autoComplete="email"
required required
className="relative block w-full appearance-none rounded-none rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-neon focus:outline-none focus:ring-neon sm:text-sm" className="relative block w-full appearance-none rounded-none rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-neon focus:outline-none focus:ring-neon sm:text-sm"
placeholder="Email address" placeholder="Email"
/> />
</div> </div>
<div> <div>
@ -156,7 +166,7 @@ export default function Login() {
</FormProvider> </FormProvider>
</div> </div>
</div> </div>
<Toaster position="top-center" /> {/* <Toaster position="top-center" /> */}
</> </>
); );
} }

View File

@ -51,9 +51,22 @@ const userNavigation = [
href: "", href: "",
click: async (e: any) => { click: async (e: any) => {
e.preventDefault(); e.preventDefault();
const res: any = await signOut({ callbackUrl: "/login" }).finally(() => { const res: any = await toast.promise(
if (!res?.error) toast.success("Logout successful"); signOut({ callbackUrl: "/login" }),
}); {
loading: "Logging out",
success: "Your are logged out.",
error: "Could not log out :/",
},
{
style: {
minWidth: "200px",
},
success: {
duration: 10000,
},
}
);
}, },
icon: ArrowRightOnRectangleIcon, icon: ArrowRightOnRectangleIcon,
}, },
@ -229,7 +242,7 @@ export default function TopNavigation() {
</> </>
)} )}
</Disclosure> </Disclosure>
<Toaster position="top-center"></Toaster> {/* <Toaster position="top-center"></Toaster> */}
</> </>
); );
} }

View File

@ -2,7 +2,7 @@ import { XCircleIcon } from "@heroicons/react/24/outline";
import { signIn } from "next-auth/react"; import { signIn } from "next-auth/react";
import Link from "next/link"; import Link from "next/link";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form"; import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import { Toast, Toaster } from "react-hot-toast"; import { toast, Toast, Toaster } from "react-hot-toast";
import Logo from "./logo"; import Logo from "./logo";
@ -29,26 +29,37 @@ export default function Signup() {
}; };
const signUp: SubmitHandler<FormValues> = async (data) => { const signUp: SubmitHandler<FormValues> = async (data) => {
// methods.clearErrors(); const res = await toast.promise(
await fetch("/api/auth/signup", { fetch("/api/auth/signup", {
body: JSON.stringify({ body: JSON.stringify({
...data,
}),
headers: {
"Content-Type": "application/json",
},
method: "POST",
})
.then(handleErrors)
.then(async () => {
await signIn<"credentials">("credentials", {
...data, ...data,
callbackUrl: "http://localhost:3000/dashboard", }),
}); headers: {
"Content-Type": "application/json",
},
method: "POST",
}) })
.catch((err) => { .then(handleErrors)
methods.setError("apiError", { message: err.message }); .then(async () => {
}); await signIn<"credentials">("credentials", {
...data,
callbackUrl: "http://localhost:3000/dashboard",
});
})
.catch((err) => {
methods.setError("apiError", { message: err.message });
}),
{
loading: "Creating your account...",
success: "Done!",
error: (err) => err.message,
},
{
style: {
minWidth: "200px",
},
}
);
}; };
function renderApiError() { function renderApiError() {
@ -202,7 +213,7 @@ export default function Signup() {
</FormProvider> </FormProvider>
</div> </div>
</div> </div>
<Toaster position="top-center"></Toaster> {/* <Toaster position="top-center"></Toaster> */}
</> </>
); );
} }

View File

@ -4,6 +4,7 @@ import type { AppProps } from "next/app";
import { NextPage } from "next"; import { NextPage } from "next";
import { SessionProvider } from "next-auth/react"; import { SessionProvider } from "next-auth/react";
export { coloredConsole } from "@documenso/lib"; export { coloredConsole } from "@documenso/lib";
import { Toaster } from "react-hot-toast";
export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & { export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
getLayout?: (page: ReactElement) => ReactNode; getLayout?: (page: ReactElement) => ReactNode;
@ -20,6 +21,7 @@ export default function App({
const getLayout = Component.getLayout || ((page: any) => page); const getLayout = Component.getLayout || ((page: any) => page);
return ( return (
<SessionProvider session={session}> <SessionProvider session={session}>
<Toaster position="top-center"></Toaster>
{getLayout(<Component {...pageProps} />)} {getLayout(<Component {...pageProps} />)}
</SessionProvider> </SessionProvider>
); );