AWS Cognito 不允许在 Allowed Callback URLs 中使用占位符变量。但是,您可以使用 AWS Lambda 在确认注册前对回调URL进行处理。以下是示例代码:
import json
import urllib.parse
def lambda_handler(event, context):
# Extract the callback URL from the event
callback_url = event['request']['clientMetadata']['callback_url']
# Extract the user's email address from the event
email = event['request']['userAttributes']['email']
# Replace the placeholder with the email address
processed_url = callback_url.replace('{email}', urllib.parse.quote(email))
# Update the event's clientMetadata to include the processed URL
event['request']['clientMetadata']['processed_url'] = processed_url
return event
在此示例中,Lambda 使用事件中传递的客户端元数据中的回调URL和用户属性中提取的电子邮件地址,然后使用 urllib.parse.quote 函数对电子邮件地址进行编码,并将其替换回调URL中的占位符。最终,Lambda 将包含已处理的URL的客户端元数据返回给 Cognito,在确认注册之前 Cognito 将使用这些元数据来处理回调URL。