Skip to content

Commit

Permalink
⚡️ Make login resume process async to improve load speed
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Aug 10, 2020
1 parent 052ba57 commit a6f3043
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
24 changes: 16 additions & 8 deletions app/models/authcore-store/authcore-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const AuthCoreStoreModel = types
accessToken: "",
idToken: "",
hasSignedIn: false,
pendingInit: null,
}))
.extend(withEnvironment)
.views(self => ({
Expand All @@ -43,6 +44,7 @@ export const AuthCoreStoreModel = types
self.hasSignedIn = value
},
fetchCurrentUser: flow(function * () {
if (self.pendingInit) yield self.pendingInit
const currentUser: any = yield self.env.authCoreAPI.getCurrentUser(self.accessToken)
self.profile = AuthCoreUserModel.create(currentUser)
}),
Expand Down Expand Up @@ -73,6 +75,7 @@ export const AuthCoreStoreModel = types
} = yield self.env.authCoreAPI.setupModules(refreshToken, accessToken)
self.accessToken = newAccessToken
self.cosmosAddresses.replace(addresses)
self.pendingInit = null
}),
signOut: flow(function * () {
self.setHasSignedIn(false)
Expand Down Expand Up @@ -104,17 +107,22 @@ export const AuthCoreStoreModel = types
])
}),
resume: flow(function * () {
const [
{ password: refreshToken },
{ password: accessToken },
{ password: idToken },
]: any = yield Promise.all([
const promises = Promise.all([
Keychain.load(self.getCredentialKeyFor("refresh_token")),
Keychain.load(self.getCredentialKeyFor("access_token")),
Keychain.load(self.getCredentialKeyFor("id_token")),
])
yield self.init(refreshToken, accessToken, idToken)
self.setHasSignedIn(true)
]).then((res) => {
const [
{ password: refreshToken },
{ password: accessToken },
{ password: idToken },
] = res
return self.init(refreshToken, accessToken, idToken)
}).then(() => {
self.setHasSignedIn(true)
})
self.pendingInit = promises
yield promises
}),
}))

Expand Down
7 changes: 4 additions & 3 deletions app/models/root-store/setup-root-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ export async function setupRootStore() {
// Setup Authcore
env.setupAuthCore()
if (rootStore.userStore.currentUser) {
await rootStore.userStore.authCore.resume()
const address = rootStore.userStore.authCore.primaryCosmosAddress
rootStore.chainStore.setupWallet(address)
rootStore.userStore.authCore.resume().then(() => {
const address = rootStore.userStore.authCore.primaryCosmosAddress
rootStore.chainStore.setupWallet(address)
})
}
} catch (e) {
// if there's any problems loading, then let's at least fallback to an empty state
Expand Down
3 changes: 2 additions & 1 deletion app/screens/auth-loading-screen/auth-loading-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ export class AuthLoadingScreen extends React.Component<AuthLoadingScreenProps, {
currentUser: likeCoUser,
authCore: {
profile: authcoreUser,
pendingInit: authcoreIsSettingUp,
},
iapStore: {
isEnabled: isEnabledIAP,
hasSubscription,
},
} = this.props.userStore
if (authcoreUser && likeCoUser) {
if ((!!authcoreIsSettingUp || authcoreUser) && likeCoUser) {
try {
await Promise.all([
this.props.userStore.fetchUserInfo(),
Expand Down

0 comments on commit a6f3043

Please sign in to comment.