Page 1 of 1

Login in Flask application using dictionary database, user loader

Posted: Fri May 06, 2022 5:52 am
by SergeyZh
I have written some code for a simple flask login form using dictionary database for users. The code seems to be working but I'm still not clear if I did it correctly. Since I'm not using SQL database to store users ID's I had to login users using their passwords ('code' in my case) as ID's. Am I using UserMixin Class correctly, is there a logic really in the code? Thanks

Code: Select all

users = [
    {'username': 'admin1', 'code': 'pass1'},
    {'username': 'admin2', 'code': 'pass2'},
    {'username': 'admin3', 'code': 'pass3'}

class User(UserMixin):
    def is_active(self):
        return True

    def is_authenticated(self):
        return True

def load_user(code):
    user = User() = code
    return user

@app.route("/login_new", methods=['POST', 'GET'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('booking'))
    form = LoginForm()
    if form.validate_on_submit():
        user = [a for a in users if a["username"] == and a["code"] ==]
        if user:
            user = User()
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(url_for('login'))
            flash('Login Unsuccessful. Please check your name and the code', 'danger')
            return redirect(url_for('login'))
    return render_template('login_new.html', title='Login_new', form=form)

Posted: Mon May 09, 2022 12:18 pm
by Articha
This is not forum for Flask. If you ask this question on proper forum, you'll get answer much faster