✨ Add password reset functionality (#624)
This commit is contained in:
@@ -43,7 +43,7 @@ def send_test_email(email_to: str) -> None:
|
|||||||
email_to=email_to,
|
email_to=email_to,
|
||||||
subject_template=subject,
|
subject_template=subject,
|
||||||
html_template=template_str,
|
html_template=template_str,
|
||||||
current_environment={"project_name": settings.PROJECT_NAME, "email": email_to},
|
environment={"project_name": settings.PROJECT_NAME, "email": email_to},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ def send_reset_password_email(email_to: str, email: str, token: str) -> None:
|
|||||||
email_to=email_to,
|
email_to=email_to,
|
||||||
subject_template=subject,
|
subject_template=subject,
|
||||||
html_template=template_str,
|
html_template=template_str,
|
||||||
current_environment={
|
environment={
|
||||||
"project_name": settings.PROJECT_NAME,
|
"project_name": settings.PROJECT_NAME,
|
||||||
"username": email,
|
"username": email,
|
||||||
"email": email_to,
|
"email": email_to,
|
||||||
@@ -78,7 +78,7 @@ def send_new_account_email(email_to: str, username: str, password: str) -> None:
|
|||||||
email_to=email_to,
|
email_to=email_to,
|
||||||
subject_template=subject,
|
subject_template=subject,
|
||||||
html_template=template_str,
|
html_template=template_str,
|
||||||
current_environment={
|
environment={
|
||||||
"project_name": settings.PROJECT_NAME,
|
"project_name": settings.PROJECT_NAME,
|
||||||
"username": username,
|
"username": username,
|
||||||
"password": password,
|
"password": password,
|
||||||
@@ -104,6 +104,6 @@ def generate_password_reset_token(email: str) -> str:
|
|||||||
def verify_password_reset_token(token: str) -> str | None:
|
def verify_password_reset_token(token: str) -> str | None:
|
||||||
try:
|
try:
|
||||||
decoded_token = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
|
decoded_token = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
|
||||||
return decoded_token["email"]
|
return decoded_token["sub"]
|
||||||
except jwt.JWTError:
|
except jwt.JWTError:
|
||||||
return None
|
return None
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import { Button, Container, FormControl, Heading, Input, Text } from "@chakra-ui/react";
|
import { Button, Container, FormControl, FormErrorMessage, Heading, Input, Text } from "@chakra-ui/react";
|
||||||
import { SubmitHandler, useForm } from "react-hook-form";
|
import { SubmitHandler, useForm } from "react-hook-form";
|
||||||
|
|
||||||
import { LoginService } from "../client";
|
import { LoginService } from "../client";
|
||||||
@@ -11,14 +11,13 @@ interface FormData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const RecoverPassword: React.FC = () => {
|
const RecoverPassword: React.FC = () => {
|
||||||
const { register, handleSubmit } = useForm<FormData>();
|
const { register, handleSubmit, formState: { errors, isSubmitting } } = useForm<FormData>();
|
||||||
const showToast = useCustomToast();
|
const showToast = useCustomToast();
|
||||||
|
|
||||||
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
||||||
const response = await LoginService.recoverPassword({
|
await LoginService.recoverPassword({
|
||||||
email: data.email,
|
email: data.email,
|
||||||
});
|
});
|
||||||
console.log(response)
|
|
||||||
|
|
||||||
showToast("Email sent.", "We sent an email with a link to get back into your account.", "success");
|
showToast("Email sent.", "We sent an email with a link to get back into your account.", "success");
|
||||||
};
|
};
|
||||||
@@ -37,19 +36,14 @@ const RecoverPassword: React.FC = () => {
|
|||||||
<Heading size="xl" color="ui.main" textAlign="center" mb={2}>
|
<Heading size="xl" color="ui.main" textAlign="center" mb={2}>
|
||||||
Password Recovery
|
Password Recovery
|
||||||
</Heading>
|
</Heading>
|
||||||
<FormControl id="username">
|
<Text align="center">
|
||||||
<Text align="center">
|
A password recovery email will be sent to the registered account.
|
||||||
A password recovery email will be sent to the registered account.
|
</Text>
|
||||||
</Text>
|
<FormControl isInvalid={!!errors.email}>
|
||||||
<Input
|
<Input id='email' {...register('email', { required: 'Email is required', pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, message: 'Invalid email address' } })} placeholder='Email' type='email' />
|
||||||
{...register("email")}
|
{errors.email && <FormErrorMessage>{errors.email.message}</FormErrorMessage>}
|
||||||
|
|
||||||
mt={4}
|
|
||||||
placeholder="Enter your email"
|
|
||||||
type="text"
|
|
||||||
/>
|
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<Button bg="ui.main" color="white" _hover={{ opacity: 0.8 }} type="submit">
|
<Button bg="ui.main" color="white" _hover={{ opacity: 0.8 }} type="submit" isLoading={isSubmitting}>
|
||||||
Continue
|
Continue
|
||||||
</Button>
|
</Button>
|
||||||
</Container>
|
</Container>
|
||||||
|
72
src/new-frontend/src/pages/ResetPassword.tsx
Normal file
72
src/new-frontend/src/pages/ResetPassword.tsx
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
import { Button, Container, FormControl, FormErrorMessage, FormLabel, Heading, Input, Text } from "@chakra-ui/react";
|
||||||
|
import { SubmitHandler, useForm } from "react-hook-form";
|
||||||
|
|
||||||
|
import { LoginService, NewPassword } from "../client";
|
||||||
|
import useCustomToast from "../hooks/useCustomToast";
|
||||||
|
|
||||||
|
interface NewPasswordForm extends NewPassword {
|
||||||
|
confirm_password: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ResetPassword: React.FC = () => {
|
||||||
|
const { register, handleSubmit, getValues, formState: { errors } } = useForm<NewPasswordForm>({
|
||||||
|
mode: 'onBlur',
|
||||||
|
criteriaMode: 'all',
|
||||||
|
defaultValues: {
|
||||||
|
new_password: '',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const showToast = useCustomToast();
|
||||||
|
|
||||||
|
const onSubmit: SubmitHandler<NewPasswordForm> = async (data) => {
|
||||||
|
try {
|
||||||
|
const token = new URLSearchParams(window.location.search).get('token');
|
||||||
|
await LoginService.resetPassword({
|
||||||
|
requestBody: { new_password: data.new_password, token: token! }
|
||||||
|
});
|
||||||
|
showToast("Password reset.", "Your password has been reset successfully.", "success");
|
||||||
|
} catch (error) {
|
||||||
|
showToast("Error", "An error occurred while resetting your password.", "error");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Container
|
||||||
|
as="form"
|
||||||
|
onSubmit={handleSubmit(onSubmit)}
|
||||||
|
h="100vh"
|
||||||
|
maxW="sm"
|
||||||
|
alignItems="stretch"
|
||||||
|
justifyContent="center"
|
||||||
|
gap={4}
|
||||||
|
centerContent
|
||||||
|
>
|
||||||
|
<Heading size="xl" color="ui.main" textAlign="center" mb={2}>
|
||||||
|
Reset Password
|
||||||
|
</Heading>
|
||||||
|
<Text textAlign="center">
|
||||||
|
Please enter your new password and confirm it to reset your password.
|
||||||
|
</Text>
|
||||||
|
<FormControl mt={4} isInvalid={!!errors.new_password}>
|
||||||
|
<FormLabel htmlFor='password'>Set Password</FormLabel>
|
||||||
|
<Input id='password' {...register('new_password', { required: 'Password is required', minLength: { value: 8, message: 'Password must be at least 8 characters' } })} placeholder='Password' type='password' />
|
||||||
|
{errors.new_password && <FormErrorMessage>{errors.new_password.message}</FormErrorMessage>}
|
||||||
|
</FormControl>
|
||||||
|
<FormControl mt={4} isInvalid={!!errors.confirm_password}>
|
||||||
|
<FormLabel htmlFor='confirm_password'>Confirm Password</FormLabel>
|
||||||
|
<Input id='confirm_password' {...register('confirm_password', {
|
||||||
|
required: 'Please confirm your password',
|
||||||
|
validate: value => value === getValues().new_password || 'The passwords do not match'
|
||||||
|
})} placeholder='Password' type='password' />
|
||||||
|
{errors.confirm_password && <FormErrorMessage>{errors.confirm_password.message}</FormErrorMessage>}
|
||||||
|
</FormControl>
|
||||||
|
<Button bg="ui.main" color="white" _hover={{ opacity: 0.8 }} type="submit">
|
||||||
|
Reset Password
|
||||||
|
</Button>
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ResetPassword;
|
@@ -1,11 +1,13 @@
|
|||||||
import ErrorPage from '../pages/ErrorPage';
|
import ErrorPage from '../pages/ErrorPage';
|
||||||
import Login from '../pages/Login';
|
import Login from '../pages/Login';
|
||||||
import RecoverPassword from '../pages/RecoverPassword';
|
import RecoverPassword from '../pages/RecoverPassword';
|
||||||
|
import ResetPassword from '../pages/ResetPassword';
|
||||||
|
|
||||||
export default function publicRoutes() {
|
export default function publicRoutes() {
|
||||||
return [
|
return [
|
||||||
{ path: '/login', element: <Login />, errorElement: <ErrorPage /> },
|
{ path: '/login', element: <Login />, errorElement: <ErrorPage /> },
|
||||||
{ path: 'recover-password', element: <RecoverPassword />, errorElement: <ErrorPage /> },
|
{ path: 'recover-password', element: <RecoverPassword />, errorElement: <ErrorPage /> },
|
||||||
|
{ path: 'reset-password', element: <ResetPassword />, errorElement: <ErrorPage /> },
|
||||||
// TODO: complete this
|
// TODO: complete this
|
||||||
// { path: '*', element: <Navigate to='/login' replace /> }
|
// { path: '*', element: <Navigate to='/login' replace /> }
|
||||||
];
|
];
|
||||||
|
Reference in New Issue
Block a user