reset-password.tsx 3.0 KB

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