Updated password system

took way too long to figure it out because I had an error so here we go:

from getkey import getkey, keys
from replit import clear, db
from color import Fore as F,Style as S
#Fake password
username = ['']
list_1 = ['']
Character_Restriction_List = ['a','b','c','d','e','f','g','h','i','j','k','l','n','m','o','p','q','r','s','t','u','v','w','x','y','z','_','-','1','2','3','4','5','6','7','8','9','10','B','B','C','D','E','F','G','H','I','J','K','L','N','M','O','P','Q','R','S','T','U','V','W','X','Y','Z']
def check_username(username):
  global true
  true = False
  for i in username:
    if i not in Character_Restriction_List:
      print(f'Please remove the {i} in your name to proceed.')
      enter_to_continue()
      clear() 
      return False
  return True

  

  
options = [
  'Username: ', 'PassWord: ', 'Show Password', 'Hide password', 'Submit!'
]
def enter_to_continue():
  print(f'{S.BRIGHT}|{F.BLUE}Enter{F.WHITE}|To Continue')
  input()
  
# Real password
list_2 = ['']
list_3 = list_1


def Sign_In() -> None:
  global list_1, list_2, list_3, username,menu,show_hide,alert
  alert = False
  show_hide = False
  if show_hide:
  
    menu = [
    'Username: ', 'PassWord: ', 'Hide Password', 'Submit!'
  ]
  if show_hide == False:
    menu = [
  'Username: ', 'PassWord: ', 'Show Password', 'Submit!'
]
  opt_2 = ''
  Hello = False
  opt = ''
  selection = 0
  key = ''
  while True:
    try:
      if show_hide:

        menu = [
        'Username: ', 'PassWord: ', 'Hide Password', 'Submit!'
      ]
      if show_hide == False:
        menu = [
      'Username: ', 'PassWord: ', 'Show Password', 'Submit!'
    ]






      
      if key == keys.ENTER:
        if opt == 'PassWord: ':
          break
      clear()
      print('------------------------------------')
      print('             Sign In!')
      print('')
      for i in range(len(menu)):
        opt = menu[i]
        if i == selection:
          if opt == 'PassWord: ':
            opt_2 = "".join(list_2)
            if Hello:
              print(f'> {opt}{opt_2}')
            else:
              opt_2 = ''.join(list_1)
              print(f'> {opt}{opt_2}')

          elif opt == 'Username: ':
            if Hello:
              print(f'> {opt}{"".join(username)}')
            else:
              print(f'> {opt}{"".join(username)}')
          else:
                
              print(f'> {opt}')
    
        else:
          if opt == 'PassWord: ':
            if Hello:
              print(f'  {opt}{"".join(list_2)}')
            else:
              print(f'  {opt}{"".join(list_1)}')

          elif opt == 'Username: ':
            if Hello:
              print(f'  {opt}{"".join(username)}')
            else:
              print(f'  {opt}{"".join(username)}')
          else:
            print(f'  {opt}')

      key = getkey()
      string = key

      if key == keys.UP:

        selection = (selection - 1) % len(menu)
        if selection == -1:
          selection = (selection + len(menu) + 1) % len(menu)
      elif key == keys.DOWN:

        selection = (selection + 1) % len(menu)
        if selection > len(menu):
          selection = (selection - len(menu) - 1) % len(menu)
      if key == keys.UP or key == keys.DOWN:
        pass
      else:
        if selection == 0 or selection == 1:
          if key == keys.ENTER:
            alert = True
          else:
            pass

        if alert == True:
          alert = False
        
        elif alert == False:

      
          


            clear()
            
            if key == keys.ENTER and selection == 3:
              if ''.join(username) == '' or ''.join(list_2) == '':
                print('You have not entered a username or password')
                enter_to_continue()
                clear()
              elif len(list_2) <= 8:
                print('Your password must be atleast 8 characters')
                enter_to_continue()
                clear()
    

                  
                  
    
              else:
                clear()
                print('Signed in!')
                db['Name'] = ''.join(username)
                db['password'] = ''.join(list_2)
                break
            elif key == keys.ENTER and selection == 2 and menu[2] == 'Show Password':
              list_3 = list_2
              Hello = True
              show_hide = True
            elif key == keys.ENTER and selection == 2 and menu[2] == 'Hide Password':
              list_3 = list_1
              Hello = False
              show_hide = False
            if selection == 1:
              if key == keys.BACKSPACE:
                temp_var = list_1.pop(-1)
                temp_var = list_2.pop(-1)
              else:
                list_1 += '*'
                list_2 += string
            if selection == 0:
              if key == keys.BACKSPACE:
                try:
                  username.pop(-1)
                except:
                  clear()
              else:
                if string not in Character_Restriction_List:
                  pass
                else:
                  if string == keys.ENTER:
                    pass
                  else:
                    username += string
            clear()
    except:
      clear()
  

this used my version of replit db to do stuff
replit_database.py:

import os
import unittest
import json
import requests
from replit.database import AsyncDatabase
from replit import db



backup_file_path = 'backup.json'


def create_backup():
    """Create a backup of the Replit database and sync it."""
    backup_data = dict(db)
  
    with open(backup_file_path, 'w') as file:
        json.dump(backup_data, file, indent=2)
  
    sync_backup()


def load_backup():
    """Load the backup data into the Replit database."""
    if os.path.exists(backup_file_path):
        with open(backup_file_path, 'r') as file:
            backup_data = json.load(file)
        db.update(backup_data)


def save_backup():
    """Save the Replit database data to the backup file."""
    backup_data = dict(db)
    
    with open(backup_file_path, 'w') as file:
        json.dump(backup_data, file)


def sync_backup():
    """Sync the backup file with the Replit database."""
    with open(backup_file_path, 'r') as file:
        backup_data = json.load(file)
    
    db.update(backup_data)
    

class SyncDatabase(dict):
    """Subclass of dict that syncs with a JSON backup file."""

    def __setitem__(self, key, value):
        """Set a key-value pair and sync with the backup file."""
        super().__setitem__(key, value)
        sync_backup()

    def __delitem__(self, key):
        """Delete a key-value pair and sync with the backup file."""
        super().__delitem__(key)
        sync_backup()


class TestSyncDatabase(unittest.TestCase):
    """Integration tests for syncing a backup file with the Replit database."""

    def setUp(self) -> None:
        """Grab a JWT for all the tests to share."""
        if "REPLIT_DB_URL" in os.environ:
            self.db = AsyncDatabase(os.environ["REPLIT_DB_URL"])
        else:
            password = os.environ["PASSWORD"]
            req = requests.get(
                "https://database-test-jwt.kochman.repl.co", auth=("test", password)
            )
            url = req.text
            self.db = AsyncDatabase(url)

        # nuke whatever is already here
        for k in self.db.keys():
            del self.db[k]

    def tearDown(self) -> None:
        """Nuke whatever the test added."""
        for k in self.db.keys():
            del self.db[k]

    def test_sync_backup(self) -> None:
        """Test syncing the backup file with the Replit database."""
        # Create a backup
        backup_data = dict(self.db)
        with open(backup_file_path, 'w') as file:
            json.dump(backup_data, file, indent=2)

        # Sync the backup with the Replit database
        with open(backup_file_path, 'r') as file:
            backup_data = json.load(file)
        self.db.update(backup_data)

        # Perform necessary tests
        self.assertEqual(self.db["test-key"], "value")

        # Update the Replit database and save the changes to the backup file
        self.db["test-key"] = "new-value"
        backup_data = dict(self.db)
        with open(backup_file_path, 'w') as file:
            json.dump(backup_data, file)

        # Sync the backup with the Replit database again
        with open(backup_file_path, 'r') as file:
            backup_data = json.load(file)
        self.db.update(backup_data)

        # Perform necessary tests again
        self.assertEqual(self.db["test-key"], "new-value")


if __name__ == '__main__':
    db = SyncDatabase(db)
    create_backup()
    unittest.main()

note: you can just use from replit import db and remove the #from replit_database import db,create_backup… etc.
Because its my version of doing it.So this is optional.

1 Like