Skip to content

Commit c6534f3

Browse files
committed
updates to signup page
1 parent 1355209 commit c6534f3

File tree

5 files changed

+132
-113
lines changed

5 files changed

+132
-113
lines changed

server/controllers/userController.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ const bcrypt = require('bcryptjs');
77

88
userController.createUser = (req, res, next) => {
99
console.log('Creating user...');
10-
const { username, password } = req.body;
10+
const { email, username, password } = req.body;
1111
// error handling if username or password is missing
1212
if (!username) {
1313
return res.status(400).json('No username input');
1414
}
15+
if (!email) {
16+
return res.status(400).json('No email input');
17+
}
1518
if (!password) {
1619
return res.status(400).json('No password input');
1720
}
18-
const projects = [];
21+
1922
// create user using username and password
20-
Users.create({ username, password, projects }, (err, newUser) => {
23+
Users.create({ email, username, password, projects: [] }, (err, newUser) => {
2124
if (err) {
2225
return next({
2326
log: `Error in userController.createUser: ${err}`,

server/models/userModel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const Schema = mongoose.Schema;
55

66
const userSchema = new Schema({
77
username: { type: String, required: true, unique: true },
8+
email: { type: String, required: true, unique: true },
89
password: { type: String, required: true },
910
projects: Array
1011
});
1112

1213
// salt will go through 10 rounds of hashing
1314
const SALT_WORK_FACTOR = 10;
1415
const bcrypt = require('bcryptjs');
15-
const { session } = require('electron');
1616

1717
// mongoose middleware that will run before the save to collection happens (user gets put into database)
1818
// cannot use arrow function here as context of 'this' is important

src/components/login/SignIn.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { LoginInt } from '../../interfaces/Interfaces';
33
import { setLoginState } from '../../actions/actionCreators';
44
import { useSelector, useDispatch } from 'react-redux';
55
import { Link as RouteLink, withRouter, useHistory, RouteComponentProps } from 'react-router-dom';
6+
import { sessionIsCreated } from '../../helperFunctions/auth';
67

78
import Avatar from '@material-ui/core/Avatar';
89
import Button from '@material-ui/core/Button';
@@ -17,7 +18,6 @@ import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
1718
import Typography from '@material-ui/core/Typography';
1819
import { makeStyles, withStyles } from '@material-ui/core/styles';
1920
import Container from '@material-ui/core/Container';
20-
import { sessionIsCreated } from '../../helperFunctions/auth';
2121

2222
function Copyright() {
2323
return (
@@ -54,7 +54,6 @@ const useStyles = makeStyles(theme => ({
5454

5555
const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
5656
const classes = useStyles();
57-
const s = useSelector(state => state);
5857

5958
const dispatch = useDispatch();
6059

@@ -145,7 +144,8 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
145144
</Link>
146145
</Grid>
147146
<Grid item>
148-
<RouteLink to={`/signup`}>Don't have an account? Sign Up</RouteLink>
147+
{/* <RouteLink to={`/signup`} style={{ textDecoration: 'none' }}>Don't have an account? Sign Up</RouteLink> */}
148+
<Link href="/signup" variant="body2">Don't have an account? Sign Up</Link>
149149
</Grid>
150150
</Grid>
151151
</div>

src/components/login/SignUp.tsx

Lines changed: 91 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import React, { Component, useState } from 'react';
1+
import React, { Component, useState, useEffect } from 'react';
22
import { LoginInt } from '../../interfaces/Interfaces';
3-
import { setUsername, setPassword } from '../../actions/actionCreators';
4-
import { Link as RouteLink, withRouter } from 'react-router-dom';
3+
import { setLoginState } from '../../actions/actionCreators';
4+
import { useSelector, useDispatch } from 'react-redux';
5+
import { Link as RouteLink, withRouter, useHistory, RouteComponentProps } from 'react-router-dom';
6+
import { newUserIsCreated } from '../../helperFunctions/auth';
57

68
import Avatar from '@material-ui/core/Avatar';
79
import Button from '@material-ui/core/Button';
@@ -14,26 +16,8 @@ import Grid from '@material-ui/core/Grid';
1416
import Box from '@material-ui/core/Box';
1517
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
1618
import Typography from '@material-ui/core/Typography';
17-
import { makeStyles, withStyles } from '@material-ui/core/styles';
19+
import { makeStyles } from '@material-ui/core/styles';
1820
import Container from '@material-ui/core/Container';
19-
import { render } from 'enzyme';
20-
21-
const mapStateToProps = (store: any) => ({
22-
username: store.credentials.username,
23-
password: store.credentials.password
24-
});
25-
26-
const mapDispatchToProps = (dispatch: any) => ({
27-
setUsername: (username: string) => dispatch(setUsername(username)),
28-
setPassword: (password: string) => dispatch(setPassword(password))
29-
// login: (username: string, password: string) => dispatch(login(username, password)),
30-
// signup: (username: string, password: string) => dispatch(signup(username, password)),
31-
});
32-
33-
interface LoginProps extends LoginInt {
34-
setUsername(username: string): void;
35-
setPassword(username: string): void;
36-
}
3721

3822
function Copyright() {
3923
return (
@@ -48,36 +32,41 @@ function Copyright() {
4832
);
4933
}
5034

51-
const useStyles = makeStyles(theme => ({
35+
const useStyles = makeStyles((theme) => ({
5236
paper: {
5337
marginTop: theme.spacing(8),
5438
display: 'flex',
5539
flexDirection: 'column',
56-
alignItems: 'center'
40+
alignItems: 'center',
5741
},
5842
avatar: {
5943
margin: theme.spacing(1),
60-
backgroundColor: theme.palette.secondary.main
44+
backgroundColor: theme.palette.secondary.main,
6145
},
6246
form: {
6347
width: '100%', // Fix IE 11 issue.
64-
marginTop: theme.spacing(1)
48+
marginTop: theme.spacing(3),
6549
},
6650
submit: {
67-
margin: theme.spacing(3, 0, 2)
68-
}
51+
margin: theme.spacing(3, 0, 2),
52+
},
6953
}));
7054

71-
const SignUp: React.FC<LoginProps> = props => {
55+
const SignUp: React.FC<LoginInt & RouteComponentProps> = props => {
7256
const classes = useStyles();
73-
//const count = useSelector(state => state);
7457

58+
const dispatch = useDispatch();
59+
60+
const [email, setEmail] = useState('');
7561
const [username, setUsername] = useState('');
7662
const [password, setPassword] = useState('');
7763

78-
const handleChange = e => {
64+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
7965
let inputVal = e.target.value;
8066
switch (e.target.name) {
67+
case 'email':
68+
setEmail(inputVal);
69+
break;
8170
case 'username':
8271
setUsername(inputVal);
8372
break;
@@ -87,33 +76,18 @@ const SignUp: React.FC<LoginProps> = props => {
8776
}
8877
};
8978

90-
const handleSignup = e => {
91-
console.log('click fired on handleSignup');
79+
const handleSignUp = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
9280
e.preventDefault();
93-
const body = JSON.stringify({
94-
username,
95-
password
81+
console.log('click fired on handleLogin');
82+
newUserIsCreated(username, email, password).then(userCreated => {
83+
if(userCreated) {
84+
console.log('user created')
85+
dispatch(setLoginState()); // changes login state to true
86+
props.history.push('/');
87+
} else {
88+
console.log('invalid login')
89+
}
9690
});
97-
console.log(body);
98-
fetch('http://localhost:8080/signup', {
99-
method: 'POST',
100-
mode: 'cors',
101-
credentials: 'include',
102-
headers: {
103-
'Content-Type': 'application/json'
104-
},
105-
body
106-
})
107-
.then(res => res.json())
108-
.then(data => {
109-
if (typeof data === 'string') {
110-
alert(data);
111-
} else {
112-
props.history.push('/');
113-
alert('Signup successful! Please sign in to continue.');
114-
}
115-
})
116-
.catch(err => console.log(err));
11791
};
11892

11993
return (
@@ -124,63 +98,74 @@ const SignUp: React.FC<LoginProps> = props => {
12498
<LockOutlinedIcon />
12599
</Avatar>
126100
<Typography component="h1" variant="h5">
127-
Sign Up
101+
Sign up
128102
</Typography>
129-
<TextField
130-
variant="outlined"
131-
margin="normal"
132-
required
133-
fullWidth
134-
id="username"
135-
label="Username"
136-
name="username"
137-
autoComplete="username"
138-
autoFocus
139-
value={username}
140-
onChange={handleChange}
141-
/>
142-
<TextField
143-
variant="outlined"
144-
margin="normal"
145-
required
146-
fullWidth
147-
name="password"
148-
label="Password"
149-
type="password"
150-
id="password"
151-
autoComplete="current-password"
152-
value={password}
153-
onChange={handleChange}
154-
/>
155-
<FormControlLabel
156-
control={<Checkbox value="remember" color="primary" />}
157-
label="Remember me"
158-
/>
159-
<Button
160-
fullWidth
161-
variant="contained"
162-
color="primary"
163-
className={classes.submit}
164-
onClick={handleSignup}
165-
>
166-
Sign Up
167-
</Button>
168-
<Grid container>
169-
<Grid item xs>
170-
<Link href="#" variant="body2">
171-
Forgot password?
172-
</Link>
103+
<form className={classes.form} noValidate>
104+
<Grid container spacing={2}>
105+
<Grid item xs={12}>
106+
<TextField
107+
variant="outlined"
108+
required
109+
fullWidth
110+
id="email"
111+
label="Email Address"
112+
name="email"
113+
autoComplete="email"
114+
autoFocus
115+
value={email}
116+
onChange={handleChange}
117+
/>
118+
</Grid>
119+
<Grid item xs={12}>
120+
<TextField
121+
variant="outlined"
122+
required
123+
fullWidth
124+
id="username"
125+
label="Username"
126+
name="username"
127+
autoComplete="username"
128+
value={username}
129+
onChange={handleChange}
130+
/>
131+
</Grid>
132+
<Grid item xs={12}>
133+
<TextField
134+
variant="outlined"
135+
required
136+
fullWidth
137+
name="password"
138+
label="Password"
139+
type="password"
140+
id="password"
141+
autoComplete="current-password"
142+
value={password}
143+
onChange={handleChange}
144+
/>
145+
</Grid>
173146
</Grid>
174-
<Grid item>
175-
<RouteLink to={`/`}>Already have an account? Sign In</RouteLink>
147+
<Button
148+
type="submit"
149+
fullWidth
150+
variant="contained"
151+
color="primary"
152+
className={classes.submit}
153+
onClick={e => handleSignUp(e)}
154+
>
155+
Sign Up
156+
</Button>
157+
<Grid container justify="flex-end">
158+
<Grid item>
159+
<RouteLink to={`/login`}>Already have an account? Sign In</RouteLink>
160+
</Grid>
176161
</Grid>
177-
</Grid>
162+
</form>
178163
</div>
179-
<Box mt={8}>
164+
<Box mt={5}>
180165
<Copyright />
181166
</Box>
182167
</Container>
183168
);
184-
};
169+
}
185170

186171
export default withRouter(SignUp);

src/helperFunctions/auth.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,35 @@ export const sessionIsCreated = (username: string, password: string): Promise<bo
2626
return false
2727
});
2828
return result;
29+
}
30+
31+
export const newUserIsCreated = (username: string, email: string, password: string): Promise<boolean> => {
32+
const body = JSON.stringify({
33+
username,
34+
email,
35+
password
36+
});
37+
const result = fetch('http://localhost:8080/signup', {
38+
method: 'POST',
39+
credentials: 'include',
40+
headers: {
41+
'Content-Type': 'application/json'
42+
},
43+
body
44+
})
45+
.then(res => {
46+
return res.json();
47+
})
48+
.then(data => {
49+
console.log('the data', data);
50+
if (data.sessionId && typeof data.sessionId === 'string') { // check that a session id was passed down
51+
return true;
52+
}
53+
return false;
54+
})
55+
.catch(err => {
56+
console.log(err);
57+
return false
58+
});
59+
return result;
2960
}

0 commit comments

Comments
 (0)