diff --git a/Lab_3/Twidder/server.py b/Lab_3/Twidder/server.py index 26705d7ba7581a899d5226442e9ce9afa0816634..e23f3a91a0213551b97aef9e2adb374ab924f1af 100644 --- a/Lab_3/Twidder/server.py +++ b/Lab_3/Twidder/server.py @@ -38,8 +38,13 @@ def token_has_error(token): def input_has_error(input): """All standard input error checks""" + # tmp = "" + # try: + # tmp = request.get_json()[input] + # except: + # return jsonify({}), 401 if input is None: # "Server received no " + str - return True, 400 + return True, 401 if len(input) > 50: # "Server received too long " + str return True, 400 return False, 0 @@ -49,11 +54,21 @@ def input_has_error(input): def sign_in(): """Sign in user""" # return jsonify({}), 204 - email = request.get_json()['email'] - password = request.get_json()['password'] + print("1st try") + try: + email = request.get_json()['email'] + print(request.get_json()['email']) + except: + return jsonify({}), 401 + print("2st try") + try: + password = request.get_json()['password'] + except: + return jsonify({}), 401 + print("3st try") # Validate Email - tmp = input_has_error(email) + tmp = input_has_error('email') if tmp[0]: return jsonify({}), tmp[1] @@ -76,13 +91,13 @@ def sign_in(): # return the token in the Authorization header response = make_response(jsonify({})) #"Server inserted user data into database" + response.headers.add("Access-Control-Allow-Origin", "*") response.headers["Authorization"] = token return response, 204 @app.route("/myServer/sign_up", methods=['POST']) def sign_up(): - print('hello') """Sign up a user""" json_obj = request.get_json() diff --git a/Lab_3/Twidder/static/client.js b/Lab_3/Twidder/static/client.js index 6658aaec9f5964004fef3e81bae4b3ad09469b00..d82e25f94df2881402535edf035abdf2955f26b8 100644 --- a/Lab_3/Twidder/static/client.js +++ b/Lab_3/Twidder/static/client.js @@ -102,12 +102,12 @@ function sign_up() { //----------------//----------------//----------------//---------------- let xhttp = new XMLHttpRequest(); - // xhttp.onreadystatechange = function () { - // if (this.readyState == 4 && this.status == 200) { - // // Typical action to be performed when the document is ready: - // errorMess.innerHTML = xhttp.responseText; - // } - // }; + xhttp.onreadystatechange = function () { + if (this.readyState == 4 && this.status == 200) { + // Typical action to be performed when the document is ready: + errorMess.innerHTML = xhttp.responseText; + } + }; //xhttp.addEventListener("load", reqListener); xhttp.open("POST", "http://127.0.0.1:5000/myServer/sign_up", true); xhttp.setRequestHeader('Authorization', '1f191d40-c128-477d-9f9f-196c81810f02'); @@ -131,18 +131,18 @@ function sign_up() { function sign_in() { let form = document.getElementById("sign_in_form"); - let email = form[0].value; - let password = form[1].value; + let emailObj = form[0].value; + let passwordObj = form[1].value; let errorMess = document.getElementById("error"); // Error checks - if (password.length < minPassLength) + if (passwordObj.length < minPassLength) errorMess.innerHTML = "Password's length is too short"; else { //Todo // Sending sign_in request to "server" - let signInObj = serverstub.signIn(email, password); + let signInObj = serverstub.signIn(emailObj, passwordObj); var signInRequest = new XMLHttpRequest(); signInRequest.onreadystatechange = function() { @@ -159,7 +159,16 @@ function sign_in() { } }; signInRequest.open("POST", "http://127.0.0.1:5000/myServer/sign_in", true); - signInRequest.send(); + //signInRequest.send(); + // xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + signInRequest.send(JSON.stringify({email : emailObj ,password : passwordObj })); + + // var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance + // var theUrl = "/json-handler"; + // xmlhttp.open("POST", theUrl); + // xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + // xmlhttp.send(JSON.stringify({ "email": "hello@user.com", "response": { "name": "Tester" } })); + // Set message to user errorMess.innerHTML = signInObj["message"]; @@ -167,7 +176,7 @@ function sign_in() { let token = signInObj["data"]; localStorage.setItem("currentUser", token); - localStorage.setItem("homeEmail", email); + localStorage.setItem("homeEmail", emailObj); // Changes the view to profile view an loads user info displayView(); @@ -196,8 +205,6 @@ function sign_out(){ } } - - // - - - - - Changing tabs - - - - - // function showPage(ourEvent, name){