Pijush2023 commited on
Commit
78029ec
·
verified ·
1 Parent(s): 1133fd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -853,6 +853,14 @@ def initialize_database():
853
  conn.commit()
854
  conn.close()
855
 
 
 
 
 
 
 
 
 
856
  # Hashing and checking passwords
857
  def hash_password(password):
858
  return bcrypt.hashpw(password.encode(), bcrypt.gensalt())
@@ -861,8 +869,9 @@ def check_password(password, hashed):
861
  return bcrypt.checkpw(password.encode(), hashed)
862
 
863
  # User signup and login functions
864
-
865
  def signup_user(email, password):
 
 
866
  conn = sqlite3.connect('user_data.db')
867
  c = conn.cursor()
868
  try:
@@ -875,6 +884,8 @@ def signup_user(email, password):
875
  conn.close()
876
 
877
  def login_user(email, password, state):
 
 
878
  conn = sqlite3.connect('user_data.db')
879
  c = conn.cursor()
880
  c.execute("SELECT password FROM users WHERE email = ?", (email,))
@@ -886,9 +897,9 @@ def login_user(email, password, state):
886
  else:
887
  return "Invalid email or password!", state
888
 
889
- def logout(state):
890
- message, state = logout_user(state)
891
- return message, state, "Login"
892
 
893
  # Define the necessary functions
894
  def signup(email, password):
@@ -898,6 +909,10 @@ def login(email, password, state):
898
  message, state = login_user(email, password, state)
899
  return message, state, "Logout" if state['authenticated'] else "Login"
900
 
 
 
 
 
901
  # Initialize database
902
  initialize_database()
903
 
@@ -926,7 +941,7 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
926
  login_button = gr.Button("Login")
927
  login_output = gr.Textbox(interactive=False, label="Output")
928
  login_button.click(login, inputs=[login_email, login_password, state], outputs=[login_output, state, login_button])
929
- login_button.click(logout, inputs=[state], outputs=[login_output, state, login_button], show_progress=False)
930
 
931
  state = gr.State()
932
  chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
 
853
  conn.commit()
854
  conn.close()
855
 
856
+ # Email validation
857
+ def is_valid_email(email):
858
+ regex = r'^\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
859
+ if re.match(regex, email):
860
+ return True
861
+ else:
862
+ return False
863
+
864
  # Hashing and checking passwords
865
  def hash_password(password):
866
  return bcrypt.hashpw(password.encode(), bcrypt.gensalt())
 
869
  return bcrypt.checkpw(password.encode(), hashed)
870
 
871
  # User signup and login functions
 
872
  def signup_user(email, password):
873
+ if not is_valid_email(email):
874
+ return "Invalid email!"
875
  conn = sqlite3.connect('user_data.db')
876
  c = conn.cursor()
877
  try:
 
884
  conn.close()
885
 
886
  def login_user(email, password, state):
887
+ if not is_valid_email(email):
888
+ return "Invalid email!", state
889
  conn = sqlite3.connect('user_data.db')
890
  c = conn.cursor()
891
  c.execute("SELECT password FROM users WHERE email = ?", (email,))
 
897
  else:
898
  return "Invalid email or password!", state
899
 
900
+ def logout_user(state):
901
+ state['authenticated'] = False
902
+ return "Logged out successfully!", state
903
 
904
  # Define the necessary functions
905
  def signup(email, password):
 
909
  message, state = login_user(email, password, state)
910
  return message, state, "Logout" if state['authenticated'] else "Login"
911
 
912
+ def logout(state):
913
+ message, state = logout_user(state)
914
+ return message, state, "Login", "", ""
915
+
916
  # Initialize database
917
  initialize_database()
918
 
 
941
  login_button = gr.Button("Login")
942
  login_output = gr.Textbox(interactive=False, label="Output")
943
  login_button.click(login, inputs=[login_email, login_password, state], outputs=[login_output, state, login_button])
944
+ login_button.click(logout, inputs=[state], outputs=[login_output, state, login_button, login_email, login_password], show_progress=False)
945
 
946
  state = gr.State()
947
  chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)