Guest User

Untitled

a guest
Dec 29th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. @app.route("/reset_password/<token>", methods=['GET', 'POST'])
  2. def reset_token(token):
  3. if current_user.is_authenticated:
  4. return redirect(url_for('homepage'))
  5. user = User.verify_reset_token(token)
  6. if user is None:
  7. flash('That is an invalid or expired token', 'warning')
  8. return redirect(url_for('reset_request'))
  9. form = ResetPasswordForm()
  10. if form.validate_on_submit():
  11. user.password = hashed_password
  12. user.password =generate_password_hash(form.password.data, method='sha256')
  13.  
  14. flash('Your password has been updated! You are now able to log in', 'success')
  15. return redirect(url_for('login'))
  16. return render_template('reset_token.html', title='Reset Password', form=form)
  17.  
  18. class User(UserMixin, db.Document):
  19. meta = {'collection': 'users'}
  20. email = db.StringField(max_length=35)
  21. name = db.StringField(max_length=35)
  22. surname = db.StringField(max_length=35)
  23. password = db.StringField()
  24. username = db.StringField()
  25. image_file = db.StringField(nullable=False, default='static/img/default.jpg')
  26.  
  27. skills = db.ListField(db.StringField())
  28.  
  29. def get_reset_token(self, expires_sec=1800):
  30. s = Serializer(app.config['SECRET_KEY'], expires_sec)
  31. return s.dumps({'email': self.email}).decode('utf-8')
  32.  
  33. @staticmethod
  34. def verify_reset_token(token):
  35. s = Serializer(app.config['SECRET_KEY'])
  36. try:
  37. email = s.loads(token)['email']
  38. except:
  39. return None
  40. return User.objects(email='email')
  41.  
  42. def __repr__(self):
  43. return f"User('{self.email}', '{self.image_file}')"
  44.  
  45. class RequestResetForm(FlaskForm):
  46. email = StringField('Email', validators=[validators.DataRequired()])
  47. submit = SubmitField('Request Password Reset')
  48.  
  49. def validate_email(self, email):
  50. user = User.objects(email=email.data).first()
  51. if user is None:
  52. raise ValidationError('There is no account with that email. You must register first.')
  53.  
  54.  
  55. class ResetPasswordForm(FlaskForm):
  56. password = PasswordField('New Password', validators=[validators.DataRequired()])
  57. confirm_password = PasswordField('Confirm Password', validators=[validators.DataRequired(), validators.EqualTo('password')])
  58. submit = SubmitField('Reset Password')
Add Comment
Please, Sign In to add comment