2

I am new in VueJS and Amazon-cognito and trying to sign in using my simple VueJS application.

I am using NPM and webpack. I have followed the steps from amazon-cognito-auth-js

Below is my script tag.

<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>

I am unable to see the alert of neither success nor failure. Any help will be highly appreciated!

Jayesh Dhandha
  • 1,983
  • 28
  • 50

1 Answers1

0

You can try this code.. working for me.

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);
  }
}
Mangesh Daundkar
  • 1,026
  • 1
  • 15
  • 20