🔍 Code Extractor

function get_user_name

Maturity: 32

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

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
525 - 527
Complexity:
simple

Purpose

This function provides a safe way to access the current authenticated user's name from the Flask session. It's designed to work within a Flask web application context where user information is stored in the session after authentication. The function uses nested dictionary get() methods to prevent KeyError exceptions when the 'user' key or 'name' subkey don't exist in the session.

Source Code

def get_user_name():
    """Get current user name"""
    return session.get('user', {}).get('name', 'Unknown User')

Return Value

Returns a string containing the current user's name from session['user']['name']. If the 'user' key doesn't exist in the session, or if the 'name' key doesn't exist within the user dictionary, returns the default string 'Unknown User'. Type: str

Dependencies

  • flask

Required Imports

from flask import session

Usage Example

from flask import Flask, session

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

def get_user_name():
    return session.get('user', {}).get('name', 'Unknown User')

@app.route('/profile')
def profile():
    username = get_user_name()
    return f'Hello, {username}!'

# In a login route, you would set the session:
@app.route('/login', methods=['POST'])
def login():
    # After authentication
    session['user'] = {'name': 'John Doe', 'id': 123}
    return 'Logged in'

if __name__ == '__main__':
    app.run()

Best Practices

  • This function must be called within a Flask request context, otherwise it will raise a RuntimeError
  • Ensure Flask session is properly configured with a SECRET_KEY before using this function
  • The function assumes a specific session structure: session['user']['name']. Ensure your authentication system populates this structure
  • Consider adding type hints for better code documentation: def get_user_name() -> str:
  • For production use, consider logging when 'Unknown User' is returned to detect authentication issues
  • If you need to distinguish between an unauthenticated user and a user without a name, consider returning None instead of 'Unknown User' and handling the display logic elsewhere

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_user_email 82.9% 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_user_id 81.3% 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 79.1% 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_current_user 76.9% 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_user_id 76.5% 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
← Back to Browse