Kol*_*Kol 3 jwt google-signin angular
我的角度网络应用程序中有一个谷歌登录按钮。成功登录后,我存储用户配置文件和 id 令牌。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));
});
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用 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));
});
}
);
}
Run Code Online (Sandbox Code Playgroud)
一切似乎都工作正常。我没有收到 JWT 尚未有效的错误,并且我的公司列表页面可以正常加载数据。
我决定使用选项 1,因为我得到了默认的 google 登录按钮。
有什么想法这两个代码之间有什么区别吗?
问题是您的服务器时间与 Google 服务器时间不同。当您验证从 google 收到的令牌时,该令牌可能会在 1 或 n 秒内有效。这就是你收到错误的原因JWT not yet valid
要修复它,您可以将服务器时间与谷歌服务器时间同步。谷歌文档如何做到这一点是https://developers.google.com/time/
| 归档时间: |
|
| 查看次数: |
11185 次 |
| 最近记录: |