def auth_login(email, password): """ Logs user into flockr (tracks account in database). """ email_in_database = check_email_in_database(email) validate_email_regex = check_email_regex(email) if not email_in_database or not validate_email_regex: raise InputError('Invalid email or email already in database') database = DataBase() users = database.database_dict['users'] valid_user = {} for user in users: if user['email'] == email and user['password'] == password: valid_user = user break if not valid_user: raise InputError('Invalid email or email already in database') # Add user to active_user database active_users = database.database_dict['active_users'] token_str_1 = f"active_user_{valid_user['u_id']}" token_str_2 = f"{database.database_dict['no_active_users']}" token_str = token_str_1 + "_" + token_str_2 token_str = database.token_generator(token_str) return_dict = {'u_id': valid_user['u_id'], 'token': token_str} active_users.append(return_dict) database.database_dict['no_active_users'] += 1 database.close() return return_dict
def auth_register(email, password, name_first, name_last): """ Create user account based on valid info. """ # Validate entries valid_email = validate_email(email) valid_password = validate_password(password) valid_names = validate_names(name_first, name_last) # If every input is valid if not valid_email or not valid_names or not valid_password: raise InputError('Invalid email or names or password') # Open database for editing database = DataBase() # Add user to database users = database.database_dict['users'] # Give user specific permissions permissions = USER_PERMISSIONS if database.database_dict['no_users'] == 0: permissions = OWNER_PERMISSIONS user_dict = { 'handle_str': create_handle(name_first, name_last), 'email': email, 'password': password, 'name_first': name_first, 'name_last': name_last, 'permissions': permissions, 'u_id': len(users), 'profile_img_url': None, } users.append(user_dict) database.database_dict['no_users'] += 1 # Add user to active user active_users = database.database_dict['active_users'] token_str = f"active_user_{user_dict['u_id']}_{database.database_dict['no_active_users']}" token_str = database.token_generator(token_str) return_dict = {'u_id': user_dict['u_id'], 'token': token_str} database.database_dict['no_active_users'] += 1 active_users.append(return_dict) # Close database to save changes database.close() return return_dict