Skip to content

Commit

Permalink
[ASL-5124] add phone field in qubic user 3
Browse files Browse the repository at this point in the history
* fixed hydrate user issue
  • Loading branch information
roadmanfong committed Apr 2, 2024
1 parent 51d68c4 commit 26c7364
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions packages/core/src/QubicConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,22 @@ export class QubicConnect {

this.handleRedirectResult();
initGaTrack(this.config.trackGaSettings);
// when state changed, persist user
this.onAuthStateChanged(QubicConnect.persistUser);
// hydrateUser will trigger handleLogin/handleLogout
this.hydrateUser()
.then(() => {
this.onAuthStateChanged(QubicConnect.persistUser);
.then(hasSavedUser => {
this.isUserReady = true;
// no stored user and no redirect result or error
if (!hasSavedUser && !this.hasRedirectResult) {
this.handleLogout(null);
}

if (!this.user && this.shouldAutoLoginInWalletIab) {
this.loginWithWallet(window.ethereum?.isQubic ? 'qubic' : 'metamask');
}
})
.finally(() => {
.catch(() => {
this.isUserReady = true;
});
}
Expand All @@ -204,9 +211,9 @@ export class QubicConnect {
}
}

private async hydrateUser() {
private async hydrateUser(): Promise<boolean> {
const saved = localStorage.getItem(USER_STORAGE_KEY);
if (!saved) return;
if (!saved) return false;
try {
const user = JSON.parse(saved) as WalletUser;
setAccessToken(user.accessToken);
Expand All @@ -215,23 +222,24 @@ export class QubicConnect {
provider.enable();
}
if (QubicConnect.ifTokenExpired(user.expiredAt)) {
this.handleLogout(null);
} else {
// login again to get qubicUser data if exists
const {
me: { qubicUser },
} = await getMe(this.marketRequestGraphql);

this.handleLogin(null, {
...user,
qubicUser,
provider,
});
return false;
}
// login again to get qubicUser data if exists
const {
me: { qubicUser },
} = await getMe(this.marketRequestGraphql);

this.handleLogin(null, {
...user,
qubicUser,
provider,
});
return true;
} catch (error) {
// ignore error
console.warn('can not recover user from localStorage');
}
return false;
}

private handleUserPurge() {
Expand Down Expand Up @@ -374,7 +382,9 @@ export class QubicConnect {

public async logout(): Promise<void> {
try {
await logout(this.fetch);
if (this.accessToken) {
await logout(this.fetch);
}
this.handleLogout(null);
} catch (error) {
if (error instanceof Error) {
Expand Down Expand Up @@ -502,6 +512,7 @@ export class QubicConnect {
return parsedQuery;
}

private hasRedirectResult = false;
private async handleRedirectResult(): Promise<void> {
try {
const responsePassToConnect = QubicConnect.getRedirectResultFromUrlAndClearUrl();
Expand All @@ -513,6 +524,8 @@ export class QubicConnect {
return;
}

this.hasRedirectResult = true;

if (responsePassToConnect.action === 'bind') {
// handle all bin error message here
if ('errorMessage' in responsePassToConnect) {
Expand Down

0 comments on commit 26c7364

Please sign in to comment.