This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [complete, setComplete] = userState(false); | |
const [, forgotPassword] = useForgotPasswordMutation(); | |
return ( | |
<Wrapper variant="small"> | |
<Formik | |
initialValues={{ email: "" }} | |
onSubmit={async (values) => { | |
await forgotPassword(values); | |
setComplete(true); | |
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// under to inputfield add this code | |
<Flex mt={2}> | |
<NextLink href="/forgot-password"> | |
<Link ml="auto">forgot password?</Link> | |
</NextLink> | |
</Flex> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
const ForgotPassword: React.FC<{}> = ({}) => { | |
return <div>Forgot Password</div>; | |
}; | |
export default ForgotPassword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const token = v4(); | |
await redis.set( | |
FORGET_PASSWORD_PREFIX + token, | |
user.id, | |
"ex", | |
1000 * 60 * 60 * 24 * 3 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const user = await em.findOne( | |
User, | |
usernameOrEmail.includes("@") | |
? { email: usernameOrEmail } | |
: { username: usernameOrEmail } | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
NewerOlder