def login(): try: email = request.json["email"] password = request.json["pwd"] if email and password: user = list(filter(lambda x: security.dec( x["email"]) == email and security.checkpwd(password, x["password"]), getUsers())) # Check if user exists if len(user) == 1: token = create_access_token(identity=user[0]["id"]) refresh_token = create_refresh_token(identity=user[0]["id"]) return jsonify({"token": token, "refreshToken": refresh_token}) else: return jsonify({"error": "Invalid credentials"}) else: return jsonify({"error": "Invalid form"}) except Exception as e: print(e) return jsonify({"error": "Invalid form"})
def register(): try: email = request.json["email"] email = email.lower() password = security.encpwd(request.json["password"]) firstname = request.json["firstname"] lastname = request.json["lastname"] print(email, password, request.json["password"], firstname, lastname) if not (email and password and firstname and lastname): return jsonify({"error": "Invalid form"}) # Check to see if user already exists users = getUsers() if len(list(filter(lambda x: security.dec(x["email"]) == email, users))) == 1: return jsonify({"error": "Invalid form"}) # Email validation check if not re.match(r"[\w._]{5,}@\w{3,}\.\w{2,4}", email): return jsonify({"error": "Invalid email"}) addUser(firstname, lastname, security.enc(email), password) return jsonify({"success": True}) except Exception as e: return jsonify({"error": f"Invalid form{e}"})