def generate_otp(uid): import string import random # Takes random choices from # ascii_letters and digits try: generate_pass = ''.join( [random.choice(string.digits) for n in range(6)]) doc_ref = db.collection(u'users').document(uid) doc_ref.update({'verification_code': generate_pass}) print(generate_pass) return generate_pass except firebase_admin._auth_utils.UserNotFoundError as ex: from Pages.UserAuthentication.Exceptions import User_not_Found User_not_Found() return False except Exception as ex: messagebox.showerror( 'Error', 'Oops!! Something went wrong!!\nTry again later.') print('Exception Occurred which is of type :', ex.__class__.__name__) y = input('If you want to see Traceback press 1 : ') if y == '1': traceback.print_exc() return False
def get_user_by_phone_number(phone): """ Returns a user object that is dictionary of the user with attributes: display_name , email , password, phone_number """ from firebase_admin import auth try: user = auth.get_user_by_phone_number(phone) doc = db.collection(u'users').document(user.uid) doc = doc.get().to_dict() return doc except firebase_admin._auth_utils.UserNotFoundError as ex: from Pages.UserAuthentication.Exceptions import User_not_Found User_not_Found() return False except Exception as ex: messagebox.showerror( 'Error', 'Oops!! Something went wrong!!\nTry again later.') print('Exception Occurred which is of type :', ex.__class__.__name__) y = input('If you want to see Traceback press 1 : ') if y == '1': traceback.print_exc() return False
def register_user(username, email, phone_number, password): """ Returns user uid if successfully registered else returns false """ from firebase_admin import auth try: if username == '' or email == '' or phone_number == '': raise Exception('Some of fields were found to be empty') elif len(password) <= 6: raise Exception('Password length less then equal to 6') user = auth.create_user( email=email, phone_number='+91' + phone_number, password=password, display_name=username, email_verified=False, ) doc_ref = db.collection(u'users').document(user.uid) doc_ref.set({ 'email': email, 'phone_number': '+91' + phone_number, 'password': password, 'display_name': username, 'email_verified': False }) print('Successfully created new user: {0}'.format(user.uid)) return user.uid except firebase_admin._auth_utils.EmailAlreadyExistsError as ex: from Pages.UserAuthentication.Exceptions import Email_already_exists Email_already_exists() return False except firebase_admin._auth_utils.PhoneNumberAlreadyExistsError as ex: from Pages.UserAuthentication.Exceptions import Phone_already_exists Phone_already_exists() return False except firebase_admin._auth_utils.UserNotFoundError as ex: from Pages.UserAuthentication.Exceptions import User_not_Found User_not_Found() return False except Exception as ex: messagebox.showerror( 'Error', 'Oops!! Something went wrong!!\nTry again later.') print('Exception Occurred which is of type :', ex.__class__.__name__) y = input('If you want to see Traceback press 1 : ') if y == '1': traceback.print_exc() return False
def send_email_verification_otp(email): ''' :param otp: email: email of the user :return: bool ''' try: from firebase_admin import auth import smtplib user = auth.get_user_by_email(email) otp = generate_otp(user.uid) fromaddr = '[email protected].' toaddrs = email Text = 'Hello ' + user.display_name + ',\nEnter the following OTP to verify your email address. \nYour verification code is ' + otp + '\nIf you didn’t ask to verify this address, you can ignore this email.\nThanks,\nYour AmplifyTeam' subject = 'Email Verification' username = '******' password = '******' print('i ma in the funtion') message = 'Subject: {}\n\n{}'.format(subject, Text) message = message.encode() server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.login(username, password) server.sendmail(fromaddr, toaddrs, message) server.quit() except firebase_admin._auth_utils.UserNotFoundError as ex: from Pages.UserAuthentication.Exceptions import User_not_Found User_not_Found() return False except Exception as ex: messagebox.showerror( 'Error', 'Oops!! Something went wrong!!\nTry again later.') print('Exception Occurred which is of type :', ex.__class__.__name__) y = input('If you want to see Traceback press 1 : ') if y == '1': traceback.print_exc() return False
def sign_in_with_email_and_password(email, password): """ Returns boolean True if user is signed in succesfully else false """ from os import path try: if path.exists('user'): f = open('user', 'r') doc = get_user(f.readline()) f.close() return doc from firebase_admin import auth user = auth.get_user_by_email(email) doc = get_user_by_email(email) if doc['email'] == email and doc['password'] == password: f = open('user', "w+") f.write(user.uid) return doc else: from Pages.UserAuthentication.Exceptions import Invalid_credentials Invalid_credentials() # return False except firebase_admin._auth_utils.UserNotFoundError as ex: from Pages.UserAuthentication.Exceptions import User_not_Found User_not_Found() return False except Exception as ex: messagebox.showerror( 'Error', 'Oops!! Something went wrong!!\nTry again later.') print('Exception Occurred which is of type :', ex.__class__.__name__) y = input('If you want to see Traceback press 1 : ') if y == '1': traceback.print_exc() return False