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

标签 vue.js vue-component amazon-cognito

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

我正在使用 NPMwebpack。我已按照 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>

我看不到成功或失败的警报。 任何帮助将不胜感激!

最佳答案

你可以试试这个代码..为我工作。

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);
  }
}

关于vue.js - 无法在 amazon-cognito 中捕获用户处理程序的成功/失败事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48748463/

相关文章:

ios - 取消链接 AWS 链接登录的问题(例如 Facebook)

vue.js - 将 vue-router 组件解释为一个函数

vue.js - 如何正确地将数据从子组件传递到父组件以及将数据从父组件传递到子组件?

javascript - Vue.js - 如何在数组对象上实现计算属性?

vue.js - Vuetify - 单击时显示工具提示

amazon-web-services - AWS Cognito 用户池 - 恢复选项

vue.js keyup.enter() 专注于下一个输入不起作用

webpack - 使用 vue-cli 和 webpack 构建后无法加载任何 css 和 js 文件?

javascript - VueJS : How can I get the parameter of the class that was clicked on?

amazon-web-services - 如何将应用程序数据与 Cognito 用户池 + Cognito 身份池用户合并?