def process_login(): connection = pymongo.Connection(connection_string, safe=True) username = bottle.request.forms.get("username") password = bottle.request.forms.get("password") print "user submitted ", username, "pass ", password userRecord = {} if (user.validate_login(connection, username, password, userRecord)): session_id = user.start_session(connection, username) if (session_id == -1): bottle.redirect("/internal_error") cookie = user.make_secure_val(session_id) # Warning, if you are running into a problem whereby the cookie being set here is # not getting set on the redirct, you are probably using the experimental version of bottle (.12). # revert to .11 to solve the problem. bottle.response.set_cookie("session", cookie) bottle.redirect("/welcome") else: return bottle.template("login", dict(username=cgi.escape(username), password="", login_error="Invalid Login or password"))
def signin(): form = SignInForm(request.form) if request.method == "POST": if form.validate(): username = form.username.data password = form.password.data remember = form.remember.data connection = pymongo.Connection(CONNECTION_STRING, safe=True) response = None try: username = user.validate_login(connection[DATABASE], username, password) except DoesNotExist: pigeon.error("You haven't registered yet!") response = app.make_response(redirect_back("signup")) except UserPasswordNotMatch: pigeon.error("Wrong username/password combination!") response = app.make_response(render_template("signin.html", form=form)) if not response: session_id = user.start_session(connection[DATABASE], username) if session_id == -1: pigeon.error("Internal error!") else: cookie = user.make_secure_val(session_id) # Set cookies to client. session_id = user.start_session(connection[DATABASE], username) cookie = user.make_secure_val(session_id) redirect_to_home = redirect_back("index") response = app.make_response(redirect_to_home) response.set_cookie(COOKIE, value=cookie) # Mark this user has logged in. session["logged_in"] = True session["username"] = username return response else: return response return render_template("signin.html", form=form, status="signin")
def post(self): email = self.get_argument("email", None) password = self.get_argument("password", None) remember = self.get_argument("remember", False) if email is None or email == "": return self.write({"err":True, "msg":u"邮箱不能为空"}) if password is None or password == "": return self.write({"err":True, "msg":u"邮箱不能为空"}) result = user.validate_login(self.db, email, password) logging.info("login result:{}".format(result)) err = result["err"] if err: self.write({"err":True, "msg":result["msg"]}) else: # Add to session. TODO remember me. sid = session.add_to_session(self.db, email) self.set_secure_cookie("sid", sid) self.write({"err":False})