Bhr*_*jni 5 javascript angular angular-social-login
嗨,我在 Angular8 应用程序中使用angularx-social-login的版本2.2.1。由于 google auth 令牌过期 1 小时,我必须调用刷新令牌的方法。我浏览了许多网站,但无法获得解决方案。我已经在应用程序模块中安装和声明,现在使用登录组件来获取身份验证令牌和电子邮件地址,id 来自登录谷歌按钮点击。但我不知道如何为附近的到期时间调用刷新方法。请帮忙。
HTML:
<button type="submit" class="btn btn-primary btn-block" (click)="signInWithGoogle()">Sign in with Google</button>
Run Code Online (Sandbox Code Playgroud)
:
public signInWithGoogle(): void {
let socialUser = this.oauthService.signIn(GoogleLoginProvider.PROVIDER_ID).then((userData) => {
if(userData.idToken){
} else {
alert('danger')
}
});
}
Run Code Online (Sandbox Code Playgroud)
app.module.ts:
import { SocialLoginModule, AuthServiceConfig } from "angularx-social-login";
import { GoogleLoginProvider } from "angularx-social-login";
imports: [
SocialLoginModule
],
providers: [
{
provide: AuthServiceConfig,
useFactory: provideConfig
}],
let config = new AuthServiceConfig([
{
id: GoogleLoginProvider.PROVIDER_ID,
provider: new GoogleLoginProvider("1051927480520-u87cv859uap5e6t4r5f52mk96a8a6nhe.apps.googleusercontent.com")
},
]);
export function provideConfig() {
return config;
}
Run Code Online (Sandbox Code Playgroud)
因此,从演示中,我可以获得我选择的 Gmail 帐户的详细信息,但是令牌会在一个小时后过期,我需要在它到期之前获取刷新令牌。请帮忙
小智 0
我不确定这个问题是否相关,但是SocialAuthService从angularx-social-login包中实际上公开了执行此操作的方法 -refreshAuthToken(providerId: string): Promise<void>;
您可以简单地执行以下操作:
refreshUserToken() {
this.socialAuthService.refreshAuthToken(GoogleLoginProvider.PROVIDER_ID).then(() => {
// Any post-refresh actions if needed
});
}
Run Code Online (Sandbox Code Playgroud)
或者:
async refreshUserToken() {
await this.socialAuthService.refreshAuthToken(GoogleLoginProvider.PROVIDER_ID);
}
Run Code Online (Sandbox Code Playgroud)
如果您已经订阅了authState更改,则刷新后您将获得新的SocialUser(带有 new、refreshed authToken)。请参阅 Github 上的此方法实现。
免责声明:查看实现,它仅适用于 Google 登录。
| 归档时间: |
|
| 查看次数: |
877 次 |
| 最近记录: |