Skip to content

Commit

Permalink
modified verify email api
Browse files Browse the repository at this point in the history
  • Loading branch information
n1lby73 committed Dec 16, 2023
1 parent ccd84d4 commit 37577e4
Showing 1 changed file with 121 additions and 19 deletions.
140 changes: 121 additions & 19 deletions web-ui/webApp/apiRoute.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def post(self):

"Success": "new user created and otp sent to mail",
"email": email

})

except Exception as e:
Expand All @@ -378,38 +378,27 @@ def post(self):
db.session.close()

class verifyEmailApi(Resource):
@jwt_required()
def __init__(self):

self.parser = reqparse.RequestParser()
self.parser.add_argument("otp", required=True)

def put(self):
def post(self):

global genOtpStartTime
try:
self.parser.add_argument("otp", required=True)
self.parser.add_argument("email", required=True)

user = get_jwt_identity()
email = user["email"]

except ExpiredSignatureError:
global genOtpStartTime

# Handle the expired token error
return jsonify({"error": "Token has expired. Please re-authenticate."}), 401

except:

return jsonify({"error": "Invalid token."}), 401
args = self.parser.parse_args()
user_otp = args["otp"]
email = args["email"]

logged_user = users.query.filter_by(email=email).first()

if logged_user.verifiedEmail == "True":

return {"Msg":"email verification already completed"}

args = self.parser.parse_args()
user_otp = args["otp"]

if logged_user.otp != user_otp:

return ({"Error": "Invalid otp entered"})
Expand Down Expand Up @@ -458,7 +447,120 @@ def put(self):
finally:

db.session.close()

def put(self):

self.parser.add_argument("email", required=True)
self.parser.add_argument("newEmail", required=True)

global genOtpStartTime

args = self.parser.parse_args()
updatedEmail = args["newEmail"]
email = args["email"]

try:

logged_user = users.query.filter_by(email=email).first()

if logged_user:

otp, otpStartTime = genOTP()

genOtpStartTime = otpStartTime

msg = Message('Email Verification', recipients=[updatedEmail])
msg.html = render_template("emailVerification.html", otp=otp)

try:

mail.send(msg)

except:

return ({"Error": "Invalid email format"})

logged_user.otp = otp
logged_user.email = updatedEmail

try:

db.session.commit()

return ({

"Success": "email update completed and otp sent to new email",
"email": email

})

except Exception as e:

db.session.rollback()
error_message = str(e)

return jsonify({"Error": "update unsuccessfull", "Details": str(e)}), 500

finally:

db.session.close()

except:

return ({"Error":"Invalid old email"}), 400

def get(self):

self.parser.add_argument("email", required=True)

try:
global genOtpStartTime

email = args["email"]

logged_user = users.query.filter_by(email=email).first()

if logged_user.verifiedEmail == "True":

return {"Msg":"email verification already completed"}

otp, otpStartTime = genOTP()

genOtpStartTime = otpStartTime

msg = Message('Industrial IOT email Verification', recipients=[email])
msg.html = render_template("emailVerification.html", otp=otp)

try:

mail.send(msg)

except:

return ({"Error": "Invalid email format"}), 400

logged_user.otp = otp

try:
db.session.commit()

return ({"status": "otp sent to mail"})

except Exception as e:

db.session.rollback()
error_message = str(e)

return jsonify({"Error": "could not send to mail", "Details": str(e)}), 500

finally:

db.session.close()

except:

return ({"error": "OTP generation failed"}), 500

class genOtpApi(Resource):
@jwt_required()
def get(self):
Expand Down

0 comments on commit 37577e4

Please sign in to comment.