|
1 | | -import React, { useContext, useState } from "react"; |
| 1 | +import React, { useContext, useState, useEffect } from "react"; |
| 2 | +import { Text, Flex } from "@chakra-ui/react"; |
2 | 3 | import { Redirect } from "react-router-dom"; |
3 | 4 | import { isSignInWithEmailLink } from "firebase/auth"; |
4 | 5 | import auth from "../../firebase/firebase"; |
5 | 6 | import authAPIClient from "../../APIClients/AuthAPIClient"; |
6 | | -import { HOME_PAGE } from "../../constants/Routes"; |
| 7 | +import { CREATE_PASSWORD_PAGE, HOME_PAGE } from "../../constants/Routes"; |
7 | 8 | import AuthContext from "../../contexts/AuthContext"; |
8 | 9 | import { AuthenticatedUser } from "../../types/AuthTypes"; |
9 | | - |
10 | | -let didInit = false; |
| 10 | +import ResponsiveModalWindow from "../common/responsive/ResponsiveModalWindow"; |
11 | 11 |
|
12 | 12 | const Login = (): React.ReactElement => { |
13 | 13 | const { authenticatedUser, setAuthenticatedUser } = useContext(AuthContext); |
14 | 14 | const [email, setEmail] = useState(""); |
15 | 15 | const [password, setPassword] = useState(""); |
16 | | - const onLogInClick = async () => { |
17 | | - const user: AuthenticatedUser = await authAPIClient.login(email, password); |
18 | | - setAuthenticatedUser(user); |
19 | | - }; |
20 | | - const checkIfSignInLink = async () => { |
21 | | - if (!authenticatedUser) { |
| 16 | + const [redirectTo, setRedirectTo] = useState<string | null>(null); |
| 17 | + const [status, setStatus] = useState<"loading" | "error" | "default">( |
| 18 | + "default", |
| 19 | + ); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + setStatus("loading"); |
| 23 | + const checkIfSignInLink = async () => { |
22 | 24 | const url = window.location.href; |
23 | 25 | const urlSearchParams = new URLSearchParams(window.location.search); |
24 | 26 | const signInEmail = urlSearchParams.get("email"); // passed in from actionCode |
25 | 27 | const isSignInLink = isSignInWithEmailLink(auth, url); |
| 28 | + |
26 | 29 | if (signInEmail && isSignInLink) { |
27 | 30 | const user: AuthenticatedUser = await authAPIClient.loginWithSignInLink( |
28 | 31 | url, |
29 | 32 | signInEmail, |
30 | 33 | ); |
31 | | - setAuthenticatedUser(user); |
| 34 | + if (user) { |
| 35 | + setAuthenticatedUser(user); |
| 36 | + setRedirectTo(CREATE_PASSWORD_PAGE); |
| 37 | + } else { |
| 38 | + setStatus("error"); |
| 39 | + } |
| 40 | + } else { |
| 41 | + setStatus("default"); |
32 | 42 | } |
| 43 | + }; |
| 44 | + |
| 45 | + if (authenticatedUser) { |
| 46 | + setRedirectTo(HOME_PAGE); |
| 47 | + } else { |
| 48 | + checkIfSignInLink(); |
33 | 49 | } |
34 | | - // alert: user is already logged in, please log out before trying again |
35 | | - }; |
| 50 | + }, [authenticatedUser, setAuthenticatedUser]); |
36 | 51 |
|
37 | | - if (authenticatedUser) { |
38 | | - return <Redirect to={HOME_PAGE} />; |
| 52 | + if (redirectTo) { |
| 53 | + return <Redirect to={redirectTo} />; |
39 | 54 | } |
40 | 55 |
|
41 | | - if (!didInit) { |
42 | | - didInit = true; |
43 | | - checkIfSignInLink(); |
44 | | - } |
| 56 | + const onLogInClick = async () => { |
| 57 | + const user: AuthenticatedUser = await authAPIClient.login(email, password); |
| 58 | + setAuthenticatedUser(user); |
| 59 | + }; |
45 | 60 |
|
46 | 61 | return ( |
47 | | - <div style={{ textAlign: "center" }}> |
48 | | - <h1>Login</h1> |
49 | | - <form> |
50 | | - <div> |
51 | | - <input |
52 | | - type="email" |
53 | | - value={email} |
54 | | - onChange={(event) => setEmail(event.target.value)} |
55 | | - placeholder="username@domain.com" |
56 | | - /> |
57 | | - </div> |
58 | | - <div> |
59 | | - <input |
60 | | - type="password" |
61 | | - value={password} |
62 | | - onChange={(event) => setPassword(event.target.value)} |
63 | | - placeholder="password" |
64 | | - /> |
65 | | - </div> |
66 | | - <div> |
67 | | - <button |
68 | | - className="btn btn-primary" |
69 | | - type="button" |
70 | | - onClick={onLogInClick} |
71 | | - > |
72 | | - Log In |
73 | | - </button> |
| 62 | + <> |
| 63 | + {status === "loading" && ( |
| 64 | + <Flex |
| 65 | + maxWidth="100vw" |
| 66 | + height="100vh" |
| 67 | + position="relative" |
| 68 | + backgroundRepeat="no-repeat" |
| 69 | + backgroundPosition="center" |
| 70 | + backgroundSize="cover" |
| 71 | + sx={{ |
| 72 | + "@media (orientation: landscape)": { |
| 73 | + height: "auto", |
| 74 | + minHeight: "100vh", |
| 75 | + overflowY: "auto", |
| 76 | + }, |
| 77 | + }} |
| 78 | + > |
| 79 | + <ResponsiveModalWindow> |
| 80 | + <Text color="#2C5282" textAlign="center"> |
| 81 | + Loading, please wait... |
| 82 | + </Text> |
| 83 | + </ResponsiveModalWindow> |
| 84 | + </Flex> |
| 85 | + )} |
| 86 | + |
| 87 | + {status === "error" && ( |
| 88 | + <Flex |
| 89 | + maxWidth="100vw" |
| 90 | + height="100vh" |
| 91 | + position="relative" |
| 92 | + backgroundRepeat="no-repeat" |
| 93 | + backgroundPosition="center" |
| 94 | + backgroundSize="cover" |
| 95 | + sx={{ |
| 96 | + "@media (orientation: landscape)": { |
| 97 | + height: "auto", |
| 98 | + minHeight: "100vh", |
| 99 | + overflowY: "auto", |
| 100 | + }, |
| 101 | + }} |
| 102 | + > |
| 103 | + <ResponsiveModalWindow> |
| 104 | + <Text color="red.500" textAlign="center"> |
| 105 | + An error occurred. If your link is expired, ask an adminstrator |
| 106 | + for assistance. |
| 107 | + </Text> |
| 108 | + </ResponsiveModalWindow> |
| 109 | + </Flex> |
| 110 | + )} |
| 111 | + |
| 112 | + {status === "default" && !redirectTo && ( |
| 113 | + <div style={{ textAlign: "center" }}> |
| 114 | + <h1>Login</h1> |
| 115 | + <form> |
| 116 | + <div> |
| 117 | + <input |
| 118 | + type="email" |
| 119 | + value={email} |
| 120 | + onChange={(event) => setEmail(event.target.value)} |
| 121 | + placeholder="username@domain.com" |
| 122 | + /> |
| 123 | + </div> |
| 124 | + <div> |
| 125 | + <input |
| 126 | + type="password" |
| 127 | + value={password} |
| 128 | + onChange={(event) => setPassword(event.target.value)} |
| 129 | + placeholder="password" |
| 130 | + /> |
| 131 | + </div> |
| 132 | + <div> |
| 133 | + <button |
| 134 | + className="btn btn-primary" |
| 135 | + type="button" |
| 136 | + onClick={onLogInClick} |
| 137 | + > |
| 138 | + Log In |
| 139 | + </button> |
| 140 | + </div> |
| 141 | + </form> |
74 | 142 | </div> |
75 | | - </form> |
76 | | - </div> |
| 143 | + )} |
| 144 | + </> |
77 | 145 | ); |
78 | 146 | }; |
79 | 147 |
|
|
0 commit comments