|
| 1 | +import { useState } from "react" |
| 2 | +import { useRouter } from "next/router" |
| 3 | + |
| 4 | +import { |
| 5 | + Box, |
| 6 | + TextField, |
| 7 | + Button, |
| 8 | + Typography, |
| 9 | + CircularProgress, |
| 10 | +} from "@mui/material" |
| 11 | +import { styled } from "@mui/material/styles" |
| 12 | + |
| 13 | + |
| 14 | +const Form = styled("form")({ |
| 15 | + display: "flex", |
| 16 | + flexDirection: "column", |
| 17 | + justifyContent: "center", |
| 18 | + alignItems: "center", |
| 19 | +}) |
| 20 | + |
| 21 | +const FormControl = styled(Box)({ |
| 22 | + margin: 8, |
| 23 | + display: "flex", |
| 24 | + flexDirection: "column", |
| 25 | + justifyContent: "center", |
| 26 | + alignItems: "center", |
| 27 | + width: "320px", |
| 28 | +}) as typeof Box |
| 29 | + |
| 30 | +const FormActions = styled(Box)({ |
| 31 | + margin: 16, |
| 32 | + width: "100%", |
| 33 | + display: "flex", |
| 34 | + justifyContent: "space-evenly", |
| 35 | + alignItems: "center", |
| 36 | +}) as typeof Box |
| 37 | + |
| 38 | +const SignIn = () => { |
| 39 | + const router = useRouter() |
| 40 | + const [email, setEmail] = useState("") |
| 41 | + const [password, setPassword] = useState("") |
| 42 | + const [isLoading, setIsLoading] = useState(false) |
| 43 | + const [success, setSuccess] = useState(false) |
| 44 | + const [errorCode, setErrorCode] = useState<string | null>(null) |
| 45 | + |
| 46 | + const handleErrorResponse = (res: Response) => { |
| 47 | + res |
| 48 | + .json() |
| 49 | + .then((data) => { |
| 50 | + if (data && data.errorCode) { |
| 51 | + setErrorCode(data.errorCode) |
| 52 | + } |
| 53 | + }) |
| 54 | + .catch((err) => { |
| 55 | + console.log("failed to parse error response", err) |
| 56 | + setErrorCode("failed/unknown") |
| 57 | + }) |
| 58 | + console.error("failed to sign in", { |
| 59 | + statusText: res.statusText, |
| 60 | + status: res.status, |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + const handleSubmit = async (e: any) => { |
| 65 | + e && e.preventDefault() |
| 66 | + setIsLoading(true) |
| 67 | + setErrorCode(null) |
| 68 | + console.log("signing in user...") |
| 69 | + |
| 70 | + fetch("/api/auth/signin", { |
| 71 | + method: "POST", |
| 72 | + headers: { |
| 73 | + "Content-Type": "application/json", |
| 74 | + }, |
| 75 | + body: JSON.stringify({ |
| 76 | + email, |
| 77 | + password, |
| 78 | + }), |
| 79 | + }) |
| 80 | + .then((res) => { |
| 81 | + if (res.ok) { |
| 82 | + console.log("successful sign in") |
| 83 | + setSuccess(true) |
| 84 | + router.push("/") |
| 85 | + return |
| 86 | + } |
| 87 | + handleErrorResponse(res) |
| 88 | + }) |
| 89 | + .catch((err) => { |
| 90 | + console.error("failed to sign in", err) |
| 91 | + setErrorCode("failed/unknown") |
| 92 | + }) |
| 93 | + .finally(() => { |
| 94 | + setPassword("") |
| 95 | + setIsLoading(false) |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + const findErrorText = () => { |
| 100 | + switch (errorCode) { |
| 101 | + case "auth/wrong-password": |
| 102 | + return "Invalid password. Please try again." |
| 103 | + default: |
| 104 | + return "Failed to sign in." |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return ( |
| 109 | + <> |
| 110 | + <Typography variant="h4" component="h1"> |
| 111 | + Sign In |
| 112 | + </Typography> |
| 113 | + <Form onSubmit={handleSubmit}> |
| 114 | + <FormControl> |
| 115 | + <TextField |
| 116 | + label="Email" |
| 117 | + value={email} |
| 118 | + type="email" |
| 119 | + |
| 120 | + onChange={(e) => setEmail(e.target.value)} |
| 121 | + /> |
| 122 | + </FormControl> |
| 123 | + <FormControl> |
| 124 | + <TextField |
| 125 | + label="Password" |
| 126 | + value={password} |
| 127 | + type="password" |
| 128 | + autoComplete="current-password" |
| 129 | + onChange={(e) => setPassword(e.target.value)} |
| 130 | + /> |
| 131 | + </FormControl> |
| 132 | + {isLoading && <CircularProgress />} |
| 133 | + {!success && !isLoading && ( |
| 134 | + <FormActions> |
| 135 | + <Button type="submit" color="primary"> |
| 136 | + Sign In |
| 137 | + </Button> |
| 138 | + </FormActions> |
| 139 | + )} |
| 140 | + </Form> |
| 141 | + {success && ( |
| 142 | + <Typography color="primary">Success! Redirecting now...</Typography> |
| 143 | + )} |
| 144 | + {errorCode && <Typography color="error">{findErrorText()}</Typography>} |
| 145 | + </> |
| 146 | + ) |
| 147 | +} |
| 148 | + |
| 149 | +export default SignIn |
0 commit comments