'The parameter in the URL is returning undefined
I'm trying to reset the password via email, until now I managed to send the link and register a token in the database. Now I want to check that token, too see if it's the same one, but I'm getting undefined.
Here is what I have in the database:
userRouter.get('/reset/:token', expressAsyncHandler(async (req, res) => {
console.log(req.params)
const user = await User.findOne({
resetPasswordToken: req.params.token,
resetPasswordExpires: {
$gt: Date.now(),
},
});
if (user) {
console.log('token matched')
res.status(200).send({
message: 'password reset link ok'
})
} else {
console.log('password reset link is invalid or has expired');
res.json('password reset link is invalid or has expired');
}
})
);
The console.log(req.params) is returning undefined
I will also post the frontend code, because I don't really know where the problem is.
In the userActions.js:
export const checktoken = (token) => async (dispatch) => {
dispatch({ type: USER_RESET_PASSWORD_REQUEST, payload: { token } });
try {
const { data } = await Axios.get(`/api/users/reset/${token}`);
dispatch({ type: USER_RESET_PASSWORD_SUCCESS, payload: data });;
localStorage.setItem('userInfo', JSON.stringify(data));
} catch (error) {
dispatch({
type: USER_RESET_PASSWORD_FAIL,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
});
}
};
In the userReducer:
export const userForgotPasswordReducer = (state = {}, action) => {
switch (action.type) {
case USER_FORGOT_PASSWORD_REQUEST:
return { loading: true };
case USER_FORGOT_PASSWORD_SUCCESS:
return { loading: false, token: action.payload };
case USER_FORGOT_PASSWORD_FAIL:
return { loading: false, error: action.payload };
default:
return state;
}
};
And in the resetPassword:
const userCheckToken = useSelector((state) => state.userCheckToken);
const params = useParams();
const tokennr = params.token;
const { loading, error } = userCheckToken;
const dispatch = useDispatch();
useEffect(() => {
dispatch(checktoken());
}, [dispatch, tokennr]);
I wonder, where the problem might be, because I can't find a way to get the token in the database.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
