Ruby on rails + aws congnito的例子

时间:2016-12-19 12:04:47

标签: ruby-on-rails ruby heroku devise amazon-cognito

我正在构建部署在rails 5上的heroku应用。 我想使用AWS congnito来实现单点登录,但是没有足够的示例来实现它。 我正在使用devise进行身份验证。现在我的目标是将我的所有用户都放在AWS cognito上,并通过我的rails App验证它们。

这是我在AWS congnito with rails找到的唯一资源,我正在寻找一些示例应用程序或指向工具或ruby API文档的链接来实现此目的。

请帮助。

  

根据Bala答案更新

require 'aws-sdk'

ENV['AWS_ACCESS_KEY_ID'] = 'XXXXXXXXXXXXXXXXX'
ENV['AWS_SECRET_ACCESS_KEY'] = 'XXXX+XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
region_name = 'us-east-1'
endpoint = 'cognito-idp.us-east-1.amazonaws.com'

client = Aws::CognitoIdentityProvider::Client.new(
  region: region_name
)


resp = client.admin_create_user({
  user_pool_id: "us-east-1_iD7xNHj0x", # required
  username: "Test", # required
  user_attributes: [
    {
      name: "email", # required
      value: "sachin.singh@example.com",
    },
  ],
  validation_data: [
    {
      name: "Email", # required
      value: "AttributeValueType",
    },
  ],
  temporary_password: "PasswordType",
  force_alias_creation: false,
  message_action: "RESEND", # accepts RESEND, SUPPRESS
  desired_delivery_mediums: ["EMAIL"], # accepts SMS, EMAIL
})
  

错误堆栈跟踪

home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/seahorse/client/plugins/raise_response_errors.rb:15:in `call': User does not exist. (Aws::CognitoIdentityProvider::Errors::UserNotFoundException)
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/aws-sdk-core/plugins/idempotency_token.rb:18:in `call'
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/aws-sdk-core/plugins/param_converter.rb:20:in `call'
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/seahorse/client/plugins/response_target.rb:21:in `call'
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/seahorse/client/request.rb:70:in `send_request'
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/seahorse/client/base.rb:207:in `block (2 levels) in define_operation_methods'
    from aws_cognito.rb:20:in `<main>'
  

更新2

resp = client.admin_initiate_auth({
  user_pool_id: "us-east-1_uKM", # required
  client_id: "3g766413826eul9kre28qne4f", # required
  auth_flow: "ADMIN_NO_SRP_AUTH",
  auth_parameters: {
    "EMAIL" => "kapil.sachdev@metacube.com",
    "PASSWORD" => "Ibms#1234"
  }
})

1 个答案:

答案 0 :(得分:5)

首先,您需要为应用程序创建用户池

使用此link通过AWS控制台创建用户池

您可以在http://docs.aws.amazon.com/sdkforruby/api/Aws/CognitoIdentityProvider/Client.html找到sign_up,sign_in,更改密码和许多其他功能的ruby方法

修改

现在,您可以使用sign_up

注册用户

sign_in用户使用 admin_initiate_auth

如果您需要手机号码确认,确认电子邮件,则需要配置您正在创建的用户池。

您可以使用http://docs.aws.amazon.com/sdkforruby/api/Aws/CognitoIdentityProvider/Client.html#confirm_sign_up-instance_method

找到确认手机号码的相应方法