Skip to content
Snippets Groups Projects
Commit 1b7b3e44 authored by MaximeOLIVA's avatar MaximeOLIVA
Browse files

sign_out

parent 5c746d92
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -9,6 +9,7 @@ def get_db():
db = g.db = sqlite3.connect(DATABASE_URI)
return db
def create_database():
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
......@@ -18,6 +19,16 @@ def create_database():
conn.commit()
conn.close()
def clean_db():
try:
get_db().execute("DROP table USERS;")
get_db().execute("DROP table LOGGEDINUSERS;")
return True
except:
return False
def create_user(email, password, firstname, familyname, gender, city, country):
try:
get_db().execute("INSERT into USERS values(?, ?, ?, ?, ?, ?, ?);", [firstname, familyname, gender, city, country, email, password])
......@@ -26,21 +37,38 @@ def create_user(email, password, firstname, familyname, gender, city, country):
except:
return False
def clean_db():
def authenticate(email, password):
try:
get_db().execute("DROP table USERS;")
cursor = get_db().cursor()
cursor.execute("SELECT * FROM USERS WHERE EMAIL=? AND PASSWORD=?;", [email, password])
user = cursor.fetchone()
if user:
return True
return False
except:
return False
def log_in(token, email):
try:
get_db().execute("INSERT into LOGGEDINUSERS values(?, ?)", [token, email])
get_db().commit()
return True
except:
return False
def authenticate(email, password):
def log_out(token):
try:
cursor = get_db().cursor()
cursor.execute("SELECT * FROM USERS WHERE EMAIL=? AND PASSWORD=?;", [email, password])
cursor.execute("SELECT * FROM LOGGEDINUSERS WHERE TOKEN=?;", [token])
user = cursor.fetchone()
if user:
cursor.execute("DELETE FROM LOGGEDINUSERS WHERE TOKEN=?;", [token])
get_db().commit()
return True
return False
except:
return False
\ No newline at end of file
......@@ -4,7 +4,13 @@ CREATE TABLE USERS(
GENDER VARCHAR(6) NOT NULL,
CITY VARCHAR(20) NOT NULL,
COUNTRY VARCHAR(20) NOT NULL,
EMAIL VARCHAR(20) NOT NULL,
EMAIL VARCHAR(30) NOT NULL,
PASSWORD VARCHAR(20) NOT NULL,
PRIMARY KEY (EMAIL)
);
CREATE TABLE LOGGEDINUSERS(
TOKEN VARCHAR(40) NOT NULL,
EMAIL VARCHAR(30) NOT NULL,
PRIMARY KEY (EMAIl)
)
......@@ -36,7 +36,7 @@ def sign_up():
else:
return "", 400
else:
return "test", 400
return "", 400
@app.route("/sign_in", methods = ['POST'])
def sign_in():
......@@ -47,11 +47,25 @@ def sign_in():
return "", 401
else :
token = generate_token()
print(token)
return token, 200
if database_helper.log_in(token, data['email']):
return token, 201
else:
return "", 409
else:
return "", 400
@app.route("/sign_out", methods = ['POST'])
def sign_out():
data = request.get_json()
if('token' in data):
if database_helper.log_out(data['token']):
return "", 200
else:
return "", 401
else:
return "", 400
@app.route("/delete", methods = ['GET'])
def delete():
resp = database_helper.clean_db()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment