Skip to content

Commit e5d3f67

Browse files
committed
created helper functions in auth.ts
1 parent f108e3e commit e5d3f67

File tree

4 files changed

+85
-18
lines changed

4 files changed

+85
-18
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",

src/components/login/SignIn.tsx

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component, useState } from 'react';
1+
import React, { Component, useState, useEffect } from 'react';
22
//import { connect } from 'react';
33
import { LoginInt } from '../../interfaces/Interfaces';
44
import { setLoginState } from '../../actions/actionCreators';
@@ -20,6 +20,8 @@ import { makeStyles, withStyles } from '@material-ui/core/styles';
2020
import Container from '@material-ui/core/Container';
2121
import { render } from 'enzyme';
2222

23+
import Cookies from 'js-cookie';
24+
import { sessionIsCreated } from '../../helperFunctions/auth';
2325
/*
2426
const mapStateToProps = (store: any) => ({
2527
username: store.credentials.username,
@@ -75,15 +77,21 @@ const useStyles = makeStyles(theme => ({
7577
const SignIn: React.FC<LoginInt> = props => {
7678
const classes = useStyles();
7779
const s = useSelector(state => state);
80+
const history = useHistory();
81+
7882
const dispatch = useDispatch();
7983

8084
const [username, setUsername] = useState('');
8185
const [password, setPassword] = useState('');
82-
83-
const history = useHistory();
84-
8586
console.log('state on load: ', s.auth)
8687

88+
/*
89+
useEffect(() => {
90+
console.log('triggered')
91+
if(s.auth) history.push('/signup'); // push to the main app
92+
}, []);
93+
*/
94+
8795
const handleChange = e => {
8896
let inputVal = e.target.value;
8997
switch (e.target.name) {
@@ -97,11 +105,18 @@ const SignIn: React.FC<LoginInt> = props => {
97105
};
98106

99107
const handleLogin = e => {
108+
e.preventDefault();
100109
console.log('click fired on handleLogin');
101-
console.log('state: ', s);
102-
dispatch(setLoginState());
110+
sessionIsCreated(username, password).then(isLoggedIn => {
111+
if(isLoggedIn) {
112+
console.log('session created')
113+
dispatch(setLoginState()); // changes login state to true
114+
props.history.push('/signup');
115+
} else {
116+
console.log('invalid login')
117+
}
118+
});
103119
/*
104-
e.preventDefault();
105120
const body = JSON.stringify({
106121
username,
107122
password
@@ -118,16 +133,18 @@ const SignIn: React.FC<LoginInt> = props => {
118133
return res.json();
119134
})
120135
.then(data => {
121-
if (typeof data === 'string') {
122-
alert(data);
136+
console.log('the data', data);
137+
if (data.sessionId && typeof data.sessionId === 'string') {
138+
//alert(data); // these alerts cause the app to crash on mac
139+
console.log('success')
140+
dispatch(setLoginState()); // changes login state to true
141+
props.history.push('/signup');
123142
} else {
124-
props.history.push('/app');
125-
alert('Login successful!');
126-
//console.log('Data is', data);
143+
console.log('err');
127144
}
128145
})
129146
.catch(err => console.log(err));
130-
*/
147+
*/
131148
};
132149

133150
return (

src/helperFunctions/auth.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Cookies from 'js-cookie';
2+
3+
// check for a cookie when and redirect to main app when first loaded
4+
export const hasValidSession = () => {
5+
// Cookies.get('ssid');
6+
}
7+
8+
export const sessionIsCreated = (username: string, password: string): Promise<boolean> => {
9+
const body = JSON.stringify({
10+
username,
11+
password
12+
});
13+
const result = fetch('http://localhost:8080/login', {
14+
method: 'POST',
15+
credentials: 'include',
16+
headers: {
17+
'Content-Type': 'application/json'
18+
},
19+
body
20+
})
21+
.then(res => {
22+
return res.json();
23+
})
24+
.then(data => {
25+
console.log('the data', data);
26+
if (data.sessionId && typeof data.sessionId === 'string') { // check that a session id was passed down
27+
return true;
28+
}
29+
return false;
30+
})
31+
.catch(err => {
32+
console.log(err);
33+
return false
34+
});
35+
return result;
36+
}

src/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import 'babel-polyfill';
2-
import React from 'react';
2+
import React, { Component } from 'react';
33
import ReactDOM from 'react-dom';
44
import { Provider } from 'react-redux';
55
import App from './components/App.tsx';
66
import store from './store';
77

88
import SignIn from './components/login/SignIn.tsx';
99
import SignUp from './components/login/SignUp.tsx';
10-
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
10+
import {
11+
HashRouter as Router,
12+
Route,
13+
Redirect,
14+
Switch } from 'react-router-dom';
1115

1216
/*
1317
ReactDOM.render(
@@ -24,15 +28,23 @@ If cookie is valid, send the user to app and set the login boolean to true
2428
If the cookie has expired, send the user back to login
2529
*/
2630

31+
const PrivateRoute = ({ component: Component, ...rest }) => (
32+
<Route {...rest} render={(props) => (
33+
false
34+
? <Component {...props} />
35+
: <Redirect to='/login' />
36+
)} />
37+
)
38+
2739
ReactDOM.render(
2840
<Provider store={store}>
2941
<Router>
3042
<Switch>
3143
{/* change route to signin later for official release */}
32-
<Route exact path="/" component={SignIn} />
44+
<Route exact path="/login" component={SignIn} />
3345
<Route exact path="/signup" component={SignUp} />
34-
<Route exact path="/app" component={App} />
35-
{/* <PrivateRoute path='/protected' component={Protected} /> */}
46+
{/* <Route exact path="/app" component={App} /> */}
47+
<PrivateRoute path="/" component={App} />
3648
</Switch>
3749
</Router>
3850
</Provider>,

0 commit comments

Comments
 (0)