Skip to content

Commit 37a6d99

Browse files
committed
feat: signin/register copy, single page
1 parent bccd526 commit 37a6d99

File tree

4 files changed

+212
-163
lines changed

4 files changed

+212
-163
lines changed

pages/auth/register.tsx renamed to lib/controllers/Register.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import { useState } from "react"
22
import { useRouter } from "next/router"
3-
import NLink from "next/link"
43

54
import {
65
Box,
76
TextField,
87
Button,
98
Typography,
10-
Link,
119
CircularProgress,
1210
} from "@mui/material"
1311
import { styled } from "@mui/material/styles"
1412

15-
const BoxContainer = styled(Box)({
16-
width: "100%",
17-
height: "100vh",
18-
display: "flex",
19-
flexDirection: "column",
20-
justifyContent: "center",
21-
alignItems: "center",
22-
}) as typeof Box
2313

2414
const Form = styled("form")({
2515
display: "flex",
@@ -45,7 +35,7 @@ const FormActions = styled(Box)({
4535
alignItems: "center",
4636
}) as typeof Box
4737

48-
const SignIn = () => {
38+
const Register = () => {
4939
const router = useRouter()
5040
const [email, setEmail] = useState("")
5141
const [password, setPassword] = useState("")
@@ -125,9 +115,9 @@ const SignIn = () => {
125115
}
126116

127117
return (
128-
<BoxContainer>
118+
<>
129119
<Typography variant="h4" component="h1">
130-
Register
120+
Sign Up Today For Free
131121
</Typography>
132122
<Form onSubmit={handleSubmit}>
133123
<FormControl>
@@ -170,11 +160,8 @@ const SignIn = () => {
170160
<Typography color="primary">Success! Redirecting now...</Typography>
171161
)}
172162
{errorCode && <Typography color="error">{findErrorText()}</Typography>}
173-
<Box m={2}>
174-
<Link component={NLink} href="/auth/signin">Sign In</Link>
175-
</Box>
176-
</BoxContainer>
163+
</>
177164
)
178165
}
179166

180-
export default SignIn
167+
export default Register

lib/controllers/SignIn.tsx

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
placeholder="[email protected]"
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

Comments
 (0)