|
1 | | - |
2 | 1 | "use client" |
3 | 2 |
|
4 | | -import { Box, Button, Grid, Skeleton, TextField, Typography } from "@mui/material"; |
| 3 | +import {Box, Button, Grid, Skeleton, TextField, Typography} from "@mui/material"; |
5 | 4 | import CircularProgress from '@mui/material/CircularProgress'; |
6 | | -import { useState } from "react"; |
7 | | -import { cybTheme } from "./../../../components/themeCYB"; |
8 | | -import { signIn, useSession } from "next-auth/react"; |
| 5 | +import {useEffect, useState} from "react"; |
| 6 | +import {cybTheme} from "./../../../components/themeCYB"; |
| 7 | +import {signIn, useSession} from "next-auth/react"; |
9 | 8 | import Link from "next/link"; |
10 | | -import { useRouter } from "next/navigation"; |
11 | | -import { normalizeEmail } from "./../../../components/Login/authUtil"; |
| 9 | +import {useRouter} from "next/navigation"; |
| 10 | +import {normalizeEmail} from "./../../../components/Login/authUtil"; |
12 | 11 | import SnackbarAlert from "@/app/components/feedback/snackbarAlert"; |
13 | 12 |
|
14 | 13 |
|
15 | 14 | export default function SignInPage() { |
16 | | - |
17 | | - const [email, setEmail] = useState("") |
18 | | - const [response, setResponse] = useState("") |
19 | | - const [error, setError] = useState(false) |
20 | | - const [loading, setLoading] = useState(false) |
21 | | - const [snackbarOpen, setSnackbarOpen] = useState(false); |
22 | | - const [severity, setSeverity] = useState("") |
23 | | - |
24 | | - const session = useSession() |
25 | | - const router = useRouter() |
26 | | - |
27 | | - if (session.status == "authenticated") { |
28 | | - router.push("/"); |
29 | | - return; |
30 | | - } |
31 | | - |
32 | | - const handleLogin = async () => { |
33 | | - setLoading(true); |
34 | | - |
35 | | - if (email == "") { |
36 | | - setError(true) |
37 | | - setSeverity("error") |
38 | | - setResponse("Please fill in your email") |
39 | | - setLoading(false); |
40 | | - setSnackbarOpen(true); |
41 | | - return |
42 | | - } |
43 | | - |
44 | | - const normalizedEmail = normalizeEmail(email); |
45 | | - |
46 | | - const response = await signIn("email", { |
47 | | - email: normalizedEmail, |
48 | | - redirect: false, |
| 15 | + |
| 16 | + const [email, setEmail] = useState("") |
| 17 | + const [response, setResponse] = useState("") |
| 18 | + const [error, setError] = useState(false) |
| 19 | + const [loading, setLoading] = useState(false) |
| 20 | + const [snackbarOpen, setSnackbarOpen] = useState(false); |
| 21 | + const [severity, setSeverity] = useState("") |
| 22 | + |
| 23 | + const session = useSession() |
| 24 | + const router = useRouter() |
| 25 | + |
| 26 | + // wrap in useEffect because i don't know |
| 27 | + useEffect(() => { |
| 28 | + if (session.status === "authenticated") { |
| 29 | + router.push("/"); |
| 30 | + } |
49 | 31 | }); |
50 | | - |
51 | | - if (response.error == null) { |
52 | | - setError(false) |
53 | | - setSeverity("success") |
54 | | - setResponse(`Email sent to ${normalizedEmail}`); |
55 | | - setLoading(false); |
56 | | - setSnackbarOpen(true); |
57 | | - } else { |
58 | | - setError(true); |
59 | | - setSeverity("error") |
60 | | - setResponse(response.error); |
61 | | - setLoading(false); |
62 | | - setSnackbarOpen(true); |
| 32 | + |
| 33 | + |
| 34 | + const handleLogin = async () => { |
| 35 | + setLoading(true); |
| 36 | + |
| 37 | + if (email == "") { |
| 38 | + setError(true) |
| 39 | + setSeverity("error") |
| 40 | + setResponse("Please fill in your email") |
| 41 | + setLoading(false); |
| 42 | + setSnackbarOpen(true); |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + const normalizedEmail = normalizeEmail(email); |
| 47 | + |
| 48 | + const response = await signIn("email", { |
| 49 | + email: normalizedEmail, |
| 50 | + redirect: false, |
| 51 | + }); |
| 52 | + |
| 53 | + if (response.error == null) { |
| 54 | + setError(false) |
| 55 | + setSeverity("success") |
| 56 | + setResponse(`Email sent to ${normalizedEmail}`); |
| 57 | + setLoading(false); |
| 58 | + setSnackbarOpen(true); |
| 59 | + } else { |
| 60 | + setError(true); |
| 61 | + setSeverity("error") |
| 62 | + setResponse(response.error); |
| 63 | + setLoading(false); |
| 64 | + setSnackbarOpen(true); |
| 65 | + } |
63 | 66 | } |
64 | | - } |
65 | | - |
66 | | - return ( |
67 | | - <Box> |
68 | | - <Grid |
69 | | - container |
70 | | - spacing={2} |
71 | | - direction="column" |
72 | | - padding={4} |
73 | | - width={{ xs: "100vw", md: "30vw" }} |
74 | | - > |
75 | | - <Grid item> |
76 | | - <Typography variant="h6">Log in</Typography> |
77 | | - </Grid> |
78 | | - |
79 | | - <Grid item> |
80 | | - <TextField |
81 | | - required |
82 | | - fullWidth |
83 | | - variant="filled" |
84 | | - label="email" |
85 | | - type="email" |
86 | | - value={email} |
87 | | - error={error} |
88 | | - onChange={(event) => setEmail(event.target.value)} |
89 | | - InputLabelProps={{ shrink: true }} |
90 | | - onKeyUp={(e) => {if(e.key==="Enter") handleLogin()}} |
91 | | - /> |
92 | | - </Grid> |
93 | | - |
94 | | - <Grid item> |
95 | | - <Button |
96 | | - fullWidth |
97 | | - variant="contained" |
98 | | - onClick={() => handleLogin()} |
99 | | - disabled={loading} |
100 | | - > |
101 | | - Send magic link |
102 | | - {loading && ( |
103 | | - <CircularProgress |
104 | | - size={24} |
105 | | - sx={{ |
106 | | - position: 'absolute', |
107 | | - top: '50%', |
108 | | - left: '50%', |
109 | | - marginTop: '-12px', |
110 | | - marginLeft: '-12px', |
111 | | - }}/> |
112 | | - )} |
113 | | - </Button> |
114 | | - </Grid> |
115 | | - |
116 | | - <Grid |
117 | | - item |
118 | | - container |
119 | | - direction="row" |
120 | | - justifyContent="flex-end" |
121 | | - > |
122 | | - <Link |
123 | | - href="/auth/register" |
124 | | - passHref |
125 | | - style={{ textDecoration: "none", cursor: "pointer" }} |
126 | | - > |
127 | | - <Typography |
128 | | - variant="caption" |
129 | | - color={cybTheme.palette.primary.main} |
| 67 | + |
| 68 | + return ( |
| 69 | + <Box> |
| 70 | + <Grid |
| 71 | + container |
| 72 | + spacing={2} |
| 73 | + direction="column" |
| 74 | + padding={4} |
| 75 | + width={{xs: "100vw", md: "30vw"}} |
130 | 76 | > |
131 | | - Register new user |
132 | | - </Typography> |
133 | | - </Link> |
134 | | - </Grid> |
135 | | - <Grid item> |
136 | | - <Typography variant="caption"> |
137 | | - {response != "" ? ( |
138 | | - <SnackbarAlert |
139 | | - open={snackbarOpen} |
140 | | - setOpen={setSnackbarOpen} |
141 | | - response={response} |
142 | | - severity={severity} |
143 | | - /> |
144 | | - ) : ( |
145 | | - <Skeleton |
146 | | - animation={false} |
147 | | - variant="text" |
148 | | - sx={{ bgcolor: "inherit" }} |
149 | | - /> |
150 | | - )} |
151 | | - </Typography> |
152 | | - </Grid> |
153 | | - </Grid> |
154 | | - </Box> |
155 | | - ); |
| 77 | + <Grid item> |
| 78 | + <Typography variant="h6">Log in</Typography> |
| 79 | + </Grid> |
| 80 | + |
| 81 | + <Grid item> |
| 82 | + <TextField |
| 83 | + required |
| 84 | + fullWidth |
| 85 | + variant="filled" |
| 86 | + label="email" |
| 87 | + type="email" |
| 88 | + value={email} |
| 89 | + error={error} |
| 90 | + onChange={(event) => setEmail(event.target.value)} |
| 91 | + InputLabelProps={{shrink: true}} |
| 92 | + onKeyUp={(e) => { |
| 93 | + if (e.key === "Enter") handleLogin() |
| 94 | + }} |
| 95 | + /> |
| 96 | + </Grid> |
| 97 | + |
| 98 | + <Grid item> |
| 99 | + <Button |
| 100 | + fullWidth |
| 101 | + variant="contained" |
| 102 | + onClick={() => handleLogin()} |
| 103 | + disabled={loading} |
| 104 | + > |
| 105 | + Send magic link |
| 106 | + {loading && ( |
| 107 | + <CircularProgress |
| 108 | + size={24} |
| 109 | + sx={{ |
| 110 | + position: 'absolute', |
| 111 | + top: '50%', |
| 112 | + left: '50%', |
| 113 | + marginTop: '-12px', |
| 114 | + marginLeft: '-12px', |
| 115 | + }}/> |
| 116 | + )} |
| 117 | + </Button> |
| 118 | + </Grid> |
| 119 | + |
| 120 | + <Grid |
| 121 | + item |
| 122 | + container |
| 123 | + direction="row" |
| 124 | + justifyContent="flex-end" |
| 125 | + > |
| 126 | + <Link |
| 127 | + href="/auth/register" |
| 128 | + passHref |
| 129 | + style={{textDecoration: "none", cursor: "pointer"}} |
| 130 | + > |
| 131 | + <Typography |
| 132 | + variant="caption" |
| 133 | + color={cybTheme.palette.primary.main} |
| 134 | + > |
| 135 | + Register new user |
| 136 | + </Typography> |
| 137 | + </Link> |
| 138 | + </Grid> |
| 139 | + <Grid item> |
| 140 | + <Typography variant="caption"> |
| 141 | + {response != "" ? ( |
| 142 | + <SnackbarAlert |
| 143 | + open={snackbarOpen} |
| 144 | + setOpen={setSnackbarOpen} |
| 145 | + response={response} |
| 146 | + severity={severity} |
| 147 | + /> |
| 148 | + ) : ( |
| 149 | + <Skeleton |
| 150 | + animation={false} |
| 151 | + variant="text" |
| 152 | + sx={{bgcolor: "inherit"}} |
| 153 | + /> |
| 154 | + )} |
| 155 | + </Typography> |
| 156 | + </Grid> |
| 157 | + </Grid> |
| 158 | + </Box> |
| 159 | + ); |
156 | 160 | } |
0 commit comments