Skip to content

Commit

Permalink
Merge pull request #94 from getamis/ASL-5124/add-phone-field-in-qubic…
Browse files Browse the repository at this point in the history
…-user-3

[ASL-5124] add phone field in qubic user 3
  • Loading branch information
roadmanfong authored Apr 2, 2024
2 parents 51d68c4 + 25a9aea commit 19b0443
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions packages/core/src/QubicConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,20 @@ export class QubicConnect {

this.handleRedirectResult();
initGaTrack(this.config.trackGaSettings);
this.hydrateUser()
.then(() => {
this.onAuthStateChanged(QubicConnect.persistUser);
// when state changed, persist user
this.onAuthStateChanged(QubicConnect.persistUser);
// hydrateUser will trigger handleLogin/handleLogout
this.hydrateUser().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(() => {
this.isUserReady = true;
});
if (!this.user && this.shouldAutoLoginInWalletIab) {
this.loginWithWallet(window.ethereum?.isQubic ? 'qubic' : 'metamask');
}
});
}

private static persistUser(user: WalletUser | null) {
Expand All @@ -204,9 +207,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 +218,25 @@ 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');
console.warn('hydrate user failed');
console.warn(error);
}
return false;
}

private handleUserPurge() {
Expand Down Expand Up @@ -374,7 +379,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 +509,7 @@ export class QubicConnect {
return parsedQuery;
}

private hasRedirectResult = false;
private async handleRedirectResult(): Promise<void> {
try {
const responsePassToConnect = QubicConnect.getRedirectResultFromUrlAndClearUrl();
Expand All @@ -513,6 +521,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 19b0443

Please sign in to comment.