Skip to content

Commit eaf5283

Browse files
committed
Implement Login page and add username to registration
1 parent b6a2cf1 commit eaf5283

File tree

6 files changed

+132
-6
lines changed

6 files changed

+132
-6
lines changed

src/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import CardEditor from './CardEditor';
33
import CardViewer from './CardViewer';
44
import Homepage from './Homepage';
55
import PageRegister from './PageRegister';
6+
import PageLogin from './PageLogin';
67

78
import { Switch, Route } from 'react-router-dom';
89
import { connect } from 'react-redux';
@@ -27,6 +28,9 @@ const App = props => {
2728
<Route exact path="/register">
2829
<PageRegister />
2930
</Route>
31+
<Route exact path="/login">
32+
<PageLogin />
33+
</Route>
3034
<Route>
3135
<div>Page not found!</div>
3236
</Route>

src/CardEditor.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
22
import './CardEditor.css';
33

4-
import { Link, withRouter } from 'react-router-dom';
4+
import { Link, withRouter, Redirect } from 'react-router-dom';
55
import { firebaseConnect } from 'react-redux-firebase';
66
import { compose } from 'redux';
7+
import { connect } from 'react-redux';
78

89
class CardEditor extends React.Component {
910
constructor(props) {
@@ -50,6 +51,10 @@ class CardEditor extends React.Component {
5051
};
5152

5253
render() {
54+
if (!this.props.isLoggedIn) {
55+
return <Redirect to="/register" />;
56+
}
57+
5358
const cards = this.state.cards.map((card, index) => {
5459
return (
5560
<tr key={index}>
@@ -115,4 +120,12 @@ class CardEditor extends React.Component {
115120
}
116121
}
117122

118-
export default compose(firebaseConnect(), withRouter)(CardEditor);
123+
const mapStateToProps = state => {
124+
return { isLoggedIn: state.firebase.auth.uid };
125+
};
126+
127+
export default compose(
128+
firebaseConnect(),
129+
connect(mapStateToProps),
130+
withRouter,
131+
)(CardEditor);

src/Homepage.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,29 @@ const Homepage = props => {
2323
<Link to="/editor">Create a new flashcards deck!</Link>
2424
<h3>Flashcards</h3>
2525
{decks}
26+
<h3>Account</h3>
27+
{props.isLoggedIn ? (
28+
<div>
29+
<div>{props.email}</div>
30+
<button onClick={() => props.firebase.logout()}>Logout</button>
31+
</div>
32+
) : (
33+
<div>
34+
<Link to="/register">Register</Link>
35+
<br />
36+
<Link to="/login">Login</Link>
37+
</div>
38+
)}
2639
</div>
2740
);
2841
};
2942

3043
const mapStateToProps = state => {
31-
return { homepage: state.firebase.data.homepage };
44+
return {
45+
homepage: state.firebase.data.homepage,
46+
email: state.firebase.auth.email,
47+
isLoggedIn: state.firebase.auth.uid,
48+
};
3249
};
3350

3451
export default compose(

src/PageLogin.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React from 'react';
2+
import { firebaseConnect } from 'react-redux-firebase';
3+
import { connect } from 'react-redux';
4+
import { compose } from 'redux';
5+
import { Redirect, Link } from 'react-router-dom';
6+
7+
class PageLogin extends React.Component {
8+
constructor(props) {
9+
super(props);
10+
this.state = {
11+
email: '',
12+
password: '',
13+
};
14+
}
15+
16+
handleChange = event =>
17+
this.setState({ [event.target.name]: event.target.value, error: '' });
18+
19+
login = async () => {
20+
const credentials = {
21+
email: this.state.email,
22+
password: this.state.password,
23+
};
24+
25+
try {
26+
await this.props.firebase.login(credentials);
27+
} catch (error) {
28+
this.setState({ error: error.message });
29+
}
30+
};
31+
32+
render() {
33+
if (this.props.isLoggedIn) {
34+
return <Redirect to="/" />;
35+
}
36+
37+
return (
38+
<div>
39+
<h2>Login</h2>
40+
<div>
41+
<div>{this.state.error}</div>
42+
<input
43+
name="email"
44+
onChange={this.handleChange}
45+
placeholder="Email"
46+
value={this.state.email}
47+
/>
48+
<br />
49+
<input
50+
name="password"
51+
onChange={this.handleChange}
52+
placeholder="Password"
53+
type="password"
54+
value={this.state.password}
55+
/>
56+
</div>
57+
<br />
58+
<button onClick={this.login}>Login!</button>
59+
<hr />
60+
<Link to="/">Home</Link>
61+
<br />
62+
<Link to="/register">Register</Link>
63+
</div>
64+
);
65+
}
66+
}
67+
68+
const mapStateToProps = state => {
69+
return { isLoggedIn: state.firebase.auth.uid };
70+
};
71+
72+
export default compose(firebaseConnect(), connect(mapStateToProps))(PageLogin);

src/PageRegister.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import React from 'react';
22
import { firebaseConnect } from 'react-redux-firebase';
33
import { connect } from 'react-redux';
44
import { compose } from 'redux';
5-
import { Redirect } from 'react-router-dom';
5+
import { Redirect, Link } from 'react-router-dom';
66

77
class PageRegister extends React.Component {
88
constructor(props) {
99
super(props);
1010
this.state = {
1111
email: '',
1212
password: '',
13+
username: '',
1314
};
1415
}
1516

@@ -22,8 +23,13 @@ class PageRegister extends React.Component {
2223
password: this.state.password,
2324
};
2425

26+
const profile = {
27+
email: this.state.email,
28+
username: this.state.username,
29+
};
30+
2531
try {
26-
await this.props.firebase.createUser(credentials);
32+
await this.props.firebase.createUser(credentials, profile);
2733
} catch (error) {
2834
this.setState({ error: error.message });
2935
}
@@ -53,9 +59,22 @@ class PageRegister extends React.Component {
5359
type="password"
5460
value={this.state.password}
5561
/>
62+
<br />
63+
<input
64+
name="username"
65+
onChange={this.handleChange}
66+
placeholder="Username"
67+
value={this.state.username}
68+
/>
5669
</div>
5770
<br />
58-
<button onClick={this.register}>Register!</button>
71+
<button disabled={!this.state.username.trim()} onClick={this.register}>
72+
Register!
73+
</button>
74+
<hr />
75+
<Link to="/">Home</Link>
76+
<br />
77+
<Link to="/login">Login</Link>
5978
</div>
6079
);
6180
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const store = createStore(rootReducer, composeWithDevTools());
3737

3838
// react-redux-firebase config
3939
const rrfConfig = {
40+
preserveOnLogout: ['homepage'],
4041
userProfile: 'users',
4142
};
4243

0 commit comments

Comments
 (0)