diff options
Diffstat (limited to 'src/state/session/index.tsx')
-rw-r--r-- | src/state/session/index.tsx | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index 6b1474839..b6748bfad 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -20,6 +20,7 @@ import {useCloseAllActiveElements} from '#/state/util' import {track} from '#/lib/analytics/analytics' import {hasProp} from '#/lib/type-guards' import {readLabelers} from './agent-config' +import {logEvent, LogEvents} from '#/lib/statsig/statsig' let __globalAgent: BskyAgent = PUBLIC_BSKY_AGENT @@ -54,17 +55,22 @@ export type ApiContext = { verificationPhone?: string verificationCode?: string }) => Promise<void> - login: (props: { - service: string - identifier: string - password: string - }) => Promise<void> + login: ( + props: { + service: string + identifier: string + password: string + }, + logContext: LogEvents['account:loggedIn']['logContext'], + ) => Promise<void> /** * A full logout. Clears the `currentAccount` from session, AND removes * access tokens from all accounts, so that returning as any user will * require a full login. */ - logout: () => Promise<void> + logout: ( + logContext: LogEvents['account:loggedOut']['logContext'], + ) => Promise<void> /** * A partial logout. Clears the `currentAccount` from session, but DOES NOT * clear access tokens from accounts, allowing the user to return to their @@ -76,7 +82,10 @@ export type ApiContext = { initSession: (account: SessionAccount) => Promise<void> resumeSession: (account?: SessionAccount) => Promise<void> removeAccount: (account: SessionAccount) => void - selectAccount: (account: SessionAccount) => Promise<void> + selectAccount: ( + account: SessionAccount, + logContext: LogEvents['account:loggedIn']['logContext'], + ) => Promise<void> updateCurrentAccount: ( account: Partial< Pick<SessionAccount, 'handle' | 'email' | 'emailConfirmed'> @@ -286,7 +295,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ) const login = React.useCallback<ApiContext['login']>( - async ({service, identifier, password}) => { + async ({service, identifier, password}, logContext) => { logger.debug(`session: login`, {}, logger.DebugContext.session) const agent = new BskyAgent({service}) @@ -329,24 +338,29 @@ export function Provider({children}: React.PropsWithChildren<{}>) { logger.debug(`session: logged in`, {}, logger.DebugContext.session) track('Sign In', {resumedSession: false}) + logEvent('account:loggedIn', {logContext, withPassword: true}) }, [upsertAccount, queryClient, clearCurrentAccount], ) - const logout = React.useCallback<ApiContext['logout']>(async () => { - logger.debug(`session: logout`) - clearCurrentAccount() - setStateAndPersist(s => { - return { - ...s, - accounts: s.accounts.map(a => ({ - ...a, - refreshJwt: undefined, - accessJwt: undefined, - })), - } - }) - }, [clearCurrentAccount, setStateAndPersist]) + const logout = React.useCallback<ApiContext['logout']>( + async logContext => { + logger.debug(`session: logout`) + clearCurrentAccount() + setStateAndPersist(s => { + return { + ...s, + accounts: s.accounts.map(a => ({ + ...a, + refreshJwt: undefined, + accessJwt: undefined, + })), + } + }) + logEvent('account:loggedOut', {logContext}) + }, + [clearCurrentAccount, setStateAndPersist], + ) const initSession = React.useCallback<ApiContext['initSession']>( async account => { @@ -540,11 +554,12 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ) const selectAccount = React.useCallback<ApiContext['selectAccount']>( - async account => { + async (account, logContext) => { setState(s => ({...s, isSwitchingAccounts: true})) try { await initSession(account) setState(s => ({...s, isSwitchingAccounts: false})) + logEvent('account:loggedIn', {logContext, withPassword: false}) } catch (e) { // reset this in case of error setState(s => ({...s, isSwitchingAccounts: false})) |