Skip to content

Commit 7e78426

Browse files
authored
Merge pull request #55 from uwblueprint/F24/trinity/update-login-page
F24/trinity/update-login-page
2 parents 8bc4b03 + 4cbcc00 commit 7e78426

File tree

2 files changed

+121
-49
lines changed

2 files changed

+121
-49
lines changed

frontend/src/components/auth/Login.tsx

Lines changed: 116 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,147 @@
1-
import React, { useContext, useState } from "react";
1+
import React, { useContext, useState, useEffect } from "react";
2+
import { Text, Flex } from "@chakra-ui/react";
23
import { Redirect } from "react-router-dom";
34
import { isSignInWithEmailLink } from "firebase/auth";
45
import auth from "../../firebase/firebase";
56
import authAPIClient from "../../APIClients/AuthAPIClient";
6-
import { HOME_PAGE } from "../../constants/Routes";
7+
import { CREATE_PASSWORD_PAGE, HOME_PAGE } from "../../constants/Routes";
78
import AuthContext from "../../contexts/AuthContext";
89
import { AuthenticatedUser } from "../../types/AuthTypes";
9-
10-
let didInit = false;
10+
import ResponsiveModalWindow from "../common/responsive/ResponsiveModalWindow";
1111

1212
const Login = (): React.ReactElement => {
1313
const { authenticatedUser, setAuthenticatedUser } = useContext(AuthContext);
1414
const [email, setEmail] = useState("");
1515
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 () => {
2224
const url = window.location.href;
2325
const urlSearchParams = new URLSearchParams(window.location.search);
2426
const signInEmail = urlSearchParams.get("email"); // passed in from actionCode
2527
const isSignInLink = isSignInWithEmailLink(auth, url);
28+
2629
if (signInEmail && isSignInLink) {
2730
const user: AuthenticatedUser = await authAPIClient.loginWithSignInLink(
2831
url,
2932
signInEmail,
3033
);
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");
3242
}
43+
};
44+
45+
if (authenticatedUser) {
46+
setRedirectTo(HOME_PAGE);
47+
} else {
48+
checkIfSignInLink();
3349
}
34-
// alert: user is already logged in, please log out before trying again
35-
};
50+
}, [authenticatedUser, setAuthenticatedUser]);
3651

37-
if (authenticatedUser) {
38-
return <Redirect to={HOME_PAGE} />;
52+
if (redirectTo) {
53+
return <Redirect to={redirectTo} />;
3954
}
4055

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+
};
4560

4661
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>
74142
</div>
75-
</form>
76-
</div>
143+
)}
144+
</>
77145
);
78146
};
79147

frontend/src/components/pages/CreatePasswordPage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import {
1010
FormLabel,
1111
FormControl,
1212
} from "@chakra-ui/react";
13+
import { useHistory } from "react-router-dom";
1314
import ResponsiveLogo from "../common/responsive/ResponsiveAuthPageLogo";
1415
import ResponsivePasswordInput from "../common/responsive/ResponsivePasswordInput";
1516
import ResponsiveAuthContainer from "../common/responsive/ResponsiveAuthContainer";
1617
import ResponsiveModalWindow from "../common/responsive/ResponsiveModalWindow";
1718
import background from "../assets/background.png";
1819
import backgroundMobile from "../assets/background_mobile.png";
1920
import AuthAPIClient from "../../APIClients/AuthAPIClient";
21+
import { HOME_PAGE } from "../../constants/Routes";
2022

2123
const CreatePasswordPage = (): React.ReactElement => {
2224
const [showModal, setShowModal] = React.useState(false);
@@ -25,6 +27,8 @@ const CreatePasswordPage = (): React.ReactElement => {
2527
const [errorMessage, setErrorMessage] = React.useState("");
2628
const [email, setEmail] = React.useState("Email not found.");
2729

30+
const history = useHistory();
31+
2832
React.useEffect(() => {
2933
const getEmail = async () => {
3034
const userEmail = await AuthAPIClient.getEmailOfCurrentUser();
@@ -73,7 +77,7 @@ const CreatePasswordPage = (): React.ReactElement => {
7377
};
7478

7579
const handleGetStarted = () => {
76-
// TODO: Navigate to main page
80+
history.push(HOME_PAGE);
7781
};
7882

7983
return (

0 commit comments

Comments
 (0)