【发布时间】:2018-10-17 13:29:00
【问题描述】:
我的 Angular Web 应用程序中有一个 google 登录按钮。 成功登录后,我存储用户配置文件和 id-token。 id-token 设置在我发送到自定义 api 的每个请求标头中。
我目前遇到以下代码的 2 个问题:
onGoogleSignInSuccess(event: GoogleSignInSuccess) {
var profile = event.googleUser.getBasicProfile();
var id_token = event.googleUser.getAuthResponse().id_token;
// store google user data in local storage
localStorage.setItem('googleUserProfile', JSON.stringify(profile));
localStorage.setItem('googleIdToken', JSON.stringify(id_token));
// check user's email address exist in Felix
this.userService.getUserByEmail(profile.getEmail()).subscribe(InUser => {
this.globalService.setCurrentUser(InUser);
this.router.navigate(['companylist']);
}, error => {
this.errorLoggingIn = true;
this.errorCode = error.status;
console.log('error logging in: ' + JSON.stringify(error));
});
}
- 大多数情况下,我的 API 在尝试使用 google 验证 id-token 时会抛出“JWT not yet valid”错误。它似乎每 4 次尝试一次。
- 导航到“companyList”时,它没有点击 ngOnInit
但是,当我使用 npm 模块 sngular5-social-auth 并实现以下代码时:
public socialSignIn(socialPlatform: string) {
let socialPlatformProvider;
socialPlatformProvider = GoogleLoginProvider.PROVIDER_ID;
this.socialAuthService.signIn(socialPlatformProvider).then(
(userData) => {
console.log(socialPlatform + " sign in data : ", userData);
// store google user data in local storage
localStorage.setItem('currentGoogleUserData', JSON.stringify(userData));
this.userService.getUserByEmail(userData.email).subscribe(InUser => {
this.globalService.setCurrentUser(InUser);
this.router.navigate(['companylist']);
}, error => {
this.errorLoggingIn = true;
this.errorCode = error.status;
console.log('error logging in: ' + JSON.stringify(error));
});
}
);
}
似乎一切正常。我没有收到 JWT not yet valid 错误,而且我的公司列表页面可以正常加载数据。
我决定使用选项 1,因为我获得了默认的 google 登录按钮。
任何想法这两个代码之间的区别是什么?
【问题讨论】:
-
见stackoverflow.com/questions/49696001/…。如果身份验证代码流量较低,则可以轻松修复一小段时间。我没有查看 angular5-social-auth 的来源,但他们可能实现了自定义时钟,这可以避免您在直接调用 API 时遇到的问题。
标签: angular jwt google-signin