🔍 Code Extractor

function get_current_user_id

Maturity: 34

Retrieves the current logged-in user's ID from the Flask session, returning 'anonymous' if authentication is disabled or no user is logged in.

File:
/tf/active/vicechatdev/docchat/app.py
Lines:
100 - 104
Complexity:
simple

Purpose

This function provides a centralized way to access the current user's ID in a Flask web application. It checks if authentication is required via a config setting and whether a user session exists. If both conditions are met, it extracts and returns the user_id from the session; otherwise, it returns 'anonymous' as a default value. This is commonly used for logging, auditing, access control, and personalizing user experiences.

Source Code

def get_current_user_id():
    """Get the current logged-in user's ID"""
    if config.REQUIRE_AUTH and 'user' in session:
        return session['user'].get('user_id', 'anonymous')
    return 'anonymous'

Return Value

Returns a string representing the user ID. If authentication is enabled (config.REQUIRE_AUTH is True) and a user is logged into the session, returns the 'user_id' value from the session dictionary. If the user_id key doesn't exist in the session user object, or if authentication is disabled, or if no user is in the session, returns the string 'anonymous'.

Dependencies

  • flask

Required Imports

from flask import session
import config

Usage Example

from flask import session
import config

# Assuming Flask app is set up with session support
# and config.REQUIRE_AUTH is defined

def get_current_user_id():
    """Get the current logged-in user's ID"""
    if config.REQUIRE_AUTH and 'user' in session:
        return session['user'].get('user_id', 'anonymous')
    return 'anonymous'

# Example usage in a Flask route
@app.route('/api/data')
def get_data():
    user_id = get_current_user_id()
    print(f"Request from user: {user_id}")
    # Use user_id for logging, access control, etc.
    return jsonify({'user': user_id, 'data': 'some data'})

Best Practices

  • Ensure Flask session is properly configured with a SECRET_KEY before using this function
  • The config.REQUIRE_AUTH setting should be consistently used throughout the application
  • When setting user data in the session, always include a 'user_id' key in the user dictionary
  • Consider using this function consistently across the application instead of directly accessing session data
  • The 'anonymous' return value should be handled appropriately in calling code for access control decisions
  • This function assumes the session structure has a 'user' dictionary containing 'user_id' - ensure this structure is maintained during authentication
  • For production applications, consider adding additional validation or error handling for malformed session data

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_current_user 86.7% similar

    Retrieves the current user's email from the Flask session if authenticated, otherwise returns 'anonymous'.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_current_username 83.8% similar

    Retrieves the current user's username from Flask-Login's current_user object or falls back to the Flask session, returning 'anonymous' if neither is available.

    From: /tf/active/vicechatdev/docchat/blueprint.py
  • function get_user_id 82.9% similar

    Retrieves the current user's email address from the Flask session object, returning 'unknown' if not found.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function get_user_name 76.5% similar

    Retrieves the current user's name from the Flask session object, returning 'Unknown User' if not found.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function get_user_email 73.7% similar

    Retrieves the email address of the currently authenticated user from the Flask session object.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
← Back to Browse