Login in Flask application using dictionary database, user loader

All other Source.Python topics and issues.
Junior Member
Posts: 1
Joined: Fri May 06, 2022 5:48 am

Login in Flask application using dictionary database, user loader

Postby SergeyZh » Fri May 06, 2022 5:52 am

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()
    user.id = 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"] == form.username.data and a["code"] == form.code.data]
        if user:
            user = User()
            user.id = form.code.data
            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)
User avatar
Junior Member
Posts: 12
Joined: Tue Sep 21, 2021 12:13 pm
Location: Russia

Postby Articha » Mon May 09, 2022 12:18 pm

This is not forum for Flask. If you ask this question on proper forum, you'll get answer much faster

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest