HackerNewsIndia / Universal

2 stars 0 forks source link

Forgot password needs to be functional. This link is important for MVP1. #2

Open athishsreeram opened 1 month ago

athishsreeram commented 1 month ago

Come up with a Simple screen similar to registration for forgot password

With this flow

Forgot password

  1. Enter email id (Ask user to enter his email id)
  2. Send otp and show OTP verification screen (reuse API and OTP Verification logic & screen )
  3. On Confirm otp and show the username with new password update input field (use below comment API to update the password)
  4. Override and update the current password
athishsreeram commented 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)