1

我正在使用React Native構建移動應用程序。藉助此庫https://github.com/AirLabsTeam/react-native-aws-cognito-js,我可以通過應用程序註冊並登錄用戶,並且Cognito會將訪問令牌返回給應用程序。驗證/驗證來自Rails的AWS Cognito訪問令牌

現在,應用程序將使用此令牌對Rails後端進行api調用。 (對於每個API調用)

1)Rails如何驗證令牌是否良好?

2)如何獲取用戶電子郵件?

3)是否有AWS的API端點?

4)有沒有類似這樣的例子嗎?

謝謝

回答

0

我可能已經找到了一種方法來做到這一點。

1)添加到您的Gemfile:

gem 'aws-sdk', '~> 2' 

2)添加這3 ENV變量:

export AWS_REGION=us-west-2 
export AWS_ACCESS_KEY_ID="Your AWS Access Key ID" 
export AWS_SECRET_ACCESS_KEY= "Your AWS Secret Access Key" 

3)創建一個新文件。我把以下內容放在app/modules/Aws.rb中

require 'aws-sdk' 

cognitoidentityprovider = Aws::CognitoIdentityProvider::Client.new(region: ENV['AWS_REGION']) 

begin 
    cognito_user = cognitoidentityprovider.get_user({ 
    access_token: "Token coming from the APP" 
    }) 

    email_verified = cognito_user.user_attributes 
           .detect{ |attribute_type| attribute_type["name"] === "email_verified" && attribute_type["value"] === "true" } 

    if email_verified.nil? 
    raise "Email is not verified" 
    else 
    puts "EMAIL IS GOOD!" 
    puts cognito_user.username 
    end 

rescue StandardError => msg 
    puts "ERROR!" 
    puts msg 
end 

我可以在用irb測試時返回用戶郵件。

不知道這是否是正確的方式(或AWS方式)獲取用戶電子郵件....我會很感激任何評論或反饋...