0
votes

I am trying to redirect the page upon successful login but I keep running into the same issue.

My login code:

@app.route('/login/', methods = ['GET', 'POST'])
def login():
    if request.method == 'POST':
        #data = request.get_json()
        #app.logger.debug(data)
        auth = g.couch.get('_design/authentication')
        authView = ViewDefinition('_design/authentication','authentication',auth['views']['authentication']['map'])
        for row in authView():
            if request.form['username'] == row.value['username'] and request.form['password'] == row.value['password']:
                authUser = userModel.User(row.value)
                app.logger.debug(row.value)
                authUser.authenticated = True;
                flask_login.login_user(authUser,remember = True)
                next = request.args.get('next')
                return redirect(next or url_for('/protected/')) 
    return render_template('sampleLoginPage.html')

@app.route('/protected/', methods = ['GET'])
@flask_login.login_required
def protected():
    app.logger.debug("Successful Login")
    app.logger.debug(flask_login.current_user.userID)
    return Response(json.dumps({'message' : '\'You\'re logged in so you can see this page'+flask_login.current_user.username}), mimetype = 'application/json')

The html template picks up the username and password. I have a couchdb view that authenticates the user.

When i enter my login credentials, the url redirects to http://192.168.1.109:5000/login/?next=%2Fprotected%2F

I did not fully understand the documentation. Would someone be able to better explain this concept? Why am I getting this?

This is also coming up when i try to connect it to my angularjs front end.

Regards, Sunil

1
Could you show your flask_login user_loader function?Wombatz
Hi Wombatz, Sorry for the late response. It appears that there is an issue with the front end (I am using AngularJS). The login works perfectly when I use postman or a browser. It throws this error only when I access it through the app emulator.galeej

1 Answers

-1
votes

I had this issue as well. When I removed @login_required from the declaration, the redirect worked as intended.