1小时后,我的ionic / angular / firebase应用程序将用户注销。如何使用REST API实现刷新令牌,以使登录状态无限期持久?官方文档提供了URL和必需的参数,但没有有关如何或在何处实现的信息。
https://firebase.google.com/docs/reference/rest/auth#section-refresh-token
以下代码是登录功能。 idToken是用于在Auth Guard中进行非常身份验证的状态。我意识到在此处添加刷新令牌代码可能没有任何意义,但我对Firebase并不熟悉,也不确定刷新的工作原理。
login(userEmail: string, userPassword: string) {
let email: string;
let idToken: string;
let localId: string;
let refreshToken: string;
return this.http.post<AuthResponseData>(
`https://www.googleapis.com/identitytoolkit/v3/relyingparty
/verifyPassword?key=${environment.firebaseAPIKey}`,
{ email: userEmail, password: userPassword, returnSecureToken:
true }
).pipe(
take(1),
switchMap(acctData => {
idToken = acctData.idToken;
email = acctData.email;
localId = acctData.localId;
refreshToken = acctData.refreshToken;
return this.http.post<AuthResponseData>(
`https://securetoken.googleapis.com/v1/token?
key=${environment.firebaseAPIKey}`,
{ grant_type: 'refresh_token', refresh_token: refreshToken }
);
}),
tap(refreshData => {
const tokenExp = new Date(new Date().getTime() +
(+refreshData.expiresIn * 1000));
const acct = new Account(
localId,
email,
refreshData.idToken,
refreshData.refreshToken,
tokenExp
);
this._account.next(acct);
})
);
}