代码之家  ›  专栏  ›  技术社区  ›  Stéphane GRILLON

Aws Cognito+Angular 7-如何同时创建用户并将用户分配给用户组

  •  0
  • Stéphane GRILLON  · 技术社区  · 5 年前

    同时 . 当你必须创造一个可观察的东西时,这对我来说很好。但是当有必要创建一个可观察的对象来提出两个请求时,我就不工作了。我想我们需要用 forkJoin flatMap . 我用角7所以RxJs 6.x。

    createUser(user: User): Observable<User[]> {
        const _self = this;
        const cognitoidentityserviceprovider = new AWSCognito.CognitoIdentityServiceProvider({
          credentials: this.getCognitoCreds()
        });
        const params = {
          UserPoolId: this.poolData.UserPoolId,
          Username: user.username,
          DesiredDeliveryMediums: [ 'EMAIL' ],
          ForceAliasCreation: false,
          MessageAction: 'SUPPRESS',
          TemporaryPassword: '1234fooo*',
          UserAttributes: [
            {
              Name: 'email',
              Value: user.email
            }
          ]
        };
        return Observable.create(obs => {
          cognitoidentityserviceprovider.adminCreateUser(params, function(error, data) {
            if (error) {
              console.log(error);
              return Observable.throw(error || 'Server error');
            } else {
              console.log(data);
              //
              //
              // HERE user is create  :)
              // I want add to group here (see code down in this post) 
              //
              //
              return obs.next(data);
            }
          });
        });
      }
    

    添加组码确定:

    const p = {
                GroupName: 'admin',
                UserPoolId: _self.poolData.UserPoolId,
                Username: user.username
              };
              return Observable.create(o => {
                cognitoidentityserviceprovider.adminAddUserToGroup(p, function(err, d) {
                  if (err) {
                    console.log(err);
                    return Observable.throw(err || 'Server error');
                  } else {
                    console.log(d);
                    return o.next(d);
                  }
                });
              });
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Stéphane GRILLON    5 年前

    我把第一个observate放在const中,然后返回这个observate.pipe(mergeMap((u:CognitoUser)=>{ 2nd observable }

    createUser(user: User): Observable<User[]> {
        const _self = this;
        const cognitoidentityserviceprovider = new AWSCognito.CognitoIdentityServiceProvider({
          credentials: this.getCognitoCreds()
        });
        const params = {
          UserPoolId: this.poolData.UserPoolId,
          Username: user.username,
          DesiredDeliveryMediums: [ 'EMAIL' ],
          ForceAliasCreation: false,
          MessageAction: 'SUPPRESS',
          TemporaryPassword: '1234Fooo*',
          UserAttributes: [
            {
              Name: 'email',
              Value: user.email
            }
          ]
        };
        const o = Observable.create(obs => {
          cognitoidentityserviceprovider.adminCreateUser(params, function(error, data) {
            if (error) {
              console.log(error);
              return Observable.throw(error || 'Server error');
            } else {
              console.log(data);
              return obs.next(data.User);
            }
          });
        });
        return o.pipe(mergeMap((u: CognitoUser) => {
          const p = {
            GroupName: 'admin',
            UserPoolId: _self.poolData.UserPoolId,
            Username: u.Username
          };
          return Observable.create(ob => {
            cognitoidentityserviceprovider.adminAddUserToGroup(p, function(err, d) {
              if (err) {
                console.log(err);
                return Observable.throw(err || 'Server error');
              } else {
                console.log(d);
                return ob.next(d);
              }
            });
          });
        }));
      }