cal.pub0.org/pages/settings/password.tsx

129 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-07-30 23:05:38 +00:00
import Head from "next/head";
import Link from "next/link";
import { useRef, useState } from "react";
import prisma from "../../lib/prisma";
import Modal from "../../components/Modal";
import Shell from "../../components/Shell";
import SettingsShell from "../../components/Settings";
2021-08-02 20:57:58 +00:00
import { useSession, getSession } from "next-auth/client";
import Loader from '@components/Loader';
2021-04-07 15:03:02 +00:00
export default function Settings(props) {
2021-07-30 23:05:38 +00:00
const [session, loading] = useSession();
const [successModalOpen, setSuccessModalOpen] = useState(false);
const oldPasswordRef = useRef<HTMLInputElement>();
const newPasswordRef = useRef<HTMLInputElement>();
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
if (loading) {
2021-08-02 20:57:58 +00:00
return <Loader/>;
2021-07-30 23:05:38 +00:00
}
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
const closeSuccessModal = () => {
setSuccessModalOpen(false);
};
2021-04-20 12:56:50 +00:00
2021-07-30 23:05:38 +00:00
async function changePasswordHandler(event) {
event.preventDefault();
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
const enteredOldPassword = oldPasswordRef.current.value;
const enteredNewPassword = newPasswordRef.current.value;
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
// TODO: Add validation
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
const response = await fetch("/api/auth/changepw", {
method: "PATCH",
body: JSON.stringify({ oldPassword: enteredOldPassword, newPassword: enteredNewPassword }),
headers: {
"Content-Type": "application/json",
},
});
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
setSuccessModalOpen(true);
}
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
return (
<Shell heading="Password" subtitle="Change the password that you use to sign in.">
2021-07-30 23:05:38 +00:00
<Head>
<title>Change Password | Calendso</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<SettingsShell>
<form className="divide-y divide-gray-200 lg:col-span-9" onSubmit={changePasswordHandler}>
<div className="py-6 lg:pb-8">
<div className="flex">
<div className="w-1/2 mr-2">
<label htmlFor="current_password" className="block text-sm font-medium text-gray-700">
Current Password
</label>
<div className="mt-1">
<input
ref={oldPasswordRef}
type="password"
name="current_password"
id="current_password"
required
2021-08-02 15:24:01 +00:00
className="shadow-sm focus:ring-black focus:border-black block w-full sm:text-sm border-gray-300 rounded-sm"
2021-07-30 23:05:38 +00:00
placeholder="Your old password"
/>
</div>
</div>
<div className="w-1/2 ml-2">
<label htmlFor="new_password" className="block text-sm font-medium text-gray-700">
New Password
</label>
<div className="mt-1">
<input
ref={newPasswordRef}
type="password"
name="new_password"
id="new_password"
required
2021-08-02 15:24:01 +00:00
className="shadow-sm focus:ring-black focus:border-black block w-full sm:text-sm border-gray-300 rounded-sm"
2021-07-30 23:05:38 +00:00
placeholder="Your super secure new password"
/>
</div>
</div>
</div>
<hr className="mt-8" />
<div className="py-4 flex justify-end">
<button
type="submit"
2021-08-02 15:24:01 +00:00
className="ml-2 bg-neutral-900 border border-transparent rounded-sm shadow-sm py-2 px-4 inline-flex justify-center text-sm font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-black">
2021-07-30 23:05:38 +00:00
Save
</button>
</div>
</div>
</form>
<Modal
heading="Password updated successfully"
description="Your password has been successfully changed."
open={successModalOpen}
handleClose={closeSuccessModal}
/>
</SettingsShell>
</Shell>
);
2021-04-07 15:03:02 +00:00
}
export async function getServerSideProps(context) {
2021-07-30 23:05:38 +00:00
const session = await getSession(context);
if (!session) {
return { redirect: { permanent: false, destination: "/auth/login" } };
}
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
const user = await prisma.user.findFirst({
where: {
email: session.user.email,
},
select: {
id: true,
username: true,
name: true,
},
});
2021-04-07 15:03:02 +00:00
2021-07-30 23:05:38 +00:00
return {
props: { user }, // will be passed to the page component as props
};
}