javascript - Google Auth 用户加密属性
问题描述
我在一个网络项目上使用谷歌身份验证,我试图理解属性名称。屏幕截图显示了user
google 返回的对象。我可以通过id_token
这种方式访问:
this.user.Zb.id_token
为什么Zb
?一年前是这样的:
this.user.wc.id_token
请注意,那是wc
当时,现在我的 UI 代码中断了。我错过了什么?为什么使用这些属性名称?如何制作它以便id_token
无论其父属性名称如何我都可以访问?
这是用户界面代码:
async authenticate(): Promise<gapi.auth2.GoogleUser> {
// Initialize gapi if not done yet
if (!this.gapiSetup) {
await this.initGoogleAuth();
}
// Resolve or reject signin Promise
return new Promise(async () => {
await this.authInstance.signIn().then(
user => {
this.user = user;
console.log('this.user: ', this.user);
this.cookieService.set('jwt', this.user.Zb.id_token, 365); // expires in one year (365 days)
// this.cookieService.set('jwt', this.user.wc.id_token, 365); // expires in one year (365 days)
this.owlerApiService.getHooterUsingIdTokenFromProvider()
.subscribe((data: any) => {
this.userDto = data;
},
error => {
console.log('error: ', error);
});
},
error => this.error = error);
});
}
async initGoogleAuth(): Promise<void> {
// Create a new Promise where the resolve function is the callback passed to gapi.load
const pload = new Promise((resolve) => {
gapi.load('auth2', resolve);
});
// When the first promise resolves, it means we have gapi loaded and that we can call gapi.init
return pload.then(async () => {
// ClientId safe to put here? Looks like it:
// https://stackoverflow.com/a/62123945/279516
await gapi.auth2
.init({ client_id: 'xxx.apps.googleusercontent.com' })
.then(auth => {
this.gapiSetup = true;
this.authInstance = auth;
});
});
}
解决方案
我不相信您正在以预期的形式使用 SDK;我不清楚您如何确定这是访问经过id_token
身份验证的用户的值的正确方法,因为官方文档中的任何地方都没有提到它。
signIn()
返回 的实例GoogleUser
,您可以在其上调用getAuthResponse()
。返回的gapi.auth2.AuthResponse
对象包括id_token
:
await this.authInstance.signIn().then(
user => {
this.user = user;
console.log('this.user: ', this.user);
this.cookieService.set('jwt', this.user.getAuthResponse().id_token, 365); // expires in one year (365 days)
// …
推荐阅读
- django - 如何处理上下文变量中的 django url 标签
- android - 如何检测音频当前是否正在 Android 手机扬声器上播放?
- python - Python套接字服务器,用户有没有办法在终端上输入端口?
- javascript - Ordering a bar chart labels in dc.js (again)
- python - ElasticSearch,将值附加到字段,但首先检查字段是否存在,如果不创建字段则追加
- c++ - 将 UDP 数据包发送到运行 Debian 的设备时出现问题
- spring - Spring WebClient 返回 Lambda 而不是来自 Rest Service 的数据
- angular - 在 Azure App 服务上部署多个 Angular 单页应用程序
- javascript - 地图折线,停止绘图
- java - 如何将静态库链接到在 NDK Android Build 中用作本机库的源