【发布时间】:2019-04-29 10:01:09
【问题描述】:
使用 AWS Amplify,我在我的 Angular 项目中configured Amazon Cognito 托管 UI 来尝试 Google 登录。
从现在开始,我可以调用托管 UI 并获得像这样的重定向 URL: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
});
【问题讨论】:
标签: angular amazon-cognito aws-amplify