Skip to content

Commit e9a6fb3

Browse files
committed
signin, signup pages done. signout button working
1 parent e577819 commit e9a6fb3

File tree

12 files changed

+338
-107
lines changed

12 files changed

+338
-107
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
"react-draggable": "^3.0.5",
151151
"react-konva": "^16.12.0-0",
152152
"react-redux": "^7.2.0",
153+
"react-router-dom": "^5.2.0",
153154
"react-simple-code-editor": "^0.11.0",
154155
"react-sortable-tree": "^2.2.0",
155156
"react-syntax-highlighter": "^10.2.1",

server/controllers/cookieController.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
const cookieController = {};
22

3-
// setCookie = set a cookie with a random number
4-
5-
cookieController.setCookie = (req, res, next) => {
6-
// set cookie with key of 'secret' and value of a random number between 0 and 1000
7-
res.cookie('secret', Math.floor(Math.random() * 1000));
8-
console.log('Successful setCookie');
9-
return next();
10-
};
11-
123
// setSSIDCookie - store the user id from database in cookie
134
cookieController.setSSIDCookie = (req, res, next) => {
145
// set cookie with key 'ssid' and value to user's id, also set http only
15-
res.cookie('ssid', res.locals.id, { httpOnly: true });
6+
res.cookie('ssid', res.locals.id, { maxAge: 3600 });
167
console.log('Successful setSSIDCookie');
178
return next();
189
};

server/controllers/sessionController.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,36 @@ sessionController.isLoggedIn = (req, res, next) => {
2424

2525
// startSession - create and save a new session into the database
2626
sessionController.startSession = (req, res, next) => {
27-
// if valid user logged in/signed up, res.locals.id should be user's id generated from mongodb
2827
console.log('Inside startSession');
29-
Session.create({ cookieId: res.locals.id }, err => {
28+
// first check if user is logged in already
29+
Session.findOne({ cookieId: res.locals.id }, (err, session) => {
3030
if (err) {
3131
return next({
32-
log: `Error in sessionController.startSession: ${err}`,
32+
log: `Error in sessionController.startSession find session: ${err}`,
3333
message: {
34-
err: `Error in sessionController.startSession, check server logs for details`
34+
err: `Error in sessionController.startSession find session, check server logs for details`
3535
}
3636
});
37+
// if session doesn't exist, create a session
38+
// if valid user logged in/signed up, res.locals.id should be user's id generated from mongodb, which we will set as this session's cookieId
39+
} else if (!session) {
40+
Session.create({ cookieId: res.locals.id }, err => {
41+
if (err) {
42+
return next({
43+
log: `Error in sessionController.startSession create session: ${err}`,
44+
message: {
45+
err: `Error in sessionController.startSession create session, check server logs for details`
46+
}
47+
});
48+
}
49+
console.log('Successful startSession');
50+
return next();
51+
});
52+
// if session exists, move onto next middleware
53+
} else {
54+
console.log('Session exists, moving on');
55+
return next();
3756
}
38-
console.log('Successful startSession');
39-
return next();
4057
});
4158
};
42-
4359
module.exports = sessionController;

server/controllers/userController.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ userController.createUser = (req, res, next) => {
3636

3737
userController.verifyUser = (req, res, next) => {
3838
console.log('Inside verifyUser');
39+
console.log('req.body is', req.body);
3940
const { username, password } = req.body;
41+
if (!username || !password) {
42+
return res.status(400).json('No username or password input');
43+
}
4044
Users.findOne({ username }, (err, user) => {
4145
if (err) {
4246
return next({
@@ -45,7 +49,7 @@ userController.verifyUser = (req, res, next) => {
4549
err: `Error in userController.verifyUser, check server logs for details`
4650
}
4751
});
48-
} else {
52+
} else if (user) {
4953
// bcrypt compare function checks input password against hashed password
5054
bcrypt.compare(password, user.password).then(isMatch => {
5155
if (isMatch) {
@@ -55,9 +59,11 @@ userController.verifyUser = (req, res, next) => {
5559
return next();
5660
} else {
5761
// if password does not match, redirect to ?
58-
return res.status(400).send('Incorrect password');
62+
return res.status(400).json('Incorrect password');
5963
}
6064
});
65+
} else {
66+
return res.status(400).json('No such user found');
6167
}
6268
});
6369
};

server/server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ app.use(cookieParser());
2929
// statically serve everything in build folder
3030
app.use('/build', express.static(path.resolve(__dirname, '../build')));
3131

32-
app.get('/', cookieController.setCookie, (req, res) => {
32+
app.get('/', (req, res) => {
3333
res.status(200).sendFile(path.resolve(__dirname, '../src/public/index.html'));
3434
});
3535

@@ -39,7 +39,7 @@ app.post(
3939
cookieController.setSSIDCookie,
4040
sessionController.startSession,
4141
(req, res) => {
42-
return res.status(200).json(res.locals.id);
42+
return res.status(200).json({ userId: res.locals.id });
4343
}
4444
);
4545

@@ -49,7 +49,7 @@ app.post(
4949
cookieController.setSSIDCookie,
5050
sessionController.startSession,
5151
(req, res) => {
52-
return res.status(200).json(res.locals.id);
52+
return res.status(200).json({ userId: res.locals.id });
5353
}
5454
);
5555

src/components/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import '../public/styles/style.css';
33
import AppContainer from '../containers/AppContainer';
44

55
export const App: React.SFC = () => (
6-
<div className='app'>
6+
<div className="app">
77
<div>
88
<header style={{ height: '40px', width: '100%' }}>ReacType</header>
99
<AppContainer />

src/components/login/SignIn.tsx

Lines changed: 99 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import React, { Component, useState } from 'react';
2-
import { connect } from 'react';
2+
//import { connect } from 'react';
33
import { LoginInt } from '../../interfaces/Interfaces';
44
import { setUsername, setPassword } from '../../actions/actionCreators';
5-
import { useSelector } from 'react-redux';
5+
//import { useSelector } from 'react-redux';
6+
import {
7+
Link as RouteLink,
8+
withRouter,
9+
useHistory,
10+
Redirect
11+
} from 'react-router-dom';
612

713
import Avatar from '@material-ui/core/Avatar';
814
import Button from '@material-ui/core/Button';
@@ -22,18 +28,18 @@ import { render } from 'enzyme';
2228
const mapStateToProps = (store: any) => ({
2329
username: store.credentials.username,
2430
password: store.credentials.password
25-
})
31+
});
2632

2733
const mapDispatchToProps = (dispatch: any) => ({
2834
setUsername: (username: string) => dispatch(setUsername(username)),
29-
setPassword: (password: string) => dispatch(setPassword(password)),
35+
setPassword: (password: string) => dispatch(setPassword(password))
3036
// login: (username: string, password: string) => dispatch(login(username, password)),
3137
// signup: (username: string, password: string) => dispatch(signup(username, password)),
32-
})
38+
});
3339

3440
interface LoginProps extends LoginInt {
35-
setUsername(username: string): void,
36-
setPassword(username: string): void
41+
setUsername(username: string): void;
42+
setPassword(username: string): void;
3743
}
3844

3945
function Copyright() {
@@ -49,34 +55,36 @@ function Copyright() {
4955
);
5056
}
5157

52-
const useStyles = makeStyles((theme) => ({
58+
const useStyles = makeStyles(theme => ({
5359
paper: {
5460
marginTop: theme.spacing(8),
5561
display: 'flex',
5662
flexDirection: 'column',
57-
alignItems: 'center',
63+
alignItems: 'center'
5864
},
5965
avatar: {
6066
margin: theme.spacing(1),
61-
backgroundColor: theme.palette.secondary.main,
67+
backgroundColor: theme.palette.secondary.main
6268
},
6369
form: {
6470
width: '100%', // Fix IE 11 issue.
65-
marginTop: theme.spacing(1),
71+
marginTop: theme.spacing(1)
6672
},
6773
submit: {
68-
margin: theme.spacing(3, 0, 2),
69-
},
74+
margin: theme.spacing(3, 0, 2)
75+
}
7076
}));
7177

72-
const SignIn: React.FC<LoginProps> = (props) => {
78+
const SignIn: React.FC<LoginProps> = props => {
7379
const classes = useStyles();
7480
//const count = useSelector(state => state);
7581

7682
const [username, setUsername] = useState('');
7783
const [password, setPassword] = useState('');
7884

79-
const handleChange = (e) => {
85+
const history = useHistory();
86+
87+
const handleChange = e => {
8088
let inputVal = e.target.value;
8189
switch (e.target.name) {
8290
case 'username':
@@ -88,17 +96,33 @@ const SignIn: React.FC<LoginProps> = (props) => {
8896
}
8997
};
9098

91-
const handleLogin = (e) => {
92-
console.log('click fired on handleLogin')
99+
const handleLogin = e => {
100+
console.log('click fired on handleLogin');
93101
e.preventDefault();
94-
fetch('/login', {
102+
const body = JSON.stringify({
103+
username,
104+
password
105+
});
106+
console.log(body);
107+
fetch('http://localhost:8080/login', {
95108
method: 'POST',
96-
body: JSON.stringify({ username, password })
109+
mode: 'cors',
110+
headers: {
111+
'Content-Type': 'application/json'
112+
},
113+
body
97114
})
98-
.then(res => res.json())
99-
.then(data => console.log(data))
100-
.catch(err => console.log(err))
101-
}
115+
.then(res => res.json())
116+
.then(data => {
117+
if (typeof data === 'string') {
118+
alert(data);
119+
} else {
120+
alert('Login successful!');
121+
props.history.push('/app');
122+
}
123+
})
124+
.catch(err => console.log(err));
125+
};
102126

103127
return (
104128
<Container component="main" maxWidth="xs">
@@ -110,66 +134,63 @@ const SignIn: React.FC<LoginProps> = (props) => {
110134
<Typography component="h1" variant="h5">
111135
Sign in
112136
</Typography>
113-
<form className={classes.form} noValidate>
114-
<TextField
115-
variant="outlined"
116-
margin="normal"
117-
required
118-
fullWidth
119-
id="username"
120-
label="Username"
121-
name="username"
122-
autoComplete="username"
123-
autoFocus
124-
value={password}
125-
onChange={handleChange}
126-
/>
127-
<TextField
128-
variant="outlined"
129-
margin="normal"
130-
required
131-
fullWidth
132-
name="password"
133-
label="Password"
134-
type="password"
135-
id="password"
136-
autoComplete="current-password"
137-
value={password}
138-
onChange={handleChange}
139-
/>
140-
<FormControlLabel
141-
control={<Checkbox value="remember" color="primary" />}
142-
label="Remember me"
143-
/>
144-
<Button
145-
type="submit"
146-
fullWidth
147-
variant="contained"
148-
color="primary"
149-
className={classes.submit}
150-
onClick={handleLogin}
151-
>
152-
Sign In
153-
</Button>
154-
<Grid container>
155-
<Grid item xs>
156-
<Link href="#" variant="body2">
157-
Forgot password?
158-
</Link>
159-
</Grid>
160-
<Grid item>
161-
<Link href="#" variant="body2">
162-
{"Don't have an account? Sign Up"}
163-
</Link>
164-
</Grid>
137+
<TextField
138+
variant="outlined"
139+
margin="normal"
140+
required
141+
fullWidth
142+
id="username"
143+
label="Username"
144+
name="username"
145+
autoComplete="username"
146+
autoFocus
147+
value={username}
148+
onChange={handleChange}
149+
/>
150+
<TextField
151+
variant="outlined"
152+
margin="normal"
153+
required
154+
fullWidth
155+
name="password"
156+
label="Password"
157+
type="password"
158+
id="password"
159+
autoComplete="current-password"
160+
value={password}
161+
onChange={handleChange}
162+
/>
163+
<FormControlLabel
164+
control={<Checkbox value="remember" color="primary" />}
165+
label="Remember me"
166+
/>
167+
168+
<Button
169+
fullWidth
170+
variant="contained"
171+
color="primary"
172+
className={classes.submit}
173+
onClick={handleLogin}
174+
>
175+
Sign In
176+
</Button>
177+
178+
<Grid container>
179+
<Grid item xs>
180+
<Link href="#" variant="body2">
181+
Forgot password?
182+
</Link>
165183
</Grid>
166-
</form>
184+
<Grid item>
185+
<RouteLink to={`/signup`}>Don't have an account? Sign Up</RouteLink>
186+
</Grid>
187+
</Grid>
167188
</div>
168189
<Box mt={8}>
169190
<Copyright />
170191
</Box>
171192
</Container>
172193
);
173-
}
194+
};
174195

175-
export default SignIn;
196+
export default withRouter(SignIn);

0 commit comments

Comments
 (0)