def create_user(): form = RegistrationForm(request.form) if form.validate(): new_user = User(username=form.username.data) new_user.generate_password_digest(form.password.data) if new_user.save(): session = Session(session_token=Session.generate_session_token(), username=new_user.username) if session.save(): response = jsonify( user=user_response_obj(new_user), message="New user created! Welcome {0}!".format( new_user.username)) response.set_cookie('pomodoro-to-do', session.session_token) return response else: return jsonify( error="New user created, but failed to login"), 401 else: return jsonify(error="Could not create user."), 401 else: return jsonify(errors=form.errors.items()), 400
def __maintain_max_session_limit(user): sessions = Session.objects(username=user.username) num_sessions = sessions.count() if num_sessions > 5: oldest_session = sessions[0] oldest_session.delete()
def logout(): session_key = session.get('s_key', None) if session_key: uid = Session.get(session_key)['user'] user = User.get(uid) logout_user(user) return redirect(url_for('.index'))
def __create_session(): form = LoginForm(request.form) if form.validate(): user = User.find_by_username(form.username.data) session = Session(session_token=Session.generate_session_token(), username=user.username) session.save() __maintain_max_session_limit(user) response = jsonify(user=user_response_obj(user), message = "Login successful! Welcome {0}!".format(user.username)) response.set_cookie('pomodoro-to-do', session.session_token) return response else: return jsonify(errors=form.errors.items()), 400
def __create_session(): form = LoginForm(request.form) if form.validate(): user = User.find_by_username(form.username.data) session = Session(session_token=Session.generate_session_token(), username=user.username) session.save() __maintain_max_session_limit(user) response = jsonify(user=user_response_obj(user), message="Login successful! Welcome {0}!".format( user.username)) response.set_cookie('pomodoro-to-do', session.session_token) return response else: return jsonify(errors=form.errors.items()), 400
def create_user(): form = RegistrationForm(request.form) if form.validate(): new_user = User(username = form.username.data) new_user.generate_password_digest(form.password.data) if new_user.save(): session = Session(session_token=Session.generate_session_token(), username=new_user.username) if session.save(): response = jsonify(user = user_response_obj(new_user), message = "New user created! Welcome {0}!".format(new_user.username)) response.set_cookie('pomodoro-to-do', session.session_token) return response else: return jsonify(error="New user created, but failed to login"), 401 else: return jsonify(error="Could not create user."), 401 else: return jsonify(errors=form.errors.items()), 400
def __fetch_session(): cookie = request.cookies.get('pomodoro-to-do') if cookie: session = Session.objects(session_token=cookie) if session: username = session[0].username user = User.objects.get(username=username) return jsonify(user=user_response_obj(user), message = "Welcome back {0}!".format(user.username)) else: return jsonify(user={}) else: return jsonify(user={})
def __fetch_session(): cookie = request.cookies.get('pomodoro-to-do') if cookie: session = Session.objects(session_token=cookie) if session: username = session[0].username user = User.objects.get(username=username) return jsonify(user=user_response_obj(user), message="Welcome back {0}!".format(user.username)) else: return jsonify(user={}) else: return jsonify(user={})