Skip to content

Commit 2e95208

Browse files
committed
integrated forgot password
1 parent a10c001 commit 2e95208

File tree

4 files changed

+68
-28
lines changed

4 files changed

+68
-28
lines changed

src/actions/authAction.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SET_CURRENT_USER, GET_USER_PROFILE } from './types';
1+
import { SET_CURRENT_USER, GET_USER_PROFILE, PASSWORD_SUCCESSFULLY_CHANGED, PASSWORD_CHANGE_REQUEST_SUCCESS } from './types';
22
import axios from 'axios';
33
import { setAuthToken } from '../utils/setAuthToken';
44
import jwt_decode from 'jwt-decode';
@@ -57,13 +57,17 @@ export const loginUser = (userInfo, history) => async (dispatch) => {
5757
// forgot password
5858
export const forgotPassword = (email) => async (dispatch) => {
5959
try {
60-
const res = await axios.post('/user/password_reset', email);
60+
const res = await axios.patch('/user/password_reset/request/', email);
6161
dispatch(setRequestStatus(false));
6262

6363
if(res.status === 200){
6464
dispatch(setRequestStatus(true));
6565
console.log("Forgot password request sent!!");
6666
forgotPasswordToken = res.data.token;
67+
dispatch({
68+
type: PASSWORD_CHANGE_REQUEST_SUCCESS,
69+
payload: res.data.token
70+
})
6771
}
6872

6973
} catch (error) {
@@ -74,13 +78,17 @@ export const forgotPassword = (email) => async (dispatch) => {
7478
// update password
7579
export const changePassword = (passObj) => async (dispatch) => {
7680
try {
77-
const res = await axios.post(`/user/password_reset/${forgotPasswordToken}`, passObj);
81+
const res = await axios.patch(`/user/password_reset/${forgotPasswordToken}`, passObj);
7882
dispatch(setRequestStatus(false));
7983

8084
if(res.status === 200){
8185
dispatch(setRequestStatus(true));
8286
console.log("Password updated!", res.data);
8387
// show password updated notification from here
88+
dispatch({
89+
type: PASSWORD_SUCCESSFULLY_CHANGED,
90+
payload: res.data.updated
91+
})
8492
}
8593

8694
} catch(error) {

src/actions/types.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ export const GET_ALL_PINNED_POSTS = "GET_ALL_PINNED_POSTS";
3131
export const GET_EVENT_BY_ID = "GET_EVENT_BY_ID";
3232
export const GET_ADMIN = "GET_ADMIN";
3333
export const GET_COMMENTS_OF_A_POST = "GET_COMMENTS_OF_A_POST";
34-
export const GET_SINGLE_PROJECT = "GET_SINGLE_PROJECT";
34+
export const GET_SINGLE_PROJECT = "GET_SINGLE_PROJECT";
35+
export const PASSWORD_CHANGE_REQUEST_SUCCESS = "PASSWORD_CHANGE_REQUEST_SUCCESS";
36+
export const PASSWORD_SUCCESSFULLY_CHANGED = "PASSWORD_SUCCESSFULLY_CHANGED";

src/common/Popups.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { MdVerifiedUser } from 'react-icons/md';
55
import { FaUserSlash } from 'react-icons/fa';
66
import { connect } from 'react-redux';
77
import { forgotPassword, changePassword } from '../actions/authAction';
8+
import { ToastContainer, toast } from "react-toastify";
9+
import "react-toastify/dist/ReactToastify.css";
810

911
class Popups extends Component {
1012
constructor(props){
@@ -20,16 +22,26 @@ class Popups extends Component {
2022
option: "",
2123
optionValue: "",
2224
newPass: "",
23-
cnfPass: ""
25+
cnfPass: "",
26+
requested: false
2427
}
2528
}
2629

2730
componentWillReceiveProps(nextProps){
31+
console.log('nextProps in common ', nextProps)
2832
this.setState({
2933
show: nextProps.modalShow,
3034
option: nextProps.option,
3135
optionValue: nextProps.optionValue
3236
});
37+
const { currentStep, requested } = this.state
38+
if (nextProps.auth.resetPassReq !== null && requested) {
39+
toast.success('Link sent to your registered email!')
40+
let step = currentStep >= 2 ? 2 : currentStep + 1
41+
this.setState({
42+
currentStep: step
43+
})
44+
}
3345
}
3446

3547
handleClose = () => {
@@ -54,8 +66,8 @@ class Popups extends Component {
5466

5567
changePassword = (e) => {
5668
e.preventDefault();
57-
const { newPass } = this.state;
58-
const { auth, changePassword, status } = this.props;
69+
const { newPass, success } = this.state;
70+
const { auth, changePassword } = this.props;
5971

6072
const passObj = {
6173
password: newPass,
@@ -66,30 +78,21 @@ class Popups extends Component {
6678
changePassword(passObj);
6779

6880
// show notification on sidebar if done successfully
69-
if(status.success) {
81+
if(success) {
7082
console.log("Successfully changed the password!");
7183
}
7284
}
7385

7486
forgotPasswordRequest = () => {
7587
const { email } = this.state;
76-
const { forgotPassword, status } = this.props;
88+
const { forgotPassword } = this.props;
7789

7890
console.log("forgot password request sending...")
79-
forgotPassword(email);
80-
81-
let { currentStep } = this.state;
82-
// move to next step if forgot password request successful
83-
if(status.success){
84-
currentStep = currentStep >= 2 ? 2 : currentStep + 1
85-
this.setState({
86-
currentStep: currentStep
87-
})
88-
} else {
89-
// show error message in popup
90-
console.log("Something went wrong!!");
91+
const emailObj = {
92+
email: email
9193
}
92-
94+
this.setState({ requested: true })
95+
forgotPassword(emailObj);
9396
}
9497

9598

@@ -305,12 +308,25 @@ function Step2(props) {
305308
if (props.currentStep !== 2) {
306309
return null
307310
}
308-
return(
311+
return (
309312
<React.Fragment>
310-
<div className="form-group">
311-
<label htmlFor="password">Check your email to get the link of reset the password. If it doesnot appear within few minutes, check the spam folder.</label>
312-
313-
</div>
313+
<ToastContainer
314+
position="top-right"
315+
autoClose={5000}
316+
hideProgressBar={false}
317+
newestOnTop={false}
318+
closeOnClick
319+
rtl={false}
320+
pauseOnFocusLoss
321+
draggable
322+
pauseOnHover
323+
/>
324+
<div className="form-group">
325+
<label htmlFor="password">
326+
Check your email to get the link of reset the password. If it doesnot
327+
appear within few minutes, check the spam folder.
328+
</label>
329+
</div>
314330
</React.Fragment>
315331
);
316332
}

src/reducers/authReducer.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { SET_CURRENT_USER, RESPONSE_MSG, SET_ADMIN, BLOCK_USER, UNBLOCK_USER, REMOVE_USER } from "../actions/types"
1+
import { SET_CURRENT_USER, RESPONSE_MSG, SET_ADMIN, BLOCK_USER, UNBLOCK_USER, REMOVE_USER, PASSWORD_CHANGE_REQUEST_SUCCESS, PASSWORD_SUCCESSFULLY_CHANGED } from "../actions/types"
22

33
const initialState = {
44
// make it false later, default is set to true so that contributors don't need to login for test
55
isAuthenticated: true,
66
isAdmin: false,
77
isBlocked: false,
88
isRemoved: false,
9+
resetPassReq: null,
10+
passUpdated: false,
911
user: {},
1012
response_msg: ""
1113
}
@@ -19,6 +21,18 @@ export default (state = initialState, action) => {
1921
user: action.payload
2022
}
2123
}
24+
case PASSWORD_CHANGE_REQUEST_SUCCESS: {
25+
return {
26+
...state,
27+
resetPassReq: action.payload
28+
}
29+
}
30+
case PASSWORD_SUCCESSFULLY_CHANGED: {
31+
return {
32+
...state,
33+
passUpdated: action.payload
34+
}
35+
}
2236
case BLOCK_USER: {
2337
return {
2438
...state,

0 commit comments

Comments
 (0)