fix signup page
This commit is contained in:
parent
68384d7e1d
commit
5d9cf51c10
@ -5,9 +5,9 @@ import { ApiContext } from './utils/api_context';
|
|||||||
import { AuthContext } from './utils/auth_context';
|
import { AuthContext } from './utils/auth_context';
|
||||||
import { useApi } from './utils/use_api';
|
import { useApi } from './utils/use_api';
|
||||||
import { useJwtRefresh } from './utils/use_jwt_refresh';
|
import { useJwtRefresh } from './utils/use_jwt_refresh';
|
||||||
import './app.css';
|
|
||||||
import { RolesContext } from './utils/roles_context';
|
import { RolesContext } from './utils/roles_context';
|
||||||
import { parseJwt } from './utils/parse_jwt';
|
import { parseJwt } from './utils/parse_jwt';
|
||||||
|
import './app.css';
|
||||||
|
|
||||||
export const App = () => {
|
export const App = () => {
|
||||||
const [authToken, setAuthToken] = useState(null);
|
const [authToken, setAuthToken] = useState(null);
|
||||||
@ -29,7 +29,6 @@ export const App = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const jwtPayload = parseJwt(authToken);
|
const jwtPayload = parseJwt(authToken);
|
||||||
console.log(jwtPayload);
|
|
||||||
|
|
||||||
// don't display anything while trying to get user token
|
// don't display anything while trying to get user token
|
||||||
// can display a loading screen here if desired
|
// can display a loading screen here if desired
|
||||||
|
@ -1,18 +1,21 @@
|
|||||||
import { useContext, useState } from 'react';
|
import { useContext, useState } from 'react';
|
||||||
import { useNavigate } from 'react-router';
|
import { useNavigate } from 'react-router';
|
||||||
import { AuthContext } from '../../utils/auth_context';
|
import { AuthContext } from '../../utils/auth_context';
|
||||||
|
import { ApiContext } from '../../utils/api_context';
|
||||||
import { Paper } from '../common/paper';
|
import { Paper } from '../common/paper';
|
||||||
import { Input } from '../common/input';
|
import { Input } from '../common/input';
|
||||||
import { Button } from '../common/button';
|
import { Button } from '../common/button';
|
||||||
|
|
||||||
export const SignUp = () => {
|
export const SignUp = () => {
|
||||||
const [, setAuthToken] = useContext(AuthContext);
|
const [, setAuthToken] = useContext(AuthContext);
|
||||||
|
const api = useContext(ApiContext);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [name, setName] = useState('');
|
const [firstName, setFirstName] = useState('');
|
||||||
|
const [lastName, setLastName] = useState('');
|
||||||
const [email, setEmail] = useState('');
|
const [email, setEmail] = useState('');
|
||||||
const [emailConfirmation, setEmailConfirmation] = useState('');
|
const [emailConfirmation, setEmailConfirmation] = useState('');
|
||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
const [passwordConfiramation, setPasswordConfirmation] = useState('');
|
const [passwordConfirmation, setPasswordConfirmation] = useState('');
|
||||||
const [errorMessage, setErrorMessage] = useState('');
|
const [errorMessage, setErrorMessage] = useState('');
|
||||||
|
|
||||||
const signUp = async () => {
|
const signUp = async () => {
|
||||||
@ -28,38 +31,37 @@ export const SignUp = () => {
|
|||||||
setErrorMessage('Password cannot be blank');
|
setErrorMessage('Password cannot be blank');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (password !== passwordConfiramation) {
|
if (password !== passwordConfirmation) {
|
||||||
setErrorMessage('Password does not match');
|
setErrorMessage('Password does not match');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (name === '') {
|
if (firstName === '') {
|
||||||
setErrorMessage('Name cannot be blank.');
|
setErrorMessage('First name cannot be blank.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const res = await fetch('/users', {
|
if (lastName === '') {
|
||||||
method: 'POST',
|
setErrorMessage('Last name cannot be blank.');
|
||||||
headers: {
|
return;
|
||||||
'Content-Type': 'application/json',
|
}
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
const { token } = await api.post('/users', {
|
||||||
name,
|
firstName,
|
||||||
|
lastName,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
if (res.status === 201) {
|
setAuthToken(token);
|
||||||
const result = await res.json();
|
|
||||||
setAuthToken(result.token);
|
|
||||||
navigate('/');
|
navigate('/');
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-row justify-center m-4">
|
<div className="flex flex-row justify-center m-4">
|
||||||
<div className="w-96">
|
<div className="w-96">
|
||||||
<Paper>
|
<Paper>
|
||||||
<div>Name</div>
|
<div>First Name</div>
|
||||||
<Input type="text" value={name} onChange={(e) => setName(e.target.value)} />
|
<Input type="text" value={firstName} onChange={(e) => setFirstName(e.target.value)} />
|
||||||
|
<div>Last Name</div>
|
||||||
|
<Input type="text" value={lastName} onChange={(e) => setLastName(e.target.value)} />
|
||||||
<div>Email</div>
|
<div>Email</div>
|
||||||
<Input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
|
<Input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
|
||||||
<div>Confirm Email</div>
|
<div>Confirm Email</div>
|
||||||
@ -69,7 +71,7 @@ export const SignUp = () => {
|
|||||||
<div>Confirm Password</div>
|
<div>Confirm Password</div>
|
||||||
<Input
|
<Input
|
||||||
type="password"
|
type="password"
|
||||||
value={passwordConfiramation}
|
value={passwordConfirmation}
|
||||||
onChange={(e) => setPasswordConfirmation(e.target.value)}
|
onChange={(e) => setPasswordConfirmation(e.target.value)}
|
||||||
/>
|
/>
|
||||||
<div className="flex flex-row justify-end mt-2">
|
<div className="flex flex-row justify-end mt-2">
|
||||||
|
Loading…
Reference in New Issue
Block a user