verify.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Flex } from "reflexbox/styled-components";
  2. import React, { useEffect } from "react";
  3. import styled from "styled-components";
  4. import Router from "next/router";
  5. import decode from "jwt-decode";
  6. import cookie from "js-cookie";
  7. import AppWrapper from "../components/AppWrapper";
  8. import { Button } from "../components/Button";
  9. import { useStoreActions } from "../store";
  10. import { TokenPayload } from "../types";
  11. import { NextPage } from "next";
  12. import { Col } from "../components/Layout";
  13. interface Props {
  14. token?: string;
  15. }
  16. const MessageWrapper = styled(Flex).attrs({
  17. justifyContent: "center",
  18. alignItems: "center",
  19. my: 32
  20. })``;
  21. const Message = styled.p`
  22. font-size: 24px;
  23. font-weight: 300;
  24. @media only screen and (max-width: 768px) {
  25. font-size: 18px;
  26. }
  27. `;
  28. const Icon = styled.img`
  29. width: 32px;
  30. height: 32px;
  31. margin-right: 16px;
  32. @media only screen and (max-width: 768px) {
  33. width: 26px;
  34. height: 26px;
  35. margin-right: 8px;
  36. }
  37. `;
  38. const Verify: NextPage<Props> = ({ token }) => {
  39. const addAuth = useStoreActions(s => s.auth.add);
  40. useEffect(() => {
  41. if (token) {
  42. cookie.set("token", token, { expires: 7 });
  43. const payload: TokenPayload = decode(token);
  44. addAuth(payload);
  45. }
  46. }, []);
  47. const goToHomepage = e => {
  48. e.preventDefault();
  49. Router.push("/");
  50. };
  51. return (
  52. <AppWrapper>
  53. {token ? (
  54. <Col alignItems="center">
  55. <MessageWrapper>
  56. <Icon src="/images/check.svg" />
  57. <Message>Your account has been verified successfully!</Message>
  58. </MessageWrapper>
  59. <Button icon="arrow-left" onClick={goToHomepage}>
  60. Back to homepage
  61. </Button>
  62. </Col>
  63. ) : (
  64. <MessageWrapper>
  65. <Icon src="/images/x.svg" />
  66. <Message>Invalid verification.</Message>
  67. </MessageWrapper>
  68. )}
  69. </AppWrapper>
  70. );
  71. };
  72. Verify.getInitialProps = async ({ req }) => {
  73. return { token: req && (req as any).token }; // TODO: types bro
  74. };
  75. export default Verify;