Skip to content

Commit 2a30711

Browse files
authored
Merge pull request #9 from oslabs-beta/staging-faast
Signup / Login changes by Aaron
2 parents f3791a9 + 609e037 commit 2a30711

File tree

15 files changed

+219
-275
lines changed

15 files changed

+219
-275
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"@material-ui/styles": "^4.9.6",
113113
"@types/enzyme": "^3.10.5",
114114
"@types/enzyme-adapter-react-16": "^1.0.6",
115+
"@types/js-cookie": "^2.2.6",
115116
"@types/prettier": "^1.19.0",
116117
"@types/prismjs": "^1.16.0",
117118
"@types/react": "^16.8.14",
@@ -137,6 +138,7 @@
137138
"electron-splashscreen": "^1.0.0",
138139
"enzyme": "^3.4.1",
139140
"enzyme-adapter-react-16": "^1.2.0",
141+
"js-cookie": "^2.2.1",
140142
"konva": "^4.2.0",
141143
"localforage": "^1.7.2",
142144
"lodash.throttle": "^4.1.1",

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/actionTypes/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,4 @@ export const UPDATE_CHILDREN_SORT: string = 'UPDATE_CHILDREN_SORT';
4141
export const UPDATE_CODE: string = 'UPDATE_CODE';
4242
export const UPDATE_HTML_ATTR: string = 'UPDATE_HTML_ATTR';
4343

44-
export const SET_USERNAME: string = 'SET_USERNAME';
45-
export const SET_PASSWORD: string = 'SET_PASSWORD';
46-
export const LOGIN: string = 'LOGIN';
47-
export const SIGNUP: string = 'SIGNUP';
44+
export const SET_LOGIN_STATE: string = 'SET_LOGIN_STATE';

src/actions/actionCreators.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ import {
3939
UNDO,
4040
UPDATE_HTML_ATTR,
4141
UPDATE_CODE,
42-
SET_USERNAME,
43-
SET_PASSWORD,
44-
LOGIN,
45-
SIGNUP
42+
SET_LOGIN_STATE
4643
} from '../actionTypes/index';
4744
import { loadState } from '../localStorage';
4845
import createFiles from '../utils/createFiles.util';
@@ -333,28 +330,6 @@ export const updateHtmlAttr = ({
333330
payload: { attr, value }
334331
});
335332

336-
export const setUsername = (username: string): Action => ({
337-
type: SET_USERNAME,
338-
payload: username
339-
});
340-
341-
export const setPassword = (password: string): Action => ({
342-
type: SET_PASSWORD,
343-
payload: password
344-
})
345-
346-
// export const login = (username: string, password: string) => ({
347-
// return (dispatch: any) => {
348-
// fetch('/login', {
349-
// method: 'POST',
350-
// body: JSON.stringify({ username, password })
351-
// })
352-
// .then(response => response.json())
353-
// .then(data => console.log(data))
354-
// }
355-
// })
356-
357-
// export const signup = (username: string, password: string): Action => ({
358-
// type: SIGNUP,
359-
// payload: { username, password }
360-
// })
333+
export const setLoginState = (): Action => ({
334+
type: SET_LOGIN_STATE
335+
})

src/components/login/SignIn.tsx

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { Component, useState } from 'react';
2-
//import { connect } from 'react';
1+
import React, { Component, useState, useEffect } from 'react';
32
import { LoginInt } from '../../interfaces/Interfaces';
4-
import { setUsername, setPassword } from '../../actions/actionCreators';
5-
//import { useSelector } from 'react-redux';
6-
import { Link as RouteLink, withRouter, useHistory } 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 { sessionIsCreated } from '../../helperFunctions/auth';
77

88
import Avatar from '@material-ui/core/Avatar';
99
import Button from '@material-ui/core/Button';
@@ -18,24 +18,6 @@ import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
1818
import Typography from '@material-ui/core/Typography';
1919
import { makeStyles, withStyles } from '@material-ui/core/styles';
2020
import Container from '@material-ui/core/Container';
21-
import { render } from 'enzyme';
22-
23-
const mapStateToProps = (store: any) => ({
24-
username: store.credentials.username,
25-
password: store.credentials.password
26-
});
27-
28-
const mapDispatchToProps = (dispatch: any) => ({
29-
setUsername: (username: string) => dispatch(setUsername(username)),
30-
setPassword: (password: string) => dispatch(setPassword(password))
31-
// login: (username: string, password: string) => dispatch(login(username, password)),
32-
// signup: (username: string, password: string) => dispatch(signup(username, password)),
33-
});
34-
35-
interface LoginProps extends LoginInt {
36-
setUsername(username: string): void;
37-
setPassword(username: string): void;
38-
}
3921

4022
function Copyright() {
4123
return (
@@ -70,16 +52,15 @@ const useStyles = makeStyles(theme => ({
7052
}
7153
}));
7254

73-
const SignIn: React.FC<LoginProps> = props => {
55+
const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
7456
const classes = useStyles();
75-
//const count = useSelector(state => state);
57+
58+
const dispatch = useDispatch();
7659

7760
const [username, setUsername] = useState('');
7861
const [password, setPassword] = useState('');
7962

80-
const history = useHistory();
81-
82-
const handleChange = e => {
63+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
8364
let inputVal = e.target.value;
8465
switch (e.target.name) {
8566
case 'username':
@@ -91,34 +72,18 @@ const SignIn: React.FC<LoginProps> = props => {
9172
}
9273
};
9374

94-
const handleLogin = e => {
95-
console.log('click fired on handleLogin');
75+
const handleLogin = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
9676
e.preventDefault();
97-
const body = JSON.stringify({
98-
username,
99-
password
77+
console.log('click fired on handleLogin');
78+
sessionIsCreated(username, password).then(isLoggedIn => {
79+
if(isLoggedIn) {
80+
console.log('session created')
81+
dispatch(setLoginState()); // changes login state to true
82+
props.history.push('/');
83+
} else {
84+
console.log('invalid login')
85+
}
10086
});
101-
fetch('http://localhost:8080/login', {
102-
method: 'POST',
103-
credentials: 'include',
104-
headers: {
105-
'Content-Type': 'application/json'
106-
},
107-
body
108-
})
109-
.then(res => {
110-
return res.json();
111-
})
112-
.then(data => {
113-
if (typeof data === 'string') {
114-
alert(data);
115-
} else {
116-
props.history.push('/app');
117-
alert('Login successful!');
118-
//console.log('Data is', data);
119-
}
120-
})
121-
.catch(err => console.log(err));
12287
};
12388

12489
return (
@@ -167,7 +132,7 @@ const SignIn: React.FC<LoginProps> = props => {
167132
variant="contained"
168133
color="primary"
169134
className={classes.submit}
170-
onClick={handleLogin}
135+
onClick={e => handleLogin(e)}
171136
>
172137
Sign In
173138
</Button>
@@ -179,7 +144,7 @@ const SignIn: React.FC<LoginProps> = props => {
179144
</Link>
180145
</Grid>
181146
<Grid item>
182-
<RouteLink to={`/signup`}>Don't have an account? Sign Up</RouteLink>
147+
<RouteLink to={`/signup`} className="nav_link">Don't have an account? Sign Up</RouteLink>
183148
</Grid>
184149
</Grid>
185150
</div>

0 commit comments

Comments
 (0)