Open athishsreeram opened 1 month ago
Sample API Code
from flask import Flask, jsonify
from flask_pymongo import PyMongo
from bson import ObjectId
import bcrypt
app = Flask(__name__)
# Initialize MongoClient
app.config["MONGO_URI"] = "mongodb+srv://@cluster0.mnugi39.mongodb.net/indian_hacker_news?retryWrites=true&w=majority&tlsAllowInvalidCertificates=true"
mongo = PyMongo(app)
# Access collection
collection = mongo.db.users
@app.route('/update_password/<user_id>', methods=['POST'])
def update_password(user_id):
# Get the new password from the request JSON
request_data = request.get_json()
new_password = request_data.get('new_password')
if not new_password:
return jsonify({"message": "New password not provided."}), 400
# Hash the new password
new_password_hashed = bcrypt.hashpw(new_password.encode('utf-8'), bcrypt.gensalt())
# Update document
result = collection.update_one({'_id': ObjectId(user_id)}, {"$set": {"password": new_password_hashed}})
if result.modified_count > 0:
updated_document = collection.find_one({'_id': ObjectId(user_id)})
return jsonify({"message": "Password updated successfully.", "updated_document": updated_document}), 200
else:
return jsonify({"message": "Failed to update password."}), 400
if __name__ == '__main__':
app.run(debug=True)
Come up with a Simple screen similar to registration for forgot password
With this flow
Forgot password