Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

[Session] Call persist handler directly (#3828)

authored by

dan and committed by
GitHub
6405ad7c b86c3b48

+62 -95
+62 -95
src/state/session/index.tsx
··· 1 1 import React from 'react' 2 - import {AtpPersistSessionHandler, BskyAgent} from '@atproto/api' 2 + import {AtpSessionData, AtpSessionEvent, BskyAgent} from '@atproto/api' 3 3 4 4 import {track} from '#/lib/analytics/analytics' 5 5 import {networkRetry} from '#/lib/async/retry' ··· 93 93 })) 94 94 }, [setState]) 95 95 96 - const createPersistSessionHandler = React.useCallback( 96 + const onAgentSessionChange = React.useCallback( 97 97 ( 98 98 agent: BskyAgent, 99 99 account: SessionAccount, 100 - persistSessionCallback: (props: { 101 - expired: boolean 102 - refreshedAccount: SessionAccount 103 - }) => void, 104 - { 105 - networkErrorCallback, 106 - }: { 107 - networkErrorCallback?: () => void 108 - } = {}, 109 - ): AtpPersistSessionHandler => { 110 - return function persistSession(event, session) { 111 - const expired = event === 'expired' || event === 'create-failed' 100 + event: AtpSessionEvent, 101 + session: AtpSessionData | undefined, 102 + ) => { 103 + const expired = event === 'expired' || event === 'create-failed' 112 104 113 - if (event === 'network-error') { 114 - logger.warn( 115 - `session: persistSessionHandler received network-error event`, 116 - ) 117 - networkErrorCallback?.() 118 - return 119 - } 105 + if (event === 'network-error') { 106 + logger.warn( 107 + `session: persistSessionHandler received network-error event`, 108 + ) 109 + clearCurrentAccount() 110 + return 111 + } 120 112 121 - // TODO: use agentToSessionAccount for this too. 122 - const refreshedAccount: SessionAccount = { 123 - service: account.service, 124 - did: session?.did || account.did, 125 - handle: session?.handle || account.handle, 126 - email: session?.email || account.email, 127 - emailConfirmed: session?.emailConfirmed || account.emailConfirmed, 128 - emailAuthFactor: session?.emailAuthFactor || account.emailAuthFactor, 129 - deactivated: isSessionDeactivated(session?.accessJwt), 130 - pdsUrl: agent.pdsUrl?.toString(), 113 + // TODO: use agentToSessionAccount for this too. 114 + const refreshedAccount: SessionAccount = { 115 + service: account.service, 116 + did: session?.did || account.did, 117 + handle: session?.handle || account.handle, 118 + email: session?.email || account.email, 119 + emailConfirmed: session?.emailConfirmed || account.emailConfirmed, 120 + emailAuthFactor: session?.emailAuthFactor || account.emailAuthFactor, 121 + deactivated: isSessionDeactivated(session?.accessJwt), 122 + pdsUrl: agent.pdsUrl?.toString(), 131 123 132 - /* 133 - * Tokens are undefined if the session expires, or if creation fails for 134 - * any reason e.g. tokens are invalid, network error, etc. 135 - */ 136 - refreshJwt: session?.refreshJwt, 137 - accessJwt: session?.accessJwt, 138 - } 124 + /* 125 + * Tokens are undefined if the session expires, or if creation fails for 126 + * any reason e.g. tokens are invalid, network error, etc. 127 + */ 128 + refreshJwt: session?.refreshJwt, 129 + accessJwt: session?.accessJwt, 130 + } 139 131 140 - logger.debug(`session: persistSession`, { 141 - event, 142 - deactivated: refreshedAccount.deactivated, 143 - }) 132 + logger.debug(`session: persistSession`, { 133 + event, 134 + deactivated: refreshedAccount.deactivated, 135 + }) 144 136 145 - if (expired) { 146 - logger.warn(`session: expired`) 147 - emitSessionDropped() 148 - } 137 + if (expired) { 138 + logger.warn(`session: expired`) 139 + emitSessionDropped() 140 + } 149 141 150 - /* 151 - * If the session expired, or it was successfully created/updated, we want 152 - * to update/persist the data. 153 - * 154 - * If the session creation failed, it could be a network error, or it could 155 - * be more serious like an invalid token(s). We can't differentiate, so in 156 - * order to allow the user to get a fresh token (if they need it), we need 157 - * to persist this data and wipe their tokens, effectively logging them 158 - * out. 159 - */ 160 - persistSessionCallback({ 161 - expired, 162 - refreshedAccount, 163 - }) 164 - } 142 + /* 143 + * If the session expired, or it was successfully created/updated, we want 144 + * to update/persist the data. 145 + * 146 + * If the session creation failed, it could be a network error, or it could 147 + * be more serious like an invalid token(s). We can't differentiate, so in 148 + * order to allow the user to get a fresh token (if they need it), we need 149 + * to persist this data and wipe their tokens, effectively logging them 150 + * out. 151 + */ 152 + upsertAccount(refreshedAccount, expired) 165 153 }, 166 - [], 154 + [clearCurrentAccount, upsertAccount], 167 155 ) 168 156 169 157 const createAccount = React.useCallback<SessionApiContext['createAccount']>( ··· 191 179 }, 192 180 ) 193 181 194 - agent.setPersistSessionHandler( 195 - createPersistSessionHandler( 196 - agent, 197 - account, 198 - ({expired, refreshedAccount}) => { 199 - upsertAccount(refreshedAccount, expired) 200 - }, 201 - {networkErrorCallback: clearCurrentAccount}, 202 - ), 203 - ) 182 + agent.setPersistSessionHandler((event, session) => { 183 + onAgentSessionChange(agent, account, event, session) 184 + }) 204 185 205 186 __globalAgent = agent 206 187 await fetchingGates ··· 210 191 track('Create Account') 211 192 logEvent('account:create:success', {}) 212 193 }, 213 - [upsertAccount, clearCurrentAccount, createPersistSessionHandler], 194 + [upsertAccount, onAgentSessionChange], 214 195 ) 215 196 216 197 const login = React.useCallback<SessionApiContext['login']>( ··· 223 204 authFactorToken, 224 205 }) 225 206 226 - agent.setPersistSessionHandler( 227 - createPersistSessionHandler( 228 - agent, 229 - account, 230 - ({expired, refreshedAccount}) => { 231 - upsertAccount(refreshedAccount, expired) 232 - }, 233 - {networkErrorCallback: clearCurrentAccount}, 234 - ), 235 - ) 207 + agent.setPersistSessionHandler((event, session) => { 208 + onAgentSessionChange(agent, account, event, session) 209 + }) 236 210 237 211 __globalAgent = agent 238 212 // @ts-ignore ··· 245 219 track('Sign In', {resumedSession: false}) 246 220 logEvent('account:loggedIn', {logContext, withPassword: true}) 247 221 }, 248 - [upsertAccount, clearCurrentAccount, createPersistSessionHandler], 222 + [upsertAccount, onAgentSessionChange], 249 223 ) 250 224 251 225 const logout = React.useCallback<SessionApiContext['logout']>( ··· 280 254 agent.pdsUrl = agent.api.xrpc.uri = new URL(account.pdsUrl) 281 255 } 282 256 283 - agent.setPersistSessionHandler( 284 - createPersistSessionHandler( 285 - agent, 286 - account, 287 - ({expired, refreshedAccount}) => { 288 - upsertAccount(refreshedAccount, expired) 289 - }, 290 - {networkErrorCallback: clearCurrentAccount}, 291 - ), 292 - ) 257 + agent.setPersistSessionHandler((event, session) => { 258 + onAgentSessionChange(agent, account, event, session) 259 + }) 293 260 294 261 // @ts-ignore 295 262 if (IS_DEV && isWeb) window.agent = agent ··· 380 347 return sessionAccount 381 348 } 382 349 }, 383 - [upsertAccount, clearCurrentAccount, createPersistSessionHandler], 350 + [upsertAccount, onAgentSessionChange], 384 351 ) 385 352 386 353 const removeAccount = React.useCallback<SessionApiContext['removeAccount']>(