function login_v3
Flask route handler that displays the login page and redirects authenticated users to the document workspace.
/tf/active/vicechatdev/vice_ai/complex_app.py
641 - 647
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
flaskazure_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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality: