reset-password.tsx 3.0 KB

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