Skip to content

Commit

Permalink
added login and logout actions in auth reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
SamratDe committed Aug 23, 2020
1 parent f1db4f6 commit 870f672
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 20 deletions.
32 changes: 32 additions & 0 deletions client/src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const register = ({ name, email, password }) => async (dispatch) => {
type: actionTypes.REGISTER_SUCCESS,
payload: res.data,
})
dispatch(loadUser())
} catch (err) {
const errors = err.response.data.errors
if (errors) {
Expand All @@ -44,3 +45,34 @@ export const register = ({ name, email, password }) => async (dispatch) => {
})
}
}

export const login = (email, password) => async (dispatch) => {
const config = {
headers: {
'Content-Type': 'application/json',
},
}
const body = JSON.stringify({ email, password })
try {
const res = await axios.post('/api/auth', body, config)
dispatch({
type: actionTypes.LOGIN_SUCCESS,
payload: res.data,
})
dispatch(loadUser())
} catch (err) {
const errors = err.response.data.errors
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, 'danger')))
}
dispatch({
type: actionTypes.LOGIN_FAIL,
})
}
}

export const logout = () => (dispatch) => {
dispatch({
type: actionTypes.LOGOUT,
})
}
3 changes: 3 additions & 0 deletions client/src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export const REGISTER_SUCCESS = 'REGISTER_SUCCESS'
export const REGISTER_FAIL = 'REGISTER_FAIL'
export const USER_LOADED = 'USER_LOADED'
export const AUTH_ERROR = 'AUTH_ERROR'
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS'
export const LOGIN_FAIL = 'LOGIN_FAIL'
export const LOGOUT = 'LOGOUT'
58 changes: 44 additions & 14 deletions client/src/components/Layout/Navbar.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
import React from 'react'
import React, { Fragment } from 'react'
import { Link } from 'react-router-dom'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'

import { logout } from '../../actions/auth'

const Navbar = ({ auth: { isAuthenticated, loading }, logout }) => {
const authLinks = (
<ul>
<li>
<a onClick={logout} href='#!'>
<i className='fas fa-sign-out-alt'></i>{' '}
<span className='hide-sm'>Logout</span>
</a>
</li>
</ul>
)

const guestLinks = (
<ul>
<li>
<a href='#!'>Developers</a>
</li>
<li>
<Link to='/register'>Register</Link>
</li>
<li>
<Link to='/login'>Login</Link>
</li>
</ul>
)

const Navbar = () => {
return (
<nav className='navbar bg-dark'>
<h1>
<Link to='/'>
<i className='fas fa-code'></i> Developer HUB
</Link>
</h1>
<ul>
<li>
<Link to='/profiles'>Developers</Link>
</li>
<li>
<Link to='/register'>Register</Link>
</li>
<li>
<Link to='/login'>Login</Link>
</li>
</ul>
{!loading && (
<Fragment>{isAuthenticated ? authLinks : guestLinks}</Fragment>
)}
</nav>
)
}

export default Navbar
Navbar.prototype = {
logout: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired,
}

const mapStateToProps = (state) => ({
auth: state.auth,
})

export default connect(mapStateToProps, { logout })(Navbar)
24 changes: 21 additions & 3 deletions client/src/components/auth/LogIn.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React, { Fragment, useState } from 'react'
import { Link } from 'react-router-dom'
import { Link, Redirect } from 'react-router-dom'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'

const LogIn = () => {
import { login } from '../../actions/auth'

const LogIn = ({ login, isAuthenticated }) => {
const [formData, setFormData] = useState({
email: '',
password: '',
Expand All @@ -15,6 +19,11 @@ const LogIn = () => {

const submitHandler = (e) => {
e.preventDefault()
login(email, password)
}

if (isAuthenticated) {
return <Redirect to='/dashboard' />
}

return (
Expand Down Expand Up @@ -57,4 +66,13 @@ const LogIn = () => {
)
}

export default LogIn
LogIn.propTypes = {
login: PropTypes.func.isRequired,
isAuthenticated: PropTypes.bool,
}

const mapStateToProps = (state) => ({
isAuthenticated: state.auth.isAuthenticated,
})

export default connect(mapStateToProps, { login })(LogIn)
15 changes: 12 additions & 3 deletions client/src/components/auth/Register.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { Fragment, useState } from 'react'
import { Link } from 'react-router-dom'
import { Link, Redirect } from 'react-router-dom'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'

import { setAlert } from '../../actions/alert'
import { register } from '../../actions/auth'

const Register = ({ setAlert, register }) => {
const Register = ({ setAlert, register, isAuthenticated }) => {
const [formData, setFormData] = useState({
name: '',
email: '',
Expand All @@ -29,6 +29,10 @@ const Register = ({ setAlert, register }) => {
}
}

if (isAuthenticated) {
return <Redirect to='/dashboard' />
}

return (
<Fragment>
<h1 className='large text-primary'>Sign Up</h1>
Expand Down Expand Up @@ -92,6 +96,11 @@ const Register = ({ setAlert, register }) => {
Register.propTypes = {
setAlert: PropTypes.func.isRequired,
register: PropTypes.func.isRequired,
isAuthenticated: PropTypes.bool,
}

export default connect(null, { setAlert, register })(Register)
const mapStateToProps = (state) => ({
isAuthenticated: state.auth.isAuthenticated,
})

export default connect(mapStateToProps, { setAlert, register })(Register)
3 changes: 3 additions & 0 deletions client/src/reducers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const intialState = {
export default function (state = intialState, action) {
switch (action.type) {
case actionTypes.REGISTER_SUCCESS:
case actionTypes.LOGIN_SUCCESS:
localStorage.setItem('token', action.payload.token)
return {
...state,
Expand All @@ -19,6 +20,8 @@ export default function (state = intialState, action) {
}
case actionTypes.REGISTER_FAIL:
case actionTypes.AUTH_ERROR:
case actionTypes.LOGIN_FAIL:
case actionTypes.LOGOUT:
localStorage.removeItem('token')
return {
...state,
Expand Down
3 changes: 3 additions & 0 deletions client/src/utils/setAuthToken.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import axios from 'axios'

// if the token is there it will add it to the headers
// and
// if the token is not there it will delete it from the headers
const setAuthToken = (token) => {
if (token) {
axios.defaults.headers.common['x-auth-token'] = token
Expand Down

0 comments on commit 870f672

Please sign in to comment.