无法捕获amazon-cognito中用户处理程序的成功/失败事件

时间:2018-02-12 14:12:43

标签: vue.js vue-component amazon-cognito

我是VueJS和Amazon-cognito的新手,并尝试使用我的简单VueJS应用程序登录。

我正在使用 NPM webpack 。我已按照amazon-cognito-auth-js

中的步骤操作

以下是我的脚本标记。

<script>
  import {CognitoAuth} from amazon-cognito-auth-js/dist/amazon-cognito-auth

  export default {
    name: 'App',

methods: {
  initCognitoSDK: function() {
    var authData = {
      AppWebDomain: 'XXX.auth.us-west-2.amazoncognito.com',
      TokenScopesArray: ['phone', 'email', 'profile', 'openid'],
      AdvancedSecurityDataCollectionFlag: false,
      ClientId: 'XXXXXXXXXXXXXXXXXXXXXXX',
      RedirectUriSignIn: 'http://localhost:8080/index.html',
      RedirectUriSignOut: 'http://localhost:8080/sign-out.html'
    };

    var auth = new CognitoAuth(authData);
    auth.userhandler = {
      onSuccess: function (result) {
        alert("Sign in success");
        //showSignedIn(result);
        console.log(result);
      },
      onFailure: function (err) {
        alert("Error!" + err);
      }
    };

    return auth;
  }
}  
}
</script>

我无法看到成功和失败的警报。 任何帮助将受到高度赞赏!

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码..为我工作。

import { Component, OnInit } from '@angular/core';
import { CognitoAuth } from 'amazon-cognito-auth-js';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';
  auth: any;
  constructor() {
    //
  }

  ngOnInit() {
    this.auth = this.initCognitoSDK();
    this.auth.getSession();
    const curUrl = window.location.href;
    this.auth.parseCognitoWebResponse(curUrl);
  }

  initCognitoSDK() {
    const authData = {
      ClientId: 'xyz', // Your client id here
      AppWebDomain: 'xyz', // Exclude the "https://" part.
      TokenScopesArray: ['openid'], // like ['openid','email','phone']...
      RedirectUriSignIn: 'xyz',
      UserPoolId: 'xyz',
      RedirectUriSignOut: 'xyz',
      IdentityProvider: '', // e.g. 'Facebook',
      AdvancedSecurityDataCollectionFlag: false
    };

    const auth = new CognitoAuth(authData);

    auth.userhandler = {
      onSuccess: (result) => {
        alert('Sign in success');
        this.showSignedIn(result);
      },
      onFailure: (err) => {
        alert('Error!');
      }
    };
    auth.useCodeGrantFlow();

    return auth;
  }

  showSignedIn(session) {
    console.log('Session: ', session);
  }
}