From e6d84cbd3910de462161c26ca9f8cec3ebf266f5 Mon Sep 17 00:00:00 2001 From: kdxcxs Date: Tue, 3 Nov 2020 23:12:13 +0800 Subject: [PATCH] fix: commit to database and get username through jwt in `/register` --- app.py | 11 ++++++----- dao.py | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 0e63cdc..4185675 100644 --- a/app.py +++ b/app.py @@ -26,14 +26,15 @@ def login(): @app.route('/register', methods=['POST']) def register(): cookie_jwt = request.cookies.get('jwt') + if cookie_jwt is not None: + username = jwt.check_jwt(cookie_jwt) + else: + return make_response(json.dumps({'result': 'fail'})) rdata = json.loads(request.data.decode()) # request data - username = rdata['username'] program_type = rdata['program_type'] program_name = rdata['program_name'] - if cookie_jwt is not None: - if jwt.check_jwt(cookie_jwt): - if dao.register(username, program_type, program_name): - return make_response(json.dumps({'result': 'success'})) + if dao.register(username, program_type, program_name): + return make_response(json.dumps({'result': 'success'})) return make_response(json.dumps({'result': 'fail'})) diff --git a/dao.py b/dao.py index ff918fd..576262e 100644 --- a/dao.py +++ b/dao.py @@ -7,8 +7,10 @@ def valid(username, password): password_cursor = base_cursor.execute(f'SELECT password FROM user WHERE username=="{username}";') db_password = password_cursor.fetchone() if db_password is None: + db_connection.close() return False if db_password[0] == password: + db_connection.close() return True @@ -16,5 +18,7 @@ def register(username, program_type, program_name): db_connection = sqlite3.connect('sports-registration.sqlite') base_cursor = db_connection.cursor() base_cursor.execute(f'INSERT INTO list (username, program_type, program_name) ' - f'VALUES ("{username}", "{program_type}", "{program_name}")') + f'VALUES ("{username}", "{program_type}", "{program_name}");') + db_connection.commit() + db_connection.close() return True