0

I am new to Flask and have been working on an existing app for its login module. It has to be delivered asap. The login credentials validates by Active Directory authentication, which is working as expected. I am referring this flask link which says

url_for('main', page=2): return the internal URL /?page=2. All optional keyword arguments are treated as GET parameters.

My index page is loaded from here (working perfectly):

@app.route("/", methods=['GET', 'POST'])
def login():
  error = None
  if request.method == 'POST':
    userName = request.form['username']
    pwd = request.form['password']
    try:
      ldapobject = auth.bind(userName,pwd)
      return redirect(url_for('scheduler'))  ----> NEED TO PASS USER AND PWD TO scheduler function using post here
    except ldap.LDAPError, error_message:
      error = 'Invalid Credentials. Please try again'
  return render_template('login.html', error=error)

My scheduler function:

@app.route("/home", methods=['POST'])
def scheduler():
  # some code here
  # WANT TO HAVE USER CREDENTIALS IN THIS FUNCTION FROM login page
return render_template("scheduler.html", scheduler=form)

Problem: How to pass user's credentials to scheduler method as a POST call

1
  • 1
    The short answer is: you can't. You cannot pass POST parameters through a URL. Commented Mar 3, 2016 at 15:23

1 Answer 1

3

If you want to persist user data between your application pages - you will need to store some session data on user side, e.g. cookies.

flask-login module (https://flask-login.readthedocs.org/en/) can do all the dirty work for you.

In short you need to create User class with several required fields and methods (https://flask-login.readthedocs.org/en/latest/#your-user-class) and specify several methods with decorators to load and check users that are being authenticated:

from flask_login import LoginManager

login_manager = LoginManager()
login_manager.init_app(app=app)

@login_manager.user_loader
def load_user(username):
    return User(username)

@login_manager.request_loader
def load_from_request(request):
    return User.auth_is_valid(request.authorization)

After that you can mark any routes that need user credentials with @login_requied decorator and access logged in user via current_user variable:

from flask_login import login_required, current_user
@app.route("/user_needed")
@login_required
def some_function():
    print(current_user)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.