Skip to main content

AuthenticationMiddleware

This class manages user authentication by attaching a user object to each incoming request. It utilizes lazy loading to efficiently retrieve the user from the session and provides both synchronous and asynchronous methods for accessing the authenticated user instance.

Constructor

Signature

def AuthenticationMiddleware(
get_response: callable
) - > null

Parameters

NameTypeDescription
get_responsecallableThe next middleware or view in the chain to be called.

Methods


process_request()

@classmethod
def process_request(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > null

Populates the request object with the currently authenticated user using a lazy object for performance. This method ensures that session middleware is correctly configured before attaching the user retrieval logic to the request.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming HTTP request object to be populated with authentication data.

Returns

TypeDescription
nullNothing is returned, but the request object is modified in-place with 'user' and 'auser' attributes.