-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
164047023 Feature(Reset Password) : implement reset password
- create requestPasswordReset - create resetPassword component [Finishes #164047023]
- Loading branch information
1 parent
ed7fc48
commit 410ee8d
Showing
18 changed files
with
386 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
justify-content: center; | ||
font-size: calc(10px + 2vmin); | ||
color: white; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { EMAIL_SEND, FAIL_SEND } from "./types"; | ||
import urlPath from "../configs/axios"; | ||
|
||
export const success = message => ({ type: EMAIL_SEND, payload: message }); | ||
export const failure = errors => ({ type: FAIL_SEND, payload: errors }); | ||
|
||
const requestPassword = data => (dispatch) => { | ||
const success = message => ({ type: EMAIL_SEND, payload: message }); | ||
const failure = errors => ({ type: FAIL_SEND, payload: errors }); | ||
|
||
return urlPath | ||
.request({ | ||
method: "post", | ||
url: "users/reset_password/", | ||
data: { | ||
email: data.emailData | ||
} | ||
}) | ||
.then((res) => { | ||
if (res.data) { | ||
console.log(res) | ||
dispatch(success(res.data.message)); | ||
} | ||
}) | ||
.catch((err) => { | ||
if (err.request.response) { | ||
console.log(JSON.parse(err.request.response).errors.email[0]) | ||
dispatch(failure(JSON.parse(err.request.response).errors.email[0])); | ||
} | ||
dispatch(failure(err.response.data.errors.email)); | ||
|
||
|
||
}); | ||
}; | ||
export default requestPassword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { RESET_SUCCESSFUL, RESET_FAILURE, RESET_REQUEST } from "./types"; | ||
import urlPath from "../configs/axios" | ||
|
||
export const resetPasswordAction = () => ({ type: RESET_REQUEST }); | ||
export const success = message => ({ type: RESET_SUCCESSFUL, payload: message }); | ||
export const failure = error => ({ type: RESET_FAILURE, payload: error }); | ||
|
||
|
||
export const resetPassword = (data) => (dispatch) => { | ||
const urlArray = window.location.href.split('/'); | ||
const token = urlArray[urlArray.length - 1]; | ||
dispatch(resetPasswordAction()); | ||
|
||
return urlPath | ||
.request({ | ||
method: "put", | ||
url: `users/reset_password/${token}`, | ||
data: { password: data.password } | ||
}) | ||
.then((res) => { | ||
dispatch(success(res.data.message)); | ||
setTimeout(() => { | ||
window.location.href = "/"; | ||
}, 2000); | ||
}) | ||
.catch((err) => { | ||
console.log(err.request) | ||
|
||
dispatch(failure("Error while making the request")); | ||
}); | ||
}; | ||
export default resetPassword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const EMAIL_SEND = "EMAIL_SUCCESSFUL"; | ||
export const FAIL_SEND = "FAIL_SEND"; | ||
export const RESET_REQUEST = "RESET_REQUEST"; | ||
export const RESET_SUCCESSFUL = "RESET_SUCCESSFUL"; | ||
export const RESET_FAILURE = "RESET_FAILURE"; | ||
|
||
|
||
export const BASE_URL = "https://aholympian.herokuapp.com/api/"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { connect } from "react-redux"; | ||
import React, { Component } from "react"; | ||
import requestPassword from "../actions/requestPasswordReset"; | ||
import 'bootstrap/dist/css/bootstrap.css'; | ||
import { Form, Container } from 'react-bootstrap' | ||
|
||
class RequestResetForm extends Component { | ||
state = { email: "", message: '' }; | ||
|
||
handleChange = (e) => { | ||
this.setState({ | ||
email: e.target.value | ||
}); | ||
} | ||
|
||
handleSubmit = (e) => { | ||
e.preventDefault(); | ||
const emailData = this.state.email; | ||
const data = { emailData }; | ||
this.props.requestPassword(data).then(() => { | ||
console.log(this.props.message.email) | ||
|
||
this.setState({message: this.props.message}) | ||
}); | ||
} | ||
|
||
render() { | ||
const message = this.props.message; | ||
return ( | ||
<div > | ||
<Container> | ||
<div className="m-4" style={{ height: "600px" }}> | ||
<div className="text-center mb-10 mt-50" style={{ height: "20%" }}> | ||
<h2 className="h-100 my-auto mt-25">Reset Password</h2> | ||
</div> | ||
<div className="card text-center my-auto mx-auto border-dark" style={{ height: "70%", backgroundColor: "#27586e" ,width:"50%"}}> | ||
<div className="card-body" > | ||
<Form.Group style={{ height: "20px" }} > | ||
<div><h5 style={{ color: "white" }}> Enter your email address and <br />will send you a link to reset password</h5></div> | ||
<div> | ||
{this.state.message && <p style={{ color: "red" }}>{message}</p>} | ||
</div> | ||
<input className="m-6 my-4 input-normal" style={{ width: "88%" }} type="text" placeholder="Enter your email address" value={this.state.email} name="email" onChange={this.handleChange} required /> | ||
<button className="m-6 my-4 input-normal text-white" type="button" style={{ width: "88%", backgroundColor: "#519e8a", color: "white" }} className="submitbtn" onClick={this.handleSubmit}>send email for reset password</button> | ||
|
||
</Form.Group > | ||
|
||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
</Container> | ||
<footer className="bg-dark page-footer font-small blue mb-0px" style={{ | ||
width: "100%" ,height: "25% " | ||
}}> | ||
<div class="footer-copyright text-center py-3"> | ||
All Rights Reserved<br /> | ||
Authors Haven<br /> | ||
© 2019<br /> | ||
|
||
</div> | ||
|
||
</footer> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
requestPassword: data => dispatch(requestPassword(data)) | ||
}); | ||
const mapStateToProps = state => ({ message: state.requestPassword.message }); | ||
export default connect(mapStateToProps, mapDispatchToProps)(RequestResetForm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { connect } from "react-redux"; | ||
import React, { Component } from "react"; | ||
import { resetPassword } from "../actions/resetPassword"; | ||
import 'bootstrap/dist/css/bootstrap.css'; | ||
import { Form, Container } from 'react-bootstrap'; | ||
|
||
|
||
class ResetPassword extends Component { | ||
state = { password: "", confirmPassword: "", message: true, error: true }; | ||
|
||
handleChange = (e) => { | ||
this.setState({ [e.target.id]: e.target.value }); | ||
if (e.target.id === "password") { | ||
if (e.target.value.length === 0) { | ||
this.setState({ error: true }); | ||
this.setState({ message: "Password cannot be empty" }); | ||
return; | ||
} | ||
if (!this.validatePassword(e.target.value)) { | ||
this.setState({ error: true }); | ||
this.setState({ message: "Password should be atleast 8 characters with a capital letter, a small letter, a number and a special character" }); | ||
return; | ||
} | ||
} | ||
if (e.target.id === "confirmPassword") { | ||
if (e.target.value !== this.state.password) { | ||
|
||
this.setState({ error: true }); | ||
this.setState({ message: "Passwords do not match" }); | ||
return; | ||
} | ||
else { | ||
this.setState({ error: null, message: null }); | ||
} | ||
} | ||
else { | ||
this.setState({ error: null, message: null }); | ||
} | ||
} | ||
|
||
validatePassword = (password) => { | ||
const valid = password.match(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/); | ||
return valid; | ||
} | ||
|
||
handleSubmit = (e) => { | ||
|
||
e.preventDefault(); | ||
if (this.state.password === this.state.confirmPassword) { | ||
const passwordData = this.state.password; | ||
const data = { password: passwordData }; | ||
this.props.resetPassword(data); | ||
} else { | ||
this.setState({ message: "Passwords didn't match" }); | ||
} | ||
} | ||
|
||
render() { | ||
const message = this.props.message; | ||
|
||
return ( | ||
<div> | ||
<Container> | ||
<div className="m-4" style={{ height: "600px" }}> | ||
<div className="text-center mb-10 mt-50" style={{ height: "20%" }}> | ||
<h2 className="h-100 my-auto mt-25">Reset Password</h2> | ||
</div> | ||
<div className="card text-center my-auto mx-auto border-dark" style={{ height: "70%", backgroundColor: "#27586e", width: "50%" }}> | ||
<div className="card-body" > | ||
<Form.Group style={{ height: "20px" }} > | ||
<div> | ||
{this.state.message && <p style={{ color: "red" }}>{this.state.message}</p>} | ||
</div> | ||
<input className="m-6 my-4 input-normal" style={{ width: "88%" }} type="password" id="password" placeholder="Enter New Password" value={this.state.password} name="password" onChange={this.handleChange} required /> | ||
<input className="m-6 my-4 input-normal" style={{ width: "88%" }} type="password" id="confirmPassword" placeholder=" Enter Confirm Password" value={this.state.confirmPassword} name="confirmpassword" onChange={this.handleChange} required /> | ||
<button disabled={this.state.error ? "true" : null} className="m-6 my-4 input-normal text-white" type="button" style={{ width: "88%", backgroundColor: "#519e8a", color: "white" }} className="submitbtn" onClick={this.handleSubmit}>Reset Password</button> | ||
<h2>{this.token}</h2> | ||
</Form.Group > | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
</Container> | ||
</div> | ||
|
||
); | ||
} | ||
} | ||
|
||
const mapStateToProps = state => ({ passwordReset: state.resetPassword }); | ||
export default connect(mapStateToProps, { resetPassword })(ResetPassword); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import axios from "axios"; | ||
import { BASE_URL } from "../actions/types"; | ||
|
||
const token = localStorage.getItem("token"); | ||
const urlPath = axios.create({ | ||
baseURL: `${BASE_URL}/`, | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `${token}` | ||
} | ||
}); | ||
|
||
export default urlPath; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
/* Import all your reducers here. | ||
* This will give a centralised point of access for all reducers. | ||
*/ | ||
import { combineReducers } from "redux"; | ||
import { requestPassword } from "./requestPasswordReset"; | ||
import { resetPassword } from "./resetPassword"; | ||
|
||
const rootReducer = combineReducers({ | ||
requestPassword, | ||
resetPassword, | ||
|
||
}); | ||
export default rootReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { EMAIL_SEND, FAIL_SEND } from "../actions/types"; | ||
|
||
export function requestPassword(state = {}, action) { | ||
switch (action.type) { | ||
case EMAIL_SEND: | ||
return { ...state, message: action.payload }; | ||
case FAIL_SEND: | ||
return { ...state, message: action.payload }; | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { RESET_REQUEST, RESET_SUCCESSFUL, RESET_FAILURE } from "../actions/types"; | ||
|
||
export function resetPassword(state = {}, action) { | ||
switch (action.type) { | ||
case RESET_REQUEST: | ||
return { ...state }; | ||
case RESET_SUCCESSFUL: | ||
return { ...state, message: action.payload }; | ||
case RESET_FAILURE: | ||
return { ...state, messsage: action.payload }; | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
import React, { Component } from 'react'; | ||
import Login from '../components/Login/Login'; | ||
import Home from '../components/Home/Home'; | ||
import { BrowserRouter, Route, Switch } from 'react-router-dom'; | ||
import RequestResetForm from "../components/requestReset"; | ||
import ResetPassword from "../components/resetPassword"; | ||
import { BrowserRouter, Route, Switch } from 'react-router-dom' | ||
import store from "../store/index"; | ||
import { Provider } from "react-redux"; | ||
|
||
const Approuter = () => ( | ||
<BrowserRouter> | ||
<div> | ||
<Switch> | ||
<Route path="/" component={Home} exact /> | ||
<Route path="/login" component={Login} /> | ||
</Switch> | ||
</div> | ||
</BrowserRouter> | ||
); | ||
<Provider store={store}> | ||
<BrowserRouter> | ||
<div> | ||
<Switch> | ||
<Route path='/' component={Home} exact /> | ||
<Route path='/login' component={Login} /> | ||
<Route path="/resetpassword" component={RequestResetForm} /> | ||
<Route path="/resetform/:token" component={ResetPassword} /> | ||
</Switch> | ||
</div> | ||
</BrowserRouter> | ||
</Provider> | ||
) | ||
|
||
|
||
export default Approuter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
footer{ | ||
position: absolute; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
} |
Oops, something went wrong.