zeyad
zeyad

Reputation: 53

current user.is_authenticated gives me a True value but it didn't work in base.html file

I'm trying to check if the user is logged in or not to change nav elements and to do more stuff and here is my code

And when I do this

print(attempted_account.is_authenticated) result => True

I think that the problem will be in base.html file so I hope you can help me.

routs.py:

@app.route('/login',methods=['GET','POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        attempted_account = Account.query.filter_by(email=form.email.data).first()
        print(attempted_account.is_authenticated)
        if attempted_account and attempted_account.check_password(attempted_password=form.password.data):
            login_user(attempted_account)
            flash('Success login ',category='success')
            return redirect(url_for('page',id=1))
        elif not attempted_account:
            flash("The E-mail doesn't exist",category='danger')
        else:
            flash('Tha password is incorrect',category='danger')
    return render_template('login.html',form=form)

base.html

<nav>
    <ul>
        <li><a class="home" href="{{ url_for('home') }}">Market</a></li>
        <li><a href="{{ url_for('page',id=1) }}">Go shopping</a></li> 
    </ul>
    {% if current_user.is_authenticated %}
        <ul>
            <li><a href="#">Logout</a></li> 
        </ul>
    {% else %}
        <ul>
            <li><a href="{{ url_for('signup') }}">Sign up</a></li>
            <li><a href="{{ url_for('login') }}">Login</a></li> 
        </ul>
    {% endif %}
</nav>

login.html

{% extends 'base.html' %}
{% block title %}
    Login
{% endblock %}

{% block content %}
<div class="container">
    <form method="POST" class="form-register" style="text-align: center;">
    {{ form.hidden_tag() }}
    {{ form.csrf_token }}

    {{ form.email.label() }}
    {{ form.email(class='form-control',placeholder='example@example.com') }}
    <br>
    {{ form.password.label() }}
    {{ form.password(class='form-control',placeholder='Password') }}
    <br>
    {{ form.submit(class='btn btn-lg btn-block btn-primary') }}
    </form>
</div>
{% endblock %}

Please note that I called my model Account not User

Edit:

Here is models.py:

from market import db
from market import bcrypt,login_manager
from flask_login import UserMixin

@login_manager.user_loader
def load_user(user_id):
    return Account.query.filter_by(id=user_id)

class Account(db.Model,UserMixin):

id              = db.Column(db.Integer,primary_key = True)
first_name      = db.Column(db.String(length = 50),nullable  = False)
last_name       = db.Column(db.String(length = 50),nullable  =False)
email           = db.Column(db.String(length = 100),nullable =False,unique = True)
password_hashed = db.Column(db.String(length = 25),nullable = False)
#country        = db.Column(db.String,nullable = False)
items           = db.relationship('Item',backref = 'owner',lazy = True)

@property
def password(self):
    return self.password
@password.setter
def password(self,plain_text_password):
    self.password_hashed = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')
def check_password(self,attempted_password):
    return bcrypt.check_password_hash(self.password_hashed,attempted_password)

Upvotes: 0

Views: 54

Answers (1)

Magnun Leno
Magnun Leno

Reputation: 2738

The issue is probably because you're returning a query result in the user_loader callback instead of an Account object.

Instead of returning Account.query.filter_by(id=user_id) try returning Account.query.filter_by(id=user_id).first().

Hope it works.

Upvotes: 1

Related Questions