Skip to content

Instantly share code, notes, and snippets.

View rasikag's full-sized avatar
🎯
Focusing

Rasika Gayan Gunarathna rasikag

🎯
Focusing
View GitHub Profile
const [complete, setComplete] = userState(false);
const [, forgotPassword] = useForgotPasswordMutation();
return (
<Wrapper variant="small">
<Formik
initialValues={{ email: "" }}
onSubmit={async (values) => {
await forgotPassword(values);
setComplete(true);
}}
// under to inputfield add this code
<Flex mt={2}>
<NextLink href="/forgot-password">
<Link ml="auto">forgot password?</Link>
</NextLink>
</Flex>
import React from "react";
const ForgotPassword: React.FC<{}> = ({}) => {
return <div>Forgot Password</div>;
};
export default ForgotPassword;
// update code to below code block in [token].tsx file.
{tokenError ? (
<Box>
<Box color="red">{tokenError}</Box>
<NextLink href="/forgot-password">
<Link>Click here to get a new token.</Link>
</NextLink>
</Box>
) : null}
// in resolvers/user.ts file
// first get the key out
const key = FORGET_PASSWORD_PREFIX + token;
const userId = await redis.get(key);
// then delete the key
await redis.del(key);
req.session.userId = user.id;
// take out toErrorMap first
const errorMap = toErrorMap(response.data.changePassword.errors);
if ('token' in errorMap) {
// this is because we defined filed as token in user.ts in backend
setTokenError(errorMap.token)
}
const token = v4();
await redis.set(
FORGET_PASSWORD_PREFIX + token,
user.id,
"ex",
1000 * 60 * 60 * 24 * 3
);
@Mutation(() => Boolean)
async forgotPassword(
@Arg("email") email: string,
@Ctx() { em }: RedditDbContext
) {
const user = await em.findOne(User, {email});
if (!user) {
return true;
}
const token = "asdasdsadassadsadsadsadsad";
const user = await em.findOne(
User,
usernameOrEmail.includes("@")
? { email: usernameOrEmail }
: { username: usernameOrEmail }
);
// add below code lines above from return method
const router = useRouter();
const [, changePassword] = useChangePasswordMutation();
// ...
onSubmit={async (values, { setErrors }) => {
const response = await changePassword({
newPassword: values.newPassword,
token,
});
if (response.data?.changePassword.errors) {