Add email to user registration/login flows
parent
1ac9f7ae1e
commit
49a5a90bd2
|
|
@ -21,16 +21,21 @@ interface AccessResponse {
|
||||||
jwtToken: string;
|
jwtToken: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const login = async (name: string, password: string) => {
|
export const login = async (email: string, password: string) => {
|
||||||
const response = (await axios.post("/access/login", {
|
const response = (await axios.post("/access/login", {
|
||||||
name,
|
email,
|
||||||
password,
|
password,
|
||||||
})) as AxiosResponse<AccessResponse>;
|
})) as AxiosResponse<AccessResponse>;
|
||||||
setJwtToken(response.data.jwtToken);
|
setJwtToken(response.data.jwtToken);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const registerUser = async (name: string, password: string) => {
|
export const registerUser = async (
|
||||||
|
email: string,
|
||||||
|
name: string,
|
||||||
|
password: string,
|
||||||
|
) => {
|
||||||
const response = (await axios.post("/access/register", {
|
const response = (await axios.post("/access/register", {
|
||||||
|
email,
|
||||||
name,
|
name,
|
||||||
password,
|
password,
|
||||||
})) as AxiosResponse<AccessResponse>;
|
})) as AxiosResponse<AccessResponse>;
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,15 @@
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { login } from "../../api/client";
|
import { login } from "../../api/client";
|
||||||
import { useNavigate } from "react-router";
|
import { useNavigate } from "react-router";
|
||||||
import {
|
import { Button, Grid, TextField, Typography } from "@mui/material";
|
||||||
Button,
|
|
||||||
Checkbox,
|
|
||||||
FormControlLabel,
|
|
||||||
Grid,
|
|
||||||
TextField,
|
|
||||||
Typography,
|
|
||||||
} from "@mui/material";
|
|
||||||
|
|
||||||
export const Login = () => {
|
export const Login = () => {
|
||||||
const [userName, setUserName] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const [rememberMe, setRememberMe] = useState(false);
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const handleLoginFlow = () =>
|
const handleLoginFlow = () =>
|
||||||
login(userName, password).then(() => {
|
login(email, password).then(() => {
|
||||||
navigate("/signup");
|
navigate("/signup");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -25,9 +17,9 @@ export const Login = () => {
|
||||||
<Grid container direction="column" spacing={2} alignItems="center">
|
<Grid container direction="column" spacing={2} alignItems="center">
|
||||||
<Typography variant="h3">Login</Typography>
|
<Typography variant="h3">Login</Typography>
|
||||||
<TextField
|
<TextField
|
||||||
value={userName}
|
value={email}
|
||||||
onChange={(e) => setUserName(e.target.value)}
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
placeholder="User"
|
placeholder="Email"
|
||||||
/>
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
type="password"
|
type="password"
|
||||||
|
|
@ -35,15 +27,6 @@ export const Login = () => {
|
||||||
onChange={(e) => setPassword(e.target.value)}
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
placeholder="Password"
|
placeholder="Password"
|
||||||
/>
|
/>
|
||||||
<FormControlLabel
|
|
||||||
control={
|
|
||||||
<Checkbox
|
|
||||||
checked={rememberMe}
|
|
||||||
onChange={() => setRememberMe(!rememberMe)}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
label="Remember me"
|
|
||||||
/>
|
|
||||||
<Button
|
<Button
|
||||||
onClick={handleLoginFlow}
|
onClick={handleLoginFlow}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import {
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
|
|
||||||
export const Register = () => {
|
export const Register = () => {
|
||||||
|
const [email, setEmail] = useState("");
|
||||||
const [userName, setUserName] = useState("");
|
const [userName, setUserName] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const [passwordConfirm, setPasswordConfirm] = useState("");
|
const [passwordConfirm, setPasswordConfirm] = useState("");
|
||||||
|
|
@ -48,8 +49,22 @@ export const Register = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const emailSchema = zod.email();
|
||||||
|
let emailErrors: string[] = [];
|
||||||
|
try {
|
||||||
|
emailSchema.parse(email);
|
||||||
|
} catch (e: unknown) {
|
||||||
|
if (e instanceof zod.ZodError) {
|
||||||
|
emailErrors = e.issues.map((issue) => issue.message);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const allErrors = [...emailErrors, ...passwordErrors];
|
||||||
|
|
||||||
const handleRegisterFlow = () =>
|
const handleRegisterFlow = () =>
|
||||||
registerUser(userName, password).then(() => {
|
registerUser(email, userName, password).then(() => {
|
||||||
navigate("/signup");
|
navigate("/signup");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -57,6 +72,11 @@ export const Register = () => {
|
||||||
<>
|
<>
|
||||||
<Grid container direction="column" spacing={2} alignItems="center">
|
<Grid container direction="column" spacing={2} alignItems="center">
|
||||||
<Typography variant="h3">Register</Typography>
|
<Typography variant="h3">Register</Typography>
|
||||||
|
<TextField
|
||||||
|
value={email}
|
||||||
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
|
placeholder="Email"
|
||||||
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
value={userName}
|
value={userName}
|
||||||
onChange={(e) => setUserName(e.target.value)}
|
onChange={(e) => setUserName(e.target.value)}
|
||||||
|
|
@ -75,7 +95,7 @@ export const Register = () => {
|
||||||
placeholder="Confirm password"
|
placeholder="Confirm password"
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
disabled={passwordErrors.length !== 0}
|
disabled={allErrors.length !== 0}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
sx={{ width: "100%" }}
|
sx={{ width: "100%" }}
|
||||||
|
|
@ -92,7 +112,7 @@ export const Register = () => {
|
||||||
Login
|
Login
|
||||||
</Button>
|
</Button>
|
||||||
</Grid>
|
</Grid>
|
||||||
{!!passwordErrors.length && (
|
{!!allErrors.length && (
|
||||||
<Paper
|
<Paper
|
||||||
elevation={2}
|
elevation={2}
|
||||||
sx={{
|
sx={{
|
||||||
|
|
@ -104,7 +124,7 @@ export const Register = () => {
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Grid container direction="column" spacing={2}>
|
<Grid container direction="column" spacing={2}>
|
||||||
{passwordErrors.map((error) => (
|
{allErrors.map((error) => (
|
||||||
<Alert severity="error">{error}</Alert>
|
<Alert severity="error">{error}</Alert>
|
||||||
))}
|
))}
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue