Skip to content

Commit

Permalink
account: handle account signup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mane Motha committed Jun 2, 2022
1 parent 2031496 commit 6d72698
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
import json
import sys
import os

from logic import *
2 changes: 2 additions & 0 deletions logic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .account import *
from .variables import *
13 changes: 13 additions & 0 deletions logic/account/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import websockets
import asyncio
import bcrypt
import datetime
import sys
import os
import json
import sqlite3

# modules
from logic import *
from logic.variables import *
from .account import *
35 changes: 35 additions & 0 deletions logic/account/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import json
import sqlite3

from .__init__ import *


class Account:

def __init__(self, json_packet: dict):
self.user_account = json_packet
self.user_profile = json_packet['profile']
self.email: str = self.user_account['email']
self.username: str = self.user_account['username']
self.password: str = self.user_account['password']

async def signup(self):
try:
if not os.path.exists(database_directory):
os.makedirs(database_directory)

# create and open database file
database = sqlite3.connect(f'{database_directory}/accounts.db')
cursor = database.cursor()

# create user table from username
cursor.execute(f"""
CREATE TABLE {self.username} (account json) """)

# add values
cursor.execute(f"insert into {self.username} values (?)", [json.dumps(self.user_account)])
database.commit()
return {"result": account_generated_true}

except sqlite3.OperationalError:
return {"result": account_exists_true}
44 changes: 39 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import json

import websockets.exceptions

from __init__ import *


Expand All @@ -22,9 +18,47 @@ async def index(websocket):

try:
namespace = json_packet['namespace']
user_account = json_packet['account']
user_profile = user_account['profile']

# handle namespace connection
if namespace:
pass

# login
if namespace == '/':
print('login')

# signup
elif namespace == '/signup':
if len(user_account['username']) >= 5:
if user_account['email']:
TODO: "email address verification"
if len(user_account['password']) >= 8:
signup_result: dict = await Account(user_account).signup()

if user_account['username'] != "":
if signup_result['result'] == account_exists_true:
await websocket.send(str(signup_result))
await websocket.close()
elif signup_result['result'] == username_unwanted_character:
await websocket.send(str(signup_result))
await websocket.close()
else:
await websocket.send(str(signup_result))
await websocket.close()
else:
# password < 8
await websocket.send(str({"result": "password is length less than 8"}))
await websocket.close()
else:
# email is empty
await websocket.send(str({"result": "email is empty"}))
await websocket.close()
else:
# username < 5
await websocket.send(str({"result": "username is length less than 5"}))
await websocket.close()

else:
await websocket.send(str({'result': 'unknown namespace'}))
await websocket.close()
Expand Down

0 comments on commit 6d72698

Please sign in to comment.