//Lib import Head from 'next/head'; import { useForm } from 'react-hook-form'; import { useState } from 'react'; import { SpinnerDotted } from 'spinners-react'; import { toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import { useSession } from 'next-auth/react'; import { authOptions } from '../../pages/api/auth/[...nextauth]'; import { unstable_getServerSession } from 'next-auth/next'; //Components import Error from '../../Components/UI/Error/Error'; export default function Account() { ////Var const { register, handleSubmit, formState: { errors }, reset, } = useForm(); const { status, data } = useSession(); ////State const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(); ////Functions //Form submit Handler for ADD a repo const formSubmitHandler = async (data) => { //Remove old error setError(); //Loading button on submit to avoid multiple send. setIsLoading(true); //POST API to send the new and old password const response = await fetch('/api/account/updatePassword', { method: 'PUT', headers: { 'Content-type': 'application/json', }, body: JSON.stringify(data), }); const result = await response.json(); if (!response.ok) { setIsLoading(false); reset(); setError(result.message); setTimeout(() => setError(), 4000); } else { reset(); setIsLoading(false); toast.success('🔑 Password edited !', { position: 'top-right', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, }); } }; return ( <> Account - BorgWarehouse

Welcome {status === 'authenticated' && data.user.name}{' '} 👋

Change your password

{error && }

{errors.oldPassword && errors.oldPassword.type === 'required' && ( This field is required. )}

{errors.newPassword && ( This field is required. )}

); } export async function getServerSideProps(context) { //Var const session = await unstable_getServerSession( context.req, context.res, authOptions ); if (!session) { return { redirect: { destination: '/login', permanent: false, }, }; } return { props: {}, }; }