Skip to content
Snippets Groups Projects
Commit 1ffb3f7a authored by Lawrence Zawahri's avatar Lawrence Zawahri
Browse files

lab 3 progress

parent 6a093929
No related branches found
No related tags found
No related merge requests found
...@@ -38,8 +38,13 @@ def token_has_error(token): ...@@ -38,8 +38,13 @@ def token_has_error(token):
def input_has_error(input): def input_has_error(input):
"""All standard input error checks""" """All standard input error checks"""
# tmp = ""
# try:
# tmp = request.get_json()[input]
# except:
# return jsonify({}), 401
if input is None: # "Server received no " + str if input is None: # "Server received no " + str
return True, 400 return True, 401
if len(input) > 50: # "Server received too long " + str if len(input) > 50: # "Server received too long " + str
return True, 400 return True, 400
return False, 0 return False, 0
...@@ -49,11 +54,21 @@ def input_has_error(input): ...@@ -49,11 +54,21 @@ def input_has_error(input):
def sign_in(): def sign_in():
"""Sign in user""" """Sign in user"""
# return jsonify({}), 204 # return jsonify({}), 204
email = request.get_json()['email'] print("1st try")
password = request.get_json()['password'] 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 # Validate Email
tmp = input_has_error(email) tmp = input_has_error('email')
if tmp[0]: if tmp[0]:
return jsonify({}), tmp[1] return jsonify({}), tmp[1]
...@@ -76,13 +91,13 @@ def sign_in(): ...@@ -76,13 +91,13 @@ def sign_in():
# return the token in the Authorization header # return the token in the Authorization header
response = make_response(jsonify({})) #"Server inserted user data into database" response = make_response(jsonify({})) #"Server inserted user data into database"
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers["Authorization"] = token response.headers["Authorization"] = token
return response, 204 return response, 204
@app.route("/myServer/sign_up", methods=['POST']) @app.route("/myServer/sign_up", methods=['POST'])
def sign_up(): def sign_up():
print('hello')
"""Sign up a user""" """Sign up a user"""
json_obj = request.get_json() json_obj = request.get_json()
......
...@@ -102,12 +102,12 @@ function sign_up() { ...@@ -102,12 +102,12 @@ function sign_up() {
//----------------//----------------//----------------//---------------- //----------------//----------------//----------------//----------------
let xhttp = new XMLHttpRequest(); let xhttp = new XMLHttpRequest();
// xhttp.onreadystatechange = function () { xhttp.onreadystatechange = function () {
// if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
// // Typical action to be performed when the document is ready: // Typical action to be performed when the document is ready:
// errorMess.innerHTML = xhttp.responseText; errorMess.innerHTML = xhttp.responseText;
// } }
// }; };
//xhttp.addEventListener("load", reqListener); //xhttp.addEventListener("load", reqListener);
xhttp.open("POST", "http://127.0.0.1:5000/myServer/sign_up", true); xhttp.open("POST", "http://127.0.0.1:5000/myServer/sign_up", true);
xhttp.setRequestHeader('Authorization', '1f191d40-c128-477d-9f9f-196c81810f02'); xhttp.setRequestHeader('Authorization', '1f191d40-c128-477d-9f9f-196c81810f02');
...@@ -131,18 +131,18 @@ function sign_up() { ...@@ -131,18 +131,18 @@ function sign_up() {
function sign_in() { function sign_in() {
let form = document.getElementById("sign_in_form"); let form = document.getElementById("sign_in_form");
let email = form[0].value; let emailObj = form[0].value;
let password = form[1].value; let passwordObj = form[1].value;
let errorMess = document.getElementById("error"); let errorMess = document.getElementById("error");
// Error checks // Error checks
if (password.length < minPassLength) if (passwordObj.length < minPassLength)
errorMess.innerHTML = "Password's length is too short"; errorMess.innerHTML = "Password's length is too short";
else { else {
//Todo //Todo
// Sending sign_in request to "server" // Sending sign_in request to "server"
let signInObj = serverstub.signIn(email, password); let signInObj = serverstub.signIn(emailObj, passwordObj);
var signInRequest = new XMLHttpRequest(); var signInRequest = new XMLHttpRequest();
signInRequest.onreadystatechange = function() { signInRequest.onreadystatechange = function() {
...@@ -159,7 +159,16 @@ function sign_in() { ...@@ -159,7 +159,16 @@ function sign_in() {
} }
}; };
signInRequest.open("POST", "http://127.0.0.1:5000/myServer/sign_in", true); 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 // Set message to user
errorMess.innerHTML = signInObj["message"]; errorMess.innerHTML = signInObj["message"];
...@@ -167,7 +176,7 @@ function sign_in() { ...@@ -167,7 +176,7 @@ function sign_in() {
let token = signInObj["data"]; let token = signInObj["data"];
localStorage.setItem("currentUser", token); localStorage.setItem("currentUser", token);
localStorage.setItem("homeEmail", email); localStorage.setItem("homeEmail", emailObj);
// Changes the view to profile view an loads user info // Changes the view to profile view an loads user info
displayView(); displayView();
...@@ -196,8 +205,6 @@ function sign_out(){ ...@@ -196,8 +205,6 @@ function sign_out(){
} }
} }
// - - - - - Changing tabs - - - - - // // - - - - - Changing tabs - - - - - //
function showPage(ourEvent, name){ function showPage(ourEvent, name){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment