reset-password.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { useFormState } from "react-use-form-state";
  2. import React, { useEffect, useState } from "react";
  3. import { Flex } from "rebass/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/Input";
  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 { APIv2 } 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, label }] = useFormState<{ email: string }>(null, {
  28. withIds: true
  29. });
  30. useEffect(() => {
  31. if (auth.isAuthenticated) {
  32. Router.push("/settings");
  33. }
  34. if (token) {
  35. cookie.set("token", token, { expires: 7 });
  36. const decoded: TokenPayload = decode(token);
  37. addAuth(decoded);
  38. Router.push("/settings");
  39. }
  40. }, [auth, token, addAuth]);
  41. const onSubmit = async (e) => {
  42. e.preventDefault();
  43. if (!formState.validity.email) return;
  44. setLoading(true);
  45. setMessage();
  46. try {
  47. await axios.post(APIv2.AuthResetPassword, {
  48. email: formState.values.email
  49. });
  50. setMessage("Reset password email has been sent.", "green");
  51. } catch (error) {
  52. setMessage(error?.response?.data?.error || "Couldn't reset password.");
  53. }
  54. setLoading(false);
  55. };
  56. // FIXME: make a container for width
  57. return (
  58. <AppWrapper>
  59. <Col width={600} maxWidth="100%" px={3}>
  60. <H2 my={3} bold>
  61. Reset password
  62. </H2>
  63. <Text mb={4}>
  64. If you forgot you password you can use the form below to get reset
  65. password link.
  66. </Text>
  67. <Text {...label("homepage")} as="label" mt={2} fontSize={[15, 16]} bold>
  68. Email address
  69. </Text>
  70. <Flex
  71. as="form"
  72. alignItems="center"
  73. justifyContent="flex-start"
  74. onSubmit={onSubmit}
  75. >
  76. <TextInput
  77. {...email("email")}
  78. placeholder="Email address..."
  79. height={[44, 54]}
  80. width={[1, 1 / 2]}
  81. mr={3}
  82. autoFocus
  83. required
  84. />
  85. <Button type="submit" height={[40, 44]} my={3}>
  86. {loading && <Icon name={"spinner"} stroke="white" mr={2} />}
  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;