如何处理承诺链中的多个错误?

奥西克

我使用 AWS Amplify 进行身份验证,使用 Stripe 进行支付以创建注册页面。

问题:我找不到将电子邮件和密码部分(来自 AWS Amplify)的验证与付款信息部分(来自 Stripe)结合起来的方法。

我当前的代码创建了一个 Stripe 令牌并调用 API(带有有效的付款信息),然后处理错误消息,userSignupRequest从中处理电子邮件和密码字段。

如何使用付款信息验证电子邮件和密码,然后在 AWS 和 Stripe 中创建帐户?

在此处输入图片说明

  // Stripe payment process
  this.props.stripe.createToken(
    {
      email: this.state.email
    }
  ).then(result => {
    // PROBLEM: Form server validation from Stripe
    if(result.error){
      return this.setState({ errors: { errorMsg: result.error.message }, isLoading: false })
    }

    // if success, create customer and subscription with result.token.id
    const apiName = 'NameOfAPI';
    const path = '/stripe/signup';
    let myInit = {
      body: {
        "stripeToken": result.token.id,
        "email": this.state.email
      }
    }

    API.post(apiName , path, myInit).then(reponse => {
      this.props.userSignupRequest(this.state.email, this.state.password, reponse).then(user => {
        this.setState({
          confirmAccount: true,
          isLoading: false,
          userEmail: this.state.email,
          errors: {}
        })
        this.props.history.push('/signup#confirm-account')
      }).catch(err => {
        // PROBLEM: Form server validation 
        this.setState({ errors: { errorMsg: err.message }, isLoading: false })
      })

    }).catch(err => {
      console.log(err)
      this.setState({ errors: { errorMsg: err }, isLoading: false })
    });

  })
斜线0

It seems like we have a very similar stack. My solution was to handle everything server-side. You'll need to give your lambda functions the appropriate IAM permissions to access Cognito. The code below is a little long. I use async/await, which really cleans things up for me. You'll need to use Lambda with node 8 to use async/await though.

我验证所有内容都与客户端的正确格式匹配(即电子邮件实际上是电子邮件,密码长度正确)。我意识到可能出现的唯一错误是来自 Cognito 的“现有用户”错误。这个想法是:在您尝试使用 Stripe 注册该用户之前测试该用户是否存在。没有办法“测试”用户的信用卡是否对 Stripe 有效。要么全有要么全无。如果有效,它将通过,否则,您将收到错误消息。如果通过,您可以使用 Cognito 注册用户,知道您不应该收到错误(您已经在客户端验证了电子邮件和密码,并且您知道该用途尚不存在)。

作为参考,这里是cognitoaws-sdk

const AWS = require('aws-sdk');
const cognito = new AWS.CognitoIdentityServiceProvider({
  region: "region",
  userPoolId: "cognito_user_pool_id",
});

module.exports.signUpUser = (payload) => {
  const usernamePayload = {
    UserPoolId: "cognito_user_pool_id",
    Username: payload.email,
  };

  // I use emails for usernames.

    new Promise((resolve, reject) => {
      cognito.adminGetUser(usernamePayload, (error, response) => {
        if (error && error.code === 'UserNotFoundException') {
          resolve(false);
        } else if (error) {
          reject(error);
        } else {
          // if adminGetUser doesn't fail, it means the username exists
          resolve(true);
        }
      });
    }).then((usernameExists) => {
      if (!usernameExists) {
        // run stripe API stuff
        // always run before sign up below to catch stripe errors
        // and return those errors to client
        // before you sign up the user to Cognito

        // since you've already verified the user does not exist
        // it would be rare for an error to come up here
        // as long as you validate passwords and emails client-side
        const signUpPayload = {
          ClientId: "cognito_user_pool_client_id",
          Username: payload.email,
          Password: payload.password,
          UserAttributes: [
            {
              Name: 'email',
              Value: payload.email,
            },
          ],
        };

          new Promise((resolve, reject) => {
            cognito.signUp(signUpPayload, (error, response) => {
              if (error) {
                reject(error);
              } else {
                resolve(response);
              }
            });
          }).catch((error) => {
            // you should hopefully encounter no errors here
            // once you get everything setup correctly
            console.log(error);
          })
      } else {
        // means username already exists, send error to client
        // saying username exists
      }
    }).catch((error) => {
      // may want to dispatch this error to client
      console.log(error);
    });

  return null;
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何处理承诺中的错误?

来自分类Dev

如何处理承诺中的错误?

来自分类Dev

处理承诺链中的错误

来自分类Dev

处理承诺链中的错误

来自分类Dev

处理承诺链中的错误

来自分类Dev

处理承诺链中的错误

来自分类Dev

如何处理多个流中的错误?

来自分类Dev

如何处理成功或错误的承诺

来自分类Dev

如何处理 NodeJS 中的嵌套承诺

来自分类常见问题

如果根据条件返回后续的承诺,如何处理承诺链?

来自分类Dev

如果根据条件返回后续的承诺,如何处理承诺链?

来自分类Dev

如何处理完成闭包中的多个错误

来自分类Dev

如何处理地理位置承诺中的特定错误代码?

来自分类Dev

如何处理链式承诺

来自分类Dev

在使用承诺/构建承诺链时,您如何围绕/集成错误处理?

来自分类Dev

Celery如何处理链中的任务失败?

来自分类Dev

处理错误后跳过承诺链

来自分类Dev

如何处理可能是angularjs中$ q承诺的对象?

来自分类Dev

如何处理JavaScript中的多个收益

来自分类Dev

如何处理函数中的多个参数?

来自分类Dev

如何处理淘汰订阅中的错误

来自分类Dev

如何处理Shapely中的舍入错误

来自分类Dev

如何处理GRequests中的错误?

来自分类Dev

如何处理Retrofit 2.0中的错误

来自分类Dev

如何处理R降价中的错误?

来自分类Dev

如何处理Angular组件中的错误

来自分类Dev

如何处理NestJS中的TypeORM错误?

来自分类Dev

如何处理python中的递归错误?

来自分类Dev

如何处理fetch()中的错误请求