Skip to content

Commit cd1a7f0

Browse files
committed
Create redux for session
Use redux containers in displaying sign in form and sign out button
1 parent 053becf commit cd1a7f0

File tree

15 files changed

+86
-31
lines changed

15 files changed

+86
-31
lines changed

api/src/seeders/20180209100520-users-redirect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
module.exports = {
44
up: (queryInterface, Sequelize) => {
55
return Promise.all([
6-
userRedirect(queryInterface, 'referrer@email.com', { url: '/about' }),
7-
userRedirect(queryInterface, 'redirect@email.com', { external: true, url: 'https://fb.com/Ironcoder' })
6+
userRedirect(queryInterface, 'referrer@email.com', { url: '/redux' }),
7+
userRedirect(queryInterface, 'redirect@email.com', { external: true, url: 'https://github.com/rickyhurtado/node-api-and-client-boilerplate' })
88
])
99
},
1010

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
import React, { Component } from 'react'
2+
import { Redirect } from 'react-router-dom'
23
import Axios from '../../Lib/Common/Axios'
34
import * as Session from '../../Lib/Helpers/Session'
5+
import { closeNavbar } from '../../Lib/Common/Views'
46

57
export default class SignOut extends Component {
68
signOut() {
79
Axios
810
.post(process.env.REACT_APP_API_SIGN_OUT_URL)
911
.then(response => {
1012
Session.deleteToken()
11-
12-
const referrer = this.props.referrer.split('/')[1]
13-
14-
if (referrer === 'admin') {
15-
window.location.reload()
16-
} else {
17-
window.location.href = '/sign-in'
18-
}
13+
closeNavbar()
14+
this.props.auth(false)
1915
})
2016
}
2117

2218
render() {
23-
if (Session.isSignedIn()) {
19+
const props = this.props
20+
21+
if (props.isSignedIn) {
2422
return (
2523
<li>
2624
<button className="sign-out-btn" onClick={this.signOut.bind(this)}>Sign Out</button>
2725
</li>
2826
)
2927
}
3028

31-
return null
29+
if (['*', '/admin/*'].indexOf(props.referrer) > -1) {
30+
return null
31+
}
32+
33+
return <Redirect to={props.referrer} />
3234
}
3335
}

client/src/Components/Forms/SignIn.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ export default class SignIn extends Component {
1818
alertMessage: {},
1919
showAlertMessage: false,
2020
isSigningIn: false,
21-
isSignedIn: Session.isSignedIn(),
2221
redirect: { url: '/' },
23-
locationState: props.location.state
22+
locationState: props.location.state
2423
}
2524
}
2625

@@ -62,9 +61,9 @@ export default class SignIn extends Component {
6261
setTimeout(function(){
6362
_this.setState({
6463
formData: initFormData,
65-
isSignedIn: true,
6664
isSigningIn: false
6765
})
66+
_this.props.auth(true);
6867
}, 500)
6968
})
7069
.catch(error => {
@@ -88,7 +87,7 @@ export default class SignIn extends Component {
8887
}
8988

9089
render() {
91-
if (this.state.isSignedIn) {
90+
if (this.props.isSignedIn) {
9291
const referrer = this.state.locationState
9392
? this.state.locationState.from.pathname
9493
: this.state.redirect.url

client/src/Components/Layout/Admin/Header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Component } from 'react'
22
import { Link, withRouter } from 'react-router-dom'
33
import { Navbar } from 'react-bootstrap'
44
import { NavLink } from '../../../Lib/Common/Views'
5-
import Button from '../../Button'
5+
import SignOutButton from '../../../Redux/Containers/Sessions/SignOut'
66
import ReactLogo from '../../../Assets/Images/react-logo.svg'
77

88
class Header extends Component {
@@ -23,7 +23,7 @@ class Header extends Component {
2323
<ul className="navbar-nav nav navbar-right">
2424
<NavLink title="Home" to="/" />
2525
<NavLink title="Redux" to="/redux" />
26-
<Button.SignOut referrer={path} />
26+
<SignOutButton referrer={path} />
2727
</ul>
2828
</Navbar.Collapse>
2929
</Navbar>

client/src/Components/Layout/Admin/Sidebar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react'
22
import { withRouter } from 'react-router-dom'
33
import { AuthNavLink } from '../../../Lib/Common/Views'
4-
import Button from '../../Button'
4+
import SignOutButton from '../../../Redux/Containers/Sessions/SignOut'
55

66
class Sidebar extends Component {
77
render() {
@@ -12,7 +12,7 @@ class Sidebar extends Component {
1212
<ul className="nav nav-sidebar">
1313
<AuthNavLink title="Dashboard" to="/admin/dashboard" path={path} />
1414
<AuthNavLink title="Settings" to="/admin/settings" path={path} />
15-
<Button.SignOut referrer={path} />
15+
<SignOutButton referrer={path} />
1616
</ul>
1717
</nav>
1818
)

client/src/Components/Layout/Basic/Header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Component } from 'react'
22
import { Link, withRouter } from 'react-router-dom'
33
import { Navbar } from 'react-bootstrap'
44
import { NavLink, AuthNavLink } from '../../../Lib/Common/Views'
5-
import Button from '../../Button'
5+
import SignOutButton from '../../../Redux/Containers/Sessions/SignOut'
66

77
class Header extends Component {
88
render() {
@@ -20,7 +20,7 @@ class Header extends Component {
2020
<NavLink title="Redux" to="/redux" path={path} />
2121
<NavLink title="Sign In" to="/sign-in" path={path} isSignedOut />
2222
<AuthNavLink title="Admin" to="/admin/dashboard" />
23-
<Button.SignOut referrer={path} />
23+
<SignOutButton referrer={path} />
2424
</ul>
2525
</Navbar.Collapse>
2626
</Navbar>

client/src/Lib/Common/Views.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ export const AuthNavLink = ({...props}) => {
4444
) : null
4545
}
4646

47+
export function closeNavbar() {
48+
const { width } = viewportDimension()
49+
50+
if (width < 768) {
51+
document.getElementById('js-navbar-toggle-btn').click()
52+
}
53+
}
54+
4755
export function viewportDimension() {
4856
const w = window,
4957
d = document,
@@ -58,11 +66,3 @@ export function viewportDimension() {
5866
function navLinkIsActive({...props}) {
5967
return props.path === props.to ? 'active' : ''
6068
}
61-
62-
function closeNavbar() {
63-
const { width } = viewportDimension()
64-
65-
if (width < 768) {
66-
document.getElementById('js-navbar-toggle-btn').click()
67-
}
68-
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const AUTH = 'AUTH'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { AUTH } from '../../Actions/Sessions/Types'
2+
3+
export function auth(isSignedIn) {
4+
return { type: AUTH, isSignedIn }
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { bindActionCreators } from 'redux'
2+
import { connect } from 'react-redux'
3+
import { auth } from '../../Actions/Sessions'
4+
import SignInForm from '../../../Components/Forms/SignIn'
5+
6+
function mapStateToProps({ isSignedIn }) {
7+
return { isSignedIn }
8+
}
9+
10+
function mapDispatchToProps(dispatch) {
11+
return bindActionCreators({ auth }, dispatch)
12+
}
13+
14+
export default connect(mapStateToProps, mapDispatchToProps)(SignInForm)

0 commit comments

Comments
 (0)