🔍 Code Extractor

function get_current_user

Maturity: 34

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

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
356 - 360
Complexity:
simple

Purpose

This function provides a centralized way to get the current user's identifier (email) from the session. It's used for tracking user actions, logging, and authorization checks throughout a Flask web application. The function depends on a separate is_authenticated() function to verify authentication status before accessing session data.

Source Code

def get_current_user():
    """Get current user from session"""
    if is_authenticated():
        return session.get('user', {}).get('email', 'anonymous')
    return 'anonymous'

Return Value

Returns a string representing the current user. If the user is authenticated and has an email in the session, returns that email address. If the user is not authenticated or no email is found in the session, returns the string 'anonymous'.

Dependencies

  • flask

Required Imports

from flask import session

Usage Example

from flask import Flask, session

app = Flask(__name__)
app.secret_key = 'your-secret-key'

def is_authenticated():
    return 'user' in session and session['user'].get('authenticated', False)

def get_current_user():
    if is_authenticated():
        return session.get('user', {}).get('email', 'anonymous')
    return 'anonymous'

@app.route('/profile')
def profile():
    current_user = get_current_user()
    return f'Current user: {current_user}'

# Example with authenticated session
with app.test_request_context():
    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess['user'] = {'email': 'user@example.com', 'authenticated': True}
        user = get_current_user()
        print(user)  # Output: 'user@example.com'

Best Practices

  • Ensure is_authenticated() function is properly implemented before using this function
  • Always set Flask's SECRET_KEY in production to secure session data
  • Consider using more robust user identification (user ID) instead of email for internal operations
  • This function assumes a specific session structure with nested dictionaries - ensure consistency across the application
  • Handle the 'anonymous' return value appropriately in calling code to prevent unauthorized access
  • Consider adding type hints for better code documentation: def get_current_user() -> str:
  • For production use, consider logging failed authentication attempts or session access issues

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_user_email 86.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
  • function get_current_user_id 86.7% similar

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

    From: /tf/active/vicechatdev/docchat/app.py
  • function get_user_id 85.0% 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_current_username 84.2% 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_name 76.9% 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
← Back to Browse