Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7417745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:45:25+00:00 2026-05-29T07:45:25+00:00

I’m working on a flask app that needs authentication. I’ve hooked up flask-login but

  • 0

I’m working on a flask app that needs authentication. I’ve hooked up flask-login but it doesn’t seem very graceful.

First flask-login needs to make sure the user exists:

@login_manager.user_loader
def load_user(id):
    return User.query.get(id)

But you also need to use ‘login_user’ to create the user object

# Some code above 
  user = User.query.filter_by(email = form.email.data, password = form.password.data).first()
  user.login_status = 1
  db.session.commit()
  login_user(objects.SignedInUser(user.id, user.email, user.login_status == LoginStatus.Active))    
# Some code below

In the code above ‘User’ is a model for postgres and SignedInUser is just an object to be used for flask-login.

Does anyone have an example of flask-login used with postgres?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:45:26+00:00Added an answer on May 29, 2026 at 7:45 am

    It looks like you might be misunderstanding what Flask-Login handles. It’s there to keep track of everything about the user’s session after you tell it authentication was successful (by calling login_user.) The user_loader callback only tells it how to reload the object for a user that has already been authenticated, such as when someone reconnects to a “remember me” session. The docs are not especially clear on that.

    There should be no need to keep a flag in the database for the user’s login status. Also, the code you included will raise an AttributeError if the credentials are incorrect (user = None).

    Here’s an example from a Flask-SQLAlchemy application. It uses an external authentication source and a wrapper for the SQLAlchemy User object, but the process is basically the same.

    user_loader callback:

    @login_manager.user_loader
    def load_user(user_id):
        user = User.query.get(user_id)
        if user:
            return DbUser(user)
        else:
            return None
    

    User class (wrapper for SQLAlchemy object):

    # User class
    class DbUser(object):
        """Wraps User object for Flask-Login"""
        def __init__(self, user):
            self._user = user
    
        def get_id(self):
            return unicode(self._user.id)
    
        def is_active(self):
            return self._user.enabled
    
        def is_anonymous(self):
            return False
    
        def is_authenticated(self):
            return True
    

    Login handler:

    @app.route('/login', methods=['GET', 'POST'])
    def login():
        error = None
        next = request.args.get('next')
        if request.method == 'POST':
            username = request.form['username']
            password = request.form['password']
    
    
            if authenticate(app.config['AUTH_SERVER'], username, password):
                user = User.query.filter_by(username=username).first()
                if user:
                    if login_user(DbUser(user)):
                        # do stuff
                        flash("You have logged in")
    
                        return redirect(next or url_for('index', error=error))
            error = "Login failed"
        return render_template('login.html', login=True, next=next, error=error)
    

    Note that login fails if:

    • external auth fails
    • user query returns None (user does not exist)
    • login_user returns False (user.is_active() == False)

    Logout

    @app.route('/logout')
    @login_required
    def logout():
        logout_user()
        flash('You have logged out')
        return(redirect(url_for('login')))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.