DEV Community

Cover image for Flask Rest API -Part:5- Password Reset

Flask Rest API -Part:5- Password Reset

Paurakh Sharma Humagain on January 09, 2020

Part 5: Password Reset Howdy! In the previous Part of the series, we learned how to handle errors in Flask and send a meaningful error m...
Collapse
 
joehoeller profile image
joehoeller • Edited

The code in your github doesnt work when ran. Just returns server 500s after request is made in Postman.

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

I just tried running the code from GitHub and everything went well. Can you please provide me more details like, in which endpoint you got 500. And also the error message in the terminal.

Also, did you remember to run python smtp server with python -m smtpd -n -c DebuggingServer localhost:1025
?

Collapse
 
pat64j profile image
Patrick Adonteng

I run the smtp server but I still get the "ConnectionRefusedError"

I want to ask: do I have to run it in my virtual environment? or in my normal mac terminal?

Thread Thread
 
paurakhsharma profile image
Paurakh Sharma Humagain

It doesn't have to be in your virtual environment. Just remember not to close the terminal running it.

Collapse
 
yosefco3 profile image
Yosef Cohen • Edited

I had the same problem.
My mistake was that I defined the email settings in .env file, and i forgot to import that settings to my app file.
And I still got that error, until I declared my :
mail = Mail(app)
after all that imported settings.

Collapse
 
tallmyr profile image
Simon Tallmyr

Another correction on this for newer releases of various packages.

In reset_password.py, you have the line:
user_id = decode_token(reset_token)["identity"]

The version of flask-jwt-extended that I have (4.0.2) that must instead read:
user_id = decode_token(reset_token)["sub"]

Again, thanks so much for this guide, i'm learning so much!

Collapse
 
scarow profile image
Sam Carow

This tutorial is excellent! One thing to note, I had to add MAIL_SUPPRESS_SEND = False to get the HTML to appear on my localhost:1025 server.

I didn't follow this tutorial exactly since my app is already set up slightly differently, but thought this could help anyone else who isn't seeing output on their mail server.

Collapse
 
irfan87 profile image
Ahmad Irfan Mohammad Shukri

I have a few questions here since I have followed along with all your codes. I just wondering if the user reset their password with their registered email, let's say Gmail, does the user will receive it? Or should I set the Gmail's SMTP from the .env?

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

Yes you need to set Gmail SMTP configuration. This is only for local development purposes.

Collapse
 
skow0020 profile image
Colin Skow

The .env file has an error on this page. 'MAIL_SERVER: "localhost"' <<< THe colon needs to be '='.

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

Fixed it! Thank you 😊

Collapse
 
maccerata profile image
Maccerata

Hello, thank you for this great article. But when I've tested this uri: /api/auth/forgot with unknown email got error 500 InternalServerError.
So I've updated exception handling unknown e-mail address to:

    except User.DoesNotExist:
        raise EmailDoesnotExistsError
Enter fullscreen mode Exit fullscreen mode

Is the update in file reset_password.py correct? Seems to be, because now I'm getting error 400 instead of 500

But still wondering about casdade of errors before this one is rised. Is there better way to handle it?

Collapse
 
sm0ke profile image
Sm0ke

Nice & Useful

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

Thank you 😊

Collapse
 
jagdis01 profile image
jagdis01

Nice !great and very useful

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

Thank you so much 😊

Collapse
 
paurakhsharma profile image
Paurakh Sharma Humagain

Have a good day ☺️

Collapse
 
panditvijay profile image
Vijay Pandit

Awesome article!! I have one question If I have to read a value from .env file in windows platform, how to do that?

Collapse
 
justinwkukm profile image
Waqas Khalid Obeidy

echo %MAIL_PORT%
echo $MAIL_PORT