在Angular中使用Cognito托管UI检索用户

时间:2018-11-27 15:56:22

标签: angular amazon-cognito aws-amplify

使用AWS Amplify,我在我的Angular项目中configured使用Amazon Cognito托管UI来尝试Google登录。

从现在开始,我可以调用托管的用户界面,并具有如下所示的重定向网址:http://localhost:4200/authenticated?code=f4c34ad6

使用接收到的代码,我想检索当前用户。

根据此post,我可以这样做:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Auth, Hub } from 'aws-amplify';

@Component({
  selector: 'app-authenticated',
  templateUrl: './authenticated.component.html',
  styleUrls: ['./authenticated.component.css']
})
export class AuthenticatedComponent implements OnInit {

  constructor(
    public router: Router
  ) {
    console.log('constructor');
    Hub.listen('auth', this, 'AuthCodeListener');
  }

  ngOnInit() {
  }

  onHubCapsule(capsule: any) {
    const { channel, payload } = capsule; // source
    if (channel === 'auth' && payload.event === 'signIn') {
      Auth.currentAuthenticatedUser().then((data) => {
        console.log('---', data) // THIS WORKS for Hosted UI!
        // redirect to other page
      });
    }
  }

}

但是没有成功。 onHubCapsule不会显示在控制台上。

这是我用来调用托管UI的代码:

import { Component } from '@angular/core';

@Component({
  selector: 'app-home',
  template: `
    <button class="button" (click)="login()">Log in with 
Google</button>
  `
})
export class HomeComponent {
  private url = 'https://' + 'auth-social.auth.eu-central-1.amazoncognito.com' + '/login?redirect_uri=' + 'http://localhost:4200/authenticated' + '&response_type=' + 'code' + '&client_id=' + '7b9pefeu654i7u342******';

  login() {
    window.location.assign(this.url);
  }

}

缺少什么来检索用户?

感谢您的帮助。

编辑-2018年7月12日

在我的main.ts中,我具有以下配置:

import Amplify from 'aws-amplify';
import amplify from './aws-exports';

Amplify.configure({
  Auth: {
    // Domain name
    domain: 'auth-social.auth.eu-central-1.amazoncognito.com',

    // Authorized scopes
    scope: ['phone', 'email', 'profile', 'openid', 'aws.cognito.signin.user.admin'],

    // Callback URL
    redirectSignIn: 'http://localhost:4200/authenticated',

    // Sign out URL
    redirectSignOut: 'http://localhost:4200',

    // 'code' for Authorization code grant, 
    // 'token' for Implicit grant
    responseType: 'code',

    // optional, for Cognito hosted ui specified options
    options: {
      // Indicates if the data collection is enabled to support Cognito advanced security features. By default, this flag is set to true.
      AdvancedSecurityDataCollectionFlag: true
    }
  },
  amplify
});

1 个答案:

答案 0 :(得分:1)

添加此oauth配置,它将起作用:

https://aws-amplify.github.io/docs/js/authentication#configuring-the-hosted-ui

因此配置应如下所示:

import Amplify from 'aws-amplify';
import amplify from './aws-exports';

const oauth = {
    // Domain name
    domain : 'your-domain-prefix.auth.us-east-1.amazoncognito.com', 

    // Authorized scopes
    scope : ['phone', 'email', 'profile', 'openid','aws.cognito.signin.user.admin'], 

    // Callback URL
    redirectSignIn : 'http://www.example.com/signin', 

    // Sign out URL
    redirectSignOut : 'http://www.example.com/signout',

    // 'code' for Authorization code grant, 
    // 'token' for Implicit grant
    responseType: 'code',

    // optional, for Cognito hosted ui specified options
    options: {
        // Indicates if the data collection is enabled to support Cognito advanced security features. By default, this flag is set to true.
        AdvancedSecurityDataCollectionFlag : true
    }
}

Amplify.configure({
  ...amplify,
  Auth: {
    oauth: oauth
  }
});