From d62415b5130769d478da1880d3d0d09701b62f12 Mon Sep 17 00:00:00 2001 From: James Wang Date: Thu, 24 Dec 2015 19:57:58 -0500 Subject: [PATCH] Link login front and back ends --- server/api/user.py | 9 ++++++--- server/app.py | 6 +++--- web/js/ascript.js | 2 +- web/js/login.js | 16 ++++++++++++++++ web/pages/login.html | 12 +++++++----- web/pages/register.html | 2 +- 6 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 web/js/login.js diff --git a/server/api/user.py b/server/api/user.py index be8bf1f..fd55f97 100644 --- a/server/api/user.py +++ b/server/api/user.py @@ -49,11 +49,14 @@ def user_logout(): @blueprint.route("/login", methods=["POST"]) @api_wrapper def user_login(): - username = request.form["username"] + email = request.form["email"] password = request.form["password"] - user = Users.query.filter_by(username=username).first() + user = Users.query.filter_by(email=email).first() + if user is None: + return { "success": 0, "message": "Invalid credentials." } + if utils.check_password(user.password, password): - session["username"] = username + session["username"] = user.username session["admin"] = user.admin return { "success": 1, "message": "Success!" } else: diff --git a/server/app.py b/server/app.py index c347fd3..d2462d2 100644 --- a/server/app.py +++ b/server/app.py @@ -19,6 +19,9 @@ app.secret_key = config.SECRET app.config["SQLALCHEMY_DATABASE_URI"] = config.SQLALCHEMY_DATABASE_URI app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = config.SQLALCHEMY_TRACK_MODIFICATIONS +app.register_blueprint(api.admin.blueprint, url_prefix="/api/admin") +app.register_blueprint(api.user.blueprint, url_prefix="/api/user") + @app.route("/api") def api_main(): return json.dumps({ "success": 1, "message": "The API is online." }) @@ -30,8 +33,5 @@ if __name__ == "__main__": args = parser.parse_args() keyword_args, _ = dict(args._get_kwargs()), args._get_args() - app.register_blueprint(api.admin.blueprint, url_prefix="/api/admin") - app.register_blueprint(api.user.blueprint, url_prefix="/api/user") - app.debug = keyword_args["debug"] app.run(host="0.0.0.0", port=8000) diff --git a/web/js/ascript.js b/web/js/ascript.js index a222bf7..8f52857 100644 --- a/web/js/ascript.js +++ b/web/js/ascript.js @@ -19,4 +19,4 @@ app.config(function($routeProvider) { }); app.controller("mainController", function($scope) { -}); \ No newline at end of file +}); diff --git a/web/js/login.js b/web/js/login.js new file mode 100644 index 0000000..f0335cb --- /dev/null +++ b/web/js/login.js @@ -0,0 +1,16 @@ +$("#login-form").on("submit", function(e) { + e.preventDefault(); + login($("#email").val(), $("#password").val()); +}); + +function login(email, password) { + $.post("/api/user/login", { + email: email, + password: password + }, function(data) { + $("#status").text(data.message); + if (data.success == 1) { + // wait then redirect or whatever + } + }); +} diff --git a/web/pages/login.html b/web/pages/login.html index 63f879d..a681a59 100644 --- a/web/pages/login.html +++ b/web/pages/login.html @@ -4,28 +4,30 @@
Log-in to your account
-
+
- +
- +
-
Login
+
+
Register Forgot Password?
- \ No newline at end of file + + diff --git a/web/pages/register.html b/web/pages/register.html index 441123e..78a7da9 100644 --- a/web/pages/register.html +++ b/web/pages/register.html @@ -12,4 +12,4 @@ - \ No newline at end of file +