🔍 Code Extractor

function login_v3

Maturity: 41

Flask route handler that displays the login page and redirects authenticated users to the document workspace.

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
641 - 647
Complexity:
simple

Purpose

This function serves as the entry point for user authentication in a Flask web application. It checks if the user is already authenticated and redirects them to the document workspace if so. For unauthenticated users, it generates an Azure SSO authentication URL and renders the login template with that URL, enabling users to authenticate via Azure Active Directory.

Source Code

def login():
    """Login page"""
    if is_authenticated():
        return redirect(url_for('document_workspace'))
    
    auth_url = azure_sso.get_auth_url()
    return render_template('login.html', auth_url=auth_url)

Return Value

Returns either a Flask redirect response to 'document_workspace' route if user is authenticated, or a rendered HTML template ('login.html') with the Azure SSO authentication URL passed as a template variable if user is not authenticated. The return type is a Flask Response object.

Dependencies

  • flask
  • azure_auth (custom module)
  • werkzeug

Required Imports

from flask import render_template
from flask import redirect
from flask import url_for
from auth.azure_auth import AzureSSO

Usage Example

from flask import Flask, render_template, redirect, url_for, session
from auth.azure_auth import AzureSSO

app = Flask(__name__)
app.secret_key = 'your-secret-key'
azure_sso = AzureSSO(client_id='your-client-id', tenant_id='your-tenant-id', redirect_uri='http://localhost:5000/callback')

def is_authenticated():
    return 'user_id' in session

@app.route('/login')
def login():
    if is_authenticated():
        return redirect(url_for('document_workspace'))
    auth_url = azure_sso.get_auth_url()
    return render_template('login.html', auth_url=auth_url)

@app.route('/document_workspace')
def document_workspace():
    return 'Welcome to Document Workspace'

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

Best Practices

  • Ensure is_authenticated() function is properly implemented to check session or token validity
  • The azure_sso object must be initialized before the Flask app starts accepting requests
  • Use HTTPS in production to protect authentication tokens and session data
  • Implement proper session timeout and security measures
  • The login.html template should properly handle the auth_url variable and provide a secure login interface
  • Consider implementing CSRF protection for the login flow
  • Ensure the document_workspace route exists and is properly protected with authentication checks
  • Set appropriate Flask session configuration including secure cookies and httponly flags in production

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function login_v2 92.1% similar

    Flask route handler that displays the login page and redirects authenticated users to the chat interface.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function login_v1 89.5% similar

    Flask route handler that manages user authentication by redirecting authenticated users to the index page, initiating Azure SSO login for production, or creating a development session for local testing.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function login 88.0% similar

    Flask route handler that renders the login page for user authentication, with support for Azure SSO integration and automatic redirection for authenticated users or when authentication is disabled.

    From: /tf/active/vicechatdev/docchat/app.py
  • function index_v4 85.5% similar

    Flask route handler for the root URL ('/') that redirects authenticated users to the document workspace and unauthenticated users to the login page.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function index 81.8% similar

    Flask route handler that serves as the main landing page for authenticated users, displaying their documents and text sections in a workspace interface.

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