我通过谷歌开发者教程创建了谷歌登录。但遗憾的是,在用户登录并且他的所有凭据都可以在控制台中登录之后,我不知道如何处理他的会话以在用户配置文件中显示他的信息。用户登录后,他的信息已成功写入数据库并保存在userDataService中。我想在这样的个人资料中显示他的信息:
personaldata-profile.component.html:
<p>Name: <br>...{{userDataService.displayName}}</p>
personaldata-profile.components.ts:
export class PersonalDataProfileComponent
{
constructor( public userDataService: UserDataService, private router: ActivatedRoute)
{
}
}
login.components.ts
appStart = function () {
gapi.load('auth2', this.initSigninV2);
};
initSigninV2 = function () {
this.auth2 = gapi.auth2.init({
client_id: '..........apps.googleusercontent.com',scope: 'profile'
});
public googleInit() {
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.init({
client_id: '.......apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
scope: 'profile email'
});
this.attachSignin(document.getElementById('googleBtn'));
});
}
ngAfterViewInit() {
this.googleInit();
}
public attachSignin(element) {
this.googleUser
this.auth2.attachClickHandler(element, {},(googleUser) => {
const profile = googleUser.getBasicProfile();
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
this.userDataService.googleUserId = profile.getId();
this.userDataService.displayName = profile.getName();
this.userDataService.familyName = profile.getFamilyName();
this.userDataService.givenName = profile.getGivenName();
this.userDataService.email = profile.getEmail();
this.userDataService.imageUrl = profile.getImageUrl();
this.userDataService.createUser();
this.userDataService.redirect();
},
(error) => {
alert(JSON.stringify(error, undefined, 2));
});
}
我记录了很多,发现我的个人资料页面上的userDataService对象是空的。但是在redirect()
期间,我的个人资料页面上的userDataService对象不为空。
我做错了什么?