Skip to content

Commit 8bb8431

Browse files
Signup added and routes conigured
1 parent 46a06c0 commit 8bb8431

File tree

6 files changed

+109
-11
lines changed

6 files changed

+109
-11
lines changed

src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { connect } from 'react-redux';
33
import { Route, BrowserRouter, Switch } from "react-router-dom";
44
import Login from './components/auth/Login';
55
import Signup from './components/auth/Signup';
6+
import LoggedinRoute from './components/LoggedinRoute';
67
import Navigation from './components/Navigation';
78
import PrivateRoute from './components/PrivateRoute';
89

@@ -14,7 +15,7 @@ class App extends Component {
1415
<Switch>
1516
{/* Authentication routes */}
1617
<Route path="/" exact={true} component={()=><p>Home page</p>} />
17-
<Route path="/auth" exact={true} component={Login} />
18+
<LoggedinRoute path="/auth" exact={true} component={Login} loggedin={this.props.loggedin} />
1819

1920
{/* Private routes that require users to be authenticated */}
2021
<PrivateRoute path="/dashboard" loggedin={false} component={Signup} />

src/actions/auth_actions.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ export const loginUser = (username, password) => {
5858
localStorage.setItem('token', data.token)
5959
return dispatch(loginSuccess(data.user))
6060
}
61-
}).catch(err => {
62-
return dispatch(loginError(err))
61+
}).catch(err => {
62+
dispatch(loginError("Some error has occurred"))
63+
setTimeout(()=>{
64+
dispatch(removeError())
65+
}, 0)
6366
})
6467
}
6568
}
@@ -85,6 +88,13 @@ const signup_error = (err) => {
8588
}
8689
}
8790

91+
const removeuser = () => {
92+
return {
93+
type : 'REMOVE_USER'
94+
}
95+
}
96+
97+
8898
// signup action for registeration of the users
8999
export const signupUserAction = (fname, username, email, password) => {
90100
return async dispatch => {
@@ -101,6 +111,17 @@ export const signupUserAction = (fname, username, email, password) => {
101111
}).then(res => res.json())
102112
.then(data => {
103113
console.log(data)
114+
if(data.message) {
115+
dispatch(signup_error(data.message))
116+
setTimeout(()=>{
117+
dispatch(removeError())
118+
}, 0)
119+
}else{
120+
dispatch(signup_success(data.user))
121+
setTimeout(()=>{
122+
dispatch(removeuser())
123+
}, 0)
124+
}
104125
}).catch(err => {
105126
console.log(err)
106127
})

src/components/LoggedinRoute.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { Component } from 'react';
2+
import { Redirect, Route } from 'react-router-dom';
3+
4+
export const LoggedinRoute = ({ component: Component, loggedin, ...rest }) => {
5+
return <Route
6+
{...rest}
7+
component = {
8+
({...props}) => {
9+
if(loggedin) {
10+
return <Redirect to="/" />
11+
}else{
12+
return <Component {...props} />
13+
}
14+
}
15+
}
16+
/>
17+
}
18+
19+
20+
export default LoggedinRoute;

src/components/auth/Signup.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {Component} from 'react';
22
import { withAlert } from 'react-alert';
33
import { connect } from 'react-redux';
4+
import { signupUserAction } from "../../actions/auth_actions";
45

56
class Signup extends Component {
67

@@ -31,6 +32,16 @@ class Signup extends Component {
3132
this.props.alert.show('Password length must be greater than 7', {type:'error'})
3233
return;
3334
}
35+
this.props.signupUserAction(this.state.fname, this.state.username, this.state.email, this.state.password)
36+
37+
this.setState({
38+
...this.state,
39+
fname : '',
40+
username : '',
41+
email : '',
42+
password : ''
43+
})
44+
3445
}
3546

3647
// updating state according to the input fields
@@ -42,28 +53,34 @@ class Signup extends Component {
4253

4354
// render method
4455
render () {
56+
if(this.props.signup.error) {
57+
this.props.alert.show(this.props.signup.error, {type:'error'})
58+
}
59+
if(this.props.signup.user !== null) {
60+
this.props.alert.show("Account succesfully created. Login to continue.", {type:'success'})
61+
}
4562
return (
4663
<div className="card" style={{borderRadius:"0"}}>
4764
<div className="card-body">
4865
<form onSubmit={this.formSubmit}>
4966
<div className="form-group">
5067
<label htmlFor="fname">Full Name</label>
51-
<input type="text" name="fname" id="fname" className="form-control" placeholder="John Doe" autoFocus onChange={this.onFieldsChange} />
68+
<input type="text" name="fname" id="fname" className="form-control" placeholder="John Doe" autoFocus onChange={this.onFieldsChange} value={this.state.fname} />
5269
</div>
5370

5471
<div className="form-group">
5572
<label htmlFor="username">Username</label>
56-
<input className="form-control" type="text" placeholder="ex. JohnDoe123" id="username" name="username" onChange={this.onFieldsChange} />
73+
<input className="form-control" type="text" placeholder="ex. JohnDoe123" id="username" name="username" onChange={this.onFieldsChange} value={this.state.username} />
5774
</div>
5875

5976
<div className="form-group">
6077
<label htmlFor="email">Email address</label>
61-
<input type="email" className="form-control" name="email" id="email" placeholder="johndoes123@xyz.com" onChange={this.onFieldsChange} />
78+
<input type="email" className="form-control" name="email" id="email" placeholder="johndoes123@xyz.com" onChange={this.onFieldsChange} value={this.state.email} />
6279
</div>
6380

6481
<div className="form-group">
6582
<label htmlFor="password">Password</label>
66-
<input type="password" name="password" id="password" className="form-control" placeholder="Enter a strong password" onChange={this.onFieldsChange} />
83+
<input type="password" name="password" id="password" className="form-control" placeholder="Enter a strong password" onChange={this.onFieldsChange} value={this.state.password} />
6784
</div>
6885

6986
<div className="form-group">
@@ -76,4 +93,11 @@ class Signup extends Component {
7693
}
7794
}
7895

79-
export default withAlert()(connect(null, {})(Signup));
96+
const mapStateToProps = state => {
97+
console.log(state.signup);
98+
return {
99+
signup : state.signup
100+
}
101+
}
102+
103+
export default withAlert()(connect(mapStateToProps, {signupUserAction})(Signup));

src/reducers/auth.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,35 @@ const loginReducer = (initial_state = state, action) => {
3232
}
3333
}
3434

35-
export { loginReducer }
35+
36+
// signup reducer
37+
const signupReducer = (initial_state = state, action) => {
38+
switch(action.type) {
39+
case 'SIGNUP_START' : return {
40+
...initial_state,
41+
loading : true
42+
}
43+
case 'SIGNUP_SUCCESS' : return {
44+
...initial_state,
45+
loading : false,
46+
user : action.payload
47+
}
48+
case 'SIGNUP_ERROR' : return {
49+
...initial_state,
50+
loading : false,
51+
error : action.payload
52+
}
53+
case 'REMOVE_ERROR' : return {
54+
...initial_state,
55+
loading : false,
56+
error : null
57+
}
58+
case 'REMOVE_USER' : return {
59+
...initial_state,
60+
user : null
61+
}
62+
default : return initial_state
63+
}
64+
}
65+
66+
export { loginReducer, signupReducer }

src/reducers/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {combineReducers} from 'redux';
22

3-
import { loginReducer } from "./auth";
3+
import { loginReducer, signupReducer } from "./auth";
44

55
export default combineReducers({
6-
auth : loginReducer
6+
auth : loginReducer,
7+
signup : signupReducer
78
})

0 commit comments

Comments
 (0)