Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | 1x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 8x 8x 8x 2x 2x 6x 6x 6x 5x 8x 8x 1x 4x 4x 4x 2x 6x 135x 1x 78x 30x 2x | import React, { useState } from "react";
import { useNavigate, useLocation, Link } from "react-router-dom";
import { toast } from "react-toastify";
import {
Container,
Paper,
Typography,
Box,
TextField,
Button,
Divider,
InputAdornment,
IconButton,
Alert,
CircularProgress,
} from "@mui/material";
import LockPersonIcon from "@mui/icons-material/LockPerson";
import VisibilityIcon from "@mui/icons-material/Visibility";
import VisibilityOffIcon from "@mui/icons-material/VisibilityOff";
import LoginIcon from "@mui/icons-material/Login";
import { useAuth } from "../contexts/AuthContext";
import { loginUser } from "../db/api";
const Login = () => {
const navigate = useNavigate();
const location = useLocation();
const { login } = useAuth();
// Where to redirect after successful login (supports ?next=/some-path)
const params = new URLSearchParams(location.search);
const nextPath = params.get("next") || "/minha-conta";
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [showPassword, setShowPassword] = useState(false);
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
/**
* Handles the login form submission.
*
* Looks up the email in the database via {@link getUserByEmail}, then
* compares the provided password against the stored value. On success,
* calls {@link AuthContext.login} to persist the session and redirects
* the user to `nextPath` (from the `?next=` query parameter, defaulting
* to `'/'`).
*
* @param {React.FormEvent<HTMLFormElement>} e
* @returns {Promise<void>}
*/
const handleSubmit = async (e) => {
e.preventDefault();
setError("");
if (!email.trim() || !password) {
setError("Preencha e-mail e senha.");
return;
}
setLoading(true);
try {
const loginResponse = await loginUser({ email: email.trim().toLowerCase(), password });
const dbUser = loginResponse.user ?? loginResponse;
const accessToken = loginResponse.accessToken;
if (!accessToken) {
throw new Error("Resposta de autenticação inválida: token não recebido.");
}
login({
user: {
id: dbUser.id,
name: dbUser.first_name,
lastName: dbUser.last_name,
email: dbUser.email,
personType: dbUser.person_type,
},
accessToken,
});
toast.success(`Bem-vindo(a) de volta, ${dbUser.first_name}! 👋`);
navigate(nextPath);
} catch (err) {
setError(err.message || 'Credenciais inválidas. Tente novamente.');
} finally {
setLoading(false);
}
};
return (
<Box
id="login-page-wrapper"
sx={{
minHeight: "100vh",
backgroundColor: "#eaeded",
py: 4,
px: 2,
display: "flex",
alignItems: "flex-start",
justifyContent: "center",
}}
>
<Container maxWidth="xs">
{/* Card */}
<Paper
id="login-card"
elevation={2}
sx={{
border: "1px solid #D5D9D9",
borderRadius: 3,
overflow: "hidden",
}}
>
{/* Header strip */}
<Box
id="login-header"
sx={{
background: "linear-gradient(135deg, #131921 0%, #37475A 100%)",
px: 4,
py: 3,
display: "flex",
alignItems: "center",
gap: 1.5,
}}
>
<LockPersonIcon sx={{ color: "#ff9900", fontSize: 32 }} />
<Box>
<Typography variant="h5" fontWeight={700} color="#fff">
Entrar
</Typography>
<Typography sx={{ color: "#ccc", fontSize: "0.8rem" }}>
tester<span style={{ color: "#ff9900" }}>.com</span>
</Typography>
</Box>
</Box>
{/* Form */}
<Box
id="login-form-body"
component="form"
onSubmit={handleSubmit}
sx={{ px: 4, py: 3 }}
>
{error && (
<Alert
id="login-error-alert"
severity="error"
sx={{ mb: 2 }}
onClose={() => setError("")}
>
{error}
</Alert>
)}
<TextField
id="login-email"
label="E-mail"
fullWidth
size="small"
type="email"
autoComplete="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
sx={{ mb: 2 }}
/>
<TextField
id="login-password"
label="Senha"
fullWidth
size="small"
type={showPassword ? "text" : "password"}
autoComplete="current-password"
value={password}
onChange={(e) => setPassword(e.target.value)}
sx={{ mb: 0.5 }}
InputProps={{
endAdornment: (
<InputAdornment position="end">
<IconButton
size="small"
onClick={() => setShowPassword((v) => !v)}
>
{showPassword ? (
<VisibilityOffIcon fontSize="small" />
) : (
<VisibilityIcon fontSize="small" />
)}
</IconButton>
</InputAdornment>
),
}}
/>
<Typography
id="login-forgot-password"
variant="caption"
sx={{
color: "#0066c0",
cursor: "pointer",
display: "block",
mb: 2.5,
"&:hover": { textDecoration: "underline" },
}}
>
Esqueceu a senha?
</Typography>
<Button
id="login-submit-btn"
type="submit"
variant="contained"
fullWidth
size="large"
disabled={loading}
startIcon={
loading ? (
<CircularProgress size={18} sx={{ color: "#0F1111" }} />
) : (
<LoginIcon />
)
}
sx={{
backgroundColor: "#FFD814",
color: "#0F1111",
fontWeight: 700,
border: "1px solid #FCD200",
textTransform: "none",
"&:hover": { backgroundColor: "#F7CA00" },
boxShadow: "0 2px 5px 0 rgba(213,217,217,.5)",
mb: 1,
}}
>
{loading ? "Entrando..." : "Entrar"}
</Button>
<Typography variant="caption" color="text.secondary" display="block" textAlign="center">
Ao entrar, você concorda com os{" "}
<Box
component="span"
sx={{ color: "#0066c0", cursor: "pointer", "&:hover": { textDecoration: "underline" } }}
>
Termos de Uso
</Box>
.
</Typography>
<Divider sx={{ my: 2.5 }}>
<Typography variant="caption" color="text.secondary">
Novo no amazonQA.com?
</Typography>
</Divider>
<Button
id="login-create-account-btn"
component={Link}
to="/register"
variant="outlined"
fullWidth
size="large"
sx={{
borderColor: "#D5D9D9",
color: "#131921",
fontWeight: 600,
textTransform: "none",
"&:hover": { borderColor: "#131921", backgroundColor: "#f7f7f7" },
}}
>
Criar sua conta
</Button>
</Box>
</Paper>
</Container>
</Box>
);
};
export default Login;
|