0
0
Flaskframework~5 mins

Current_user object in Flask

Choose your learning style9 modes available
Introduction

The current_user object helps you know who is using your web app right now. It makes it easy to check if someone is logged in and get their info.

To show a welcome message with the user's name after they log in.
To restrict access to certain pages only to logged-in users.
To customize content based on the user's preferences or role.
To log user actions or track who made changes.
To display user profile details on their dashboard.
Syntax
Flask
from flask_login import current_user

# Use current_user in your route or template
if current_user.is_authenticated:
    username = current_user.username
else:
    username = 'Guest'

current_user is provided by the flask_login extension.

You can access user properties like username if your user model has them.

Examples
Check if the user is logged in and greet them by name.
Flask
from flask_login import current_user

@app.route('/profile')
def profile():
    if current_user.is_authenticated:
        return f"Hello, {current_user.username}!"
    else:
        return "Please log in to see your profile."
Redirect users who are not logged in to the login page.
Flask
from flask import redirect
from flask_login import current_user

@app.route('/dashboard')
def dashboard():
    if not current_user.is_authenticated:
        return redirect('/login')
    # Show dashboard for logged-in user
    return f"Welcome to your dashboard, {current_user.username}!"
Sample Program

This Flask app logs in a user named Alice when you visit /login. Then, visiting /hello shows a greeting using current_user.username. If you are not logged in, /hello redirects you to login.

Flask
from flask import Flask, redirect, url_for
from flask_login import LoginManager, UserMixin, login_user, current_user, login_required

app = Flask(__name__)
app.secret_key = 'secret'

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'

# Simple user class
class User(UserMixin):
    def __init__(self, id, username):
        self.id = id
        self.username = username

# User loader for flask-login
@login_manager.user_loader
def load_user(user_id):
    if user_id == '1':
        return User('1', 'Alice')
    return None

@app.route('/login')
def login():
    user = User('1', 'Alice')
    login_user(user)
    return 'Logged in as Alice'

@app.route('/hello')
@login_required
def hello():
    return f'Hello, {current_user.username}!'

if __name__ == '__main__':
    app.run(debug=False)
OutputSuccess
Important Notes

Always protect routes that use current_user with @login_required to avoid errors.

current_user.is_authenticated is a quick way to check if someone is logged in.

You can add any user info to your user class and access it via current_user.

Summary

current_user tells you who is using your app right now.

Use it to check login status and get user details easily.

Protect routes with @login_required to safely use current_user.