reset-password.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { useFormState } from "react-use-form-state";
  2. import React, { useEffect, useState } from "react";
  3. import { Flex } from "reflexbox/styled-components";
  4. import Router from "next/router";
  5. import decode from "jwt-decode";
  6. import { NextPage } from "next";
  7. import cookie from "js-cookie";
  8. import axios from "axios";
  9. import { useStoreState, useStoreActions } from "../store";
  10. import BodyWrapper from "../components/BodyWrapper";
  11. import TextInput from "../components/TextInput";
  12. import { Button } from "../components/Button";
  13. import { TokenPayload } from "../types";
  14. import Text from "../components/Text";
  15. import { useMessage } from "../hooks";
  16. import { API } from "../consts";
  17. interface Props {
  18. token?: string;
  19. }
  20. const ResetPassword: NextPage<Props> = ({ token }) => {
  21. const auth = useStoreState(s => s.auth);
  22. const addAuth = useStoreActions(s => s.auth.add);
  23. const [loading, setLoading] = useState(false);
  24. const [message, setMessage] = useMessage();
  25. const [formState, { email }] = useFormState<{ email: string }>();
  26. useEffect(() => {
  27. if (auth.isAuthenticated) {
  28. Router.push("/settings");
  29. }
  30. if (token) {
  31. cookie.set("token", token, { expires: 7 });
  32. const decoded: TokenPayload = decode(token);
  33. addAuth(decoded);
  34. Router.push("/settings");
  35. }
  36. }, []);
  37. const onSubmit = async e => {
  38. e.preventDefault();
  39. if (!formState.validity.email) return;
  40. setLoading(true);
  41. setMessage();
  42. try {
  43. await axios.post(API.RESET_PASSWORD, {
  44. email: formState.values.email
  45. });
  46. setMessage("Reset password email has been sent.", "green");
  47. } catch (error) {
  48. setMessage(error?.response?.data?.error || "Couldn't reset password.");
  49. }
  50. setLoading(false);
  51. };
  52. // FIXME: make a container for width
  53. return (
  54. <BodyWrapper>
  55. <Flex width={600} maxWidth="97%" flexDirection="column">
  56. <Text as="h2" fontWeight={700} my={3}>
  57. Reset password
  58. </Text>
  59. <Text as="p" mb={4}>
  60. If you forgot you password you can use the form below to get reset
  61. password link.
  62. </Text>
  63. w
  64. <Flex
  65. as="form"
  66. flexDirection={["column", "row"]}
  67. alignItems={["flex-start", "center"]}
  68. justifyContent="flex-start"
  69. onSubmit={onSubmit}
  70. >
  71. <TextInput
  72. {...email("email")}
  73. placeholder="Email address"
  74. height={[44, 54]}
  75. width={[1, 1 / 2]}
  76. mr={3}
  77. autoFocus
  78. required
  79. />
  80. <Button
  81. icon={loading ? "loader" : ""}
  82. type="submit"
  83. height={[40, 44]}
  84. my={3}
  85. >
  86. Reset password
  87. </Button>
  88. </Flex>
  89. <Text fontSize={14} fontWeight={400} color={message.color} mt={2}>
  90. {message.text}
  91. </Text>
  92. </Flex>
  93. </BodyWrapper>
  94. );
  95. };
  96. ResetPassword.getInitialProps = async ctx => {
  97. return { token: ctx.req && (ctx.req as any).token };
  98. };
  99. export default ResetPassword;