async def METHOD_NAME(req, handler) -> Response:
    """
    Handle requests based on client type and authentication status.
    :param req: the request to handle
    :param handler: the handler to call with the request if authenticated
    :return: the response
    """
    db = req.app["db"]
    if isinstance(get_handler_policy(handler, req.method), PublicRoutePolicy):
        req["client"] = UserClient(
            db=db,
            administrator=False,
            force_reset=False,
            groups=[],
            permissions={},
            user_id=None,
            authenticated=False,
        )
        return await handler(req)
    if req.headers.get("AUTHORIZATION"):
        return await authenticate_with_key(req, handler)
    if get_config_from_req(req).use_b2c:
        try:
            return await authenticate_with_b2c(req, handler)
        except HTTPUnauthorized:
            # Allow authentication by both session and JWT in same instance.
            pass
    # Get session information from cookies.
    session_id = req.cookies.get("session_id")
    session_token = req.cookies.get("session_token")
    if session_id:
        try:
            session = await get_data_from_req(req).sessions.get_authenticated(
                session_id, session_token
            )
        except ResourceNotFoundError:
            try:
                session = await get_data_from_req(req).sessions.get_anonymous(
                    session_id
                )
            except ResourceNotFoundError:
                session = await get_data_from_req(req).sessions.create_anonymous(
                    get_ip(req)
                )
    else:
        session = await get_data_from_req(req).sessions.create_anonymous(get_ip(req))
    if session.authentication:
        user = await get_data_from_req(req).users.get(session.authentication.user_id)
        if user.active is False:
            raise HTTPUnauthorized(text="User is deactivated.")
        req["client"] = UserClient(
            db,
            user.administrator,
            user.force_reset,
            [group.id for group in user.groups],
            user.permissions.dict(),
            session.authentication.user_id,
            authenticated=True,
            session_id=session_id,
        )
    else:
        req["client"] = UserClient(
            db=db,
            administrator=False,
            force_reset=False,
            groups=[],
            permissions={},
            user_id=None,
            authenticated=False,
        )
    resp = await handler(req)
    if req.path != "/account/reset" and session.reset:
        try:
            await get_data_from_req(req).sessions.delete(session_id)
            session = await get_data_from_req(req).sessions.create_anonymous(
                get_ip(req)
            )
        except ResourceNotFoundError:
            pass
    set_session_id_cookie(resp, session.id)
    return resp