Skip to content

Commit

Permalink
validate too long handles in signup
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzius committed Jan 10, 2025
1 parent 02dbcc1 commit 672c7e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/lib/strings/handles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export function createFullHandle(name: string, domain: string): string {
return `${name}.${domain}`
}

export function maxServiceHandleLength(domain: string): number {
return 30 - `.${(domain || '').replace(/^[.]+/, '')}`.length
}

export function isInvalidHandle(handle: string): boolean {
return handle === 'handle.invalid'
}
Expand All @@ -38,15 +42,19 @@ export interface IsValidHandle {
}

// More checks from https://github.com/bluesky-social/atproto/blob/main/packages/pds/src/handle/index.ts#L72
export function validateHandle(str: string, userDomain: string): IsValidHandle {
export function validateHandle(
str: string,
userDomain: string,
serviceHandle?: boolean,
): IsValidHandle {
const fullHandle = createFullHandle(str, userDomain)

const results = {
handleChars:
!str || (VALIDATE_REGEX.test(fullHandle) && !str.includes('.')),
hyphenStartOrEnd: !str.startsWith('-') && !str.endsWith('-'),
frontLength: str.length >= 3,
totalLength: fullHandle.length <= 253,
totalLength: fullHandle.length <= (serviceHandle ? 30 : 253),
}

return {
Expand Down
13 changes: 10 additions & 3 deletions src/screens/Signup/StepHandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {logEvent} from '#/lib/statsig/statsig'
import {createFullHandle, validateHandle} from '#/lib/strings/handles'
import {
createFullHandle,
maxServiceHandleLength,
validateHandle,
} from '#/lib/strings/handles'
import {useAgent} from '#/state/session'
import {ScreenTransition} from '#/screens/Login/ScreenTransition'
import {useSignupContext} from '#/screens/Signup/state'
Expand Down Expand Up @@ -93,7 +97,7 @@ export function StepHandle() {
})
}, [dispatch, state.activeStep])

const validCheck = validateHandle(draftValue, state.userDomain)
const validCheck = validateHandle(draftValue, state.userDomain, true)
return (
<ScreenTransition>
<View style={[a.gap_lg]}>
Expand Down Expand Up @@ -166,7 +170,10 @@ export function StepHandle() {
/>
{!validCheck.totalLength ? (
<Text style={[a.text_md, a.flex_1]}>
<Trans>No longer than 253 characters</Trans>
<Trans>
No longer than {maxServiceHandleLength(state.userDomain)}{' '}
characters
</Trans>
</Text>
) : (
<Text style={[a.text_md, a.flex_1]}>
Expand Down

0 comments on commit 672c7e6

Please sign in to comment.