Please read the below code and add code, do changes as mentioned mentioned below: 1.…
- User
Please read the below code and add code, do changes as mentioned mentioned below:
-
Make the user login by OTP from the entered mobile number, do the login OTP verification (use pinnacle SMS for OTP).
-
Put condition to check if the number is present in the m_patient table, if true got ahead with sending OTP, if false then return false. Code- @Register_Blueprint.route('/signUp', methods=['POST','GET']) def signUp(): session = Session() try: if(flask.request.method == 'POST'):
RequestIp = hashlib.md5((request.remote_addr).encode()) RequestIp = RequestIp.hexdigest() request_json = request.get_json() if(request_json!='' and request_json!=None): Mobile = request_json.get('mobile') if(Mobile !='' and Mobile!=None): pswrd= hashlib.sha1(Password.encode()) ChcekPWd = pswrd.hexdigest() CheckMobile = Common_Function.CommonFun.verifyMobile(Mobile) if(CheckMobile==0): Insert = Model.models.Application.M_PatientsDtl() if(Mobile !='' and Mobile!=None): Insert.MPD_Mobile = Mobile if(Name !='' and Name!=None): Insert.MPD_Name = Name if(Email !='' and Email!=None): Insert.MPD_Username = Mobile Insert.MPD_hashedPassword = ChcekPWd Insert.MPD_AddDate = datetime.datetime.now() Insert.MPD_AddIP = request.remote_addr Insert.MPD_User = 0 session.add(Insert) session.commit() session.close() PatientNo = session.query(Model.models.Application.M_PatientsDtl.MPDID).order_by( Model.models.Application.M_PatientsDtl.MPDID.desc() ).all() output = [] output.append({'success':'Registered Successfully','PatientId':PatientNo[0].MPDID }) return jsonify(result=output) else: return jsonify({'error':'Mobile No already registered'}) else: return jsonify({'error':'Not Matched'}) else: return jsonify({'error':'JSON not available'}) else: return jsonify({'error':'Method is not allowed'})except Exception as identifier: Logger.error(identifier) finally: session.close()
-
- Assistant
Here is the updated code with added functionality for OTP login using a hypothetical service "pinnacleSMS" for sending OTP and validation to check if the number is present in the
m_patienttable. Please keep in mind that this code is a general representation of the desired functionality; thesend_smsfunction should be replaced with your actual SMS sending logic. Additionally, the OTP verification step assumes that a function for verifying the code exists in a library or is implemented elsewhere in your codebase. Replaceverify_otp_codewith your own verification method.from flask import Blueprint, request, jsonify import hashlib from sqlalchemy.orm import Session import datetime import Model.models import Common_Function.CommonFun from pinnacle_sms_client import send_sms, verify_otp_code # placeholder for actual SMS and OTP verification functions Register_Blueprint = Blueprint('register_blueprint', __name__) @Register_Blueprint.route('/signUp', methods=['POST', 'GET']) def signUp(): session = Session() try: if request.method == 'POST': request_json = request.get_json() if request_json: Mobile = request_json.get('mobile') if Mobile: # Check if the mobile number exists in the m_patient table existing_patient = session.query(Model.models.Application.M_PatientsDtl).filter_by(MPD_Mobile=Mobile).first() if existing_patient: # If the number exists, initiate the OTP process otp_code = send_sms(Mobile) # Replace with actual method to send SMS # Store the sent OTP code in a secure manner for later verification (in a database or cache) session['otp_code'] = otp_code # This is just for illustration; do not use session in production return jsonify({'success':'OTP sent successfully'}) else: # If the number does not exist, return false return jsonify({'error': 'Mobile number not registered'}) else: return jsonify({'error': 'Invalid mobile number'}) else: return jsonify({'error': 'JSON not available'}) else: return jsonify({'error': 'Method is not allowed'}) except Exception as identifier: # Assuming Logger is defined elsewhere in your code Logger.error(identifier) # Replace with your logging mechanism finally: session.close() # Assuming you have a separate route to verify OTP @Register_Blueprint.route('/verifyOTP', methods=['POST']) def verify_otp(): session = Session() try: request_json = request.get_json() if request_json: Mobile = request_json.get('mobile') otp_code = request_json.get('otp_code') # This should be the code the user received # Check against the stored OTP code if 'otp_code' in session and verify_otp_code(otp_code, session['otp_code']): # OTP verification successful, proceed with your next steps del session['otp_code'] # Ensure to clear the OTP code after verification return jsonify({'success': 'OTP verified successfully'}) else: return jsonify({'error': 'Invalid OTP'}) else: return jsonify({'error': 'JSON not available'}) except Exception as identifier: Logger.error(identifier) finally: session.close()Note:
pinnacle_sms_clientis a placeholder for the actual library or service you will use to send and verify OTP codes. Replacesend_smsandverify_otp_codewith the actual methods provided by the library or your implementation.- In an actual application, storing the OTP code in a session like
session['otp_code'] = otp_codeis not safe. Instead, you should use a secure server-side storage such as a database or cache with proper expiration and securities. - The above method
verify_otpis just a representation of the OTP verification process. Implement the necessary security measures to ensure the verification is secure and user-specific. - Handle any exceptions or errors appropriately.
- Using sessions to store sensitive information (like OTP codes) is not a recommended practice for production. You should consider using a more secure and scalable approach, such as token-based authentication or database/cache storage with a one-time expiration.
- In this example, we have omitted implementing the actual PINNACLE SMS service and have presumed the functions
send_smsandverify_otp_codeexist. You will need to replace these placeholders with the actual SMS service logic in your real application.
- Assistant
It seems like your last message was empty. If you have any more questions or need further assistance with the code provided, please feel free to ask. I'm here to help!