about summary refs log tree commit diff
path: root/src/state/models/ui/create-account.ts
blob: 78ffe88583e029f2f65083034f9134f70e4ba2e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {makeAutoObservable} from 'mobx'
import {RootStoreModel} from '../root-store'
import {ServiceDescription} from '../session'
import {DEFAULT_SERVICE} from 'state/index'
import {ComAtprotoServerCreateAccount} from '@atproto/api'
import * as EmailValidator from 'email-validator'
import {createFullHandle} from 'lib/strings/handles'
import {cleanError} from 'lib/strings/errors'
import {getAge} from 'lib/strings/time'
import {track} from 'lib/analytics/analytics'

const DEFAULT_DATE = new Date(Date.now() - 60e3 * 60 * 24 * 365 * 20) // default to 20 years ago

export class CreateAccountModel {
  step: number = 1
  isProcessing = false
  isFetchingServiceDescription = false
  didServiceDescriptionFetchFail = false
  error = ''

  serviceUrl = DEFAULT_SERVICE
  serviceDescription: ServiceDescription | undefined = undefined
  userDomain = ''
  inviteCode = ''
  email = ''
  password = ''
  handle = ''
  birthDate = DEFAULT_DATE

  constructor(public rootStore: RootStoreModel) {
    makeAutoObservable(this, {}, {autoBind: true})
  }

  // form state controls
  // =

  next() {
    this.error = ''
    if (this.step === 2) {
      if (getAge(this.birthDate) < 13) {
        this.error =
          'Unfortunately, you do not meet the requirements to create an account.'
        return
      }
    }
    this.step++
  }

  back() {
    this.error = ''
    this.step--
  }

  setStep(v: number) {
    this.step = v
  }

  async fetchServiceDescription() {
    this.setError('')
    this.setIsFetchingServiceDescription(true)
    this.setDidServiceDescriptionFetchFail(false)
    this.setServiceDescription(undefined)
    if (!this.serviceUrl) {
      return
    }
    try {
      const desc = await this.rootStore.session.describeService(this.serviceUrl)
      this.setServiceDescription(desc)
      this.setUserDomain(desc.availableUserDomains[0])
    } catch (err: any) {
      this.rootStore.log.warn(
        `Failed to fetch service description for ${this.serviceUrl}`,
        err,
      )
      this.setError(
        'Unable to contact your service. Please check your Internet connection.',
      )
      this.setDidServiceDescriptionFetchFail(true)
    } finally {
      this.setIsFetchingServiceDescription(false)
    }
  }

  async submit() {
    if (!this.email) {
      this.setStep(2)
      return this.setError('Please enter your email.')
    }
    if (!EmailValidator.validate(this.email)) {
      this.setStep(2)
      return this.setError('Your email appears to be invalid.')
    }
    if (!this.password) {
      this.setStep(2)
      return this.setError('Please choose your password.')
    }
    if (!this.handle) {
      this.setStep(3)
      return this.setError('Please choose your handle.')
    }
    this.setError('')
    this.setIsProcessing(true)
    try {
      await this.rootStore.session.createAccount({
        service: this.serviceUrl,
        email: this.email,
        handle: createFullHandle(this.handle, this.userDomain),
        password: this.password,
        inviteCode: this.inviteCode,
      })
    } catch (e: any) {
      let errMsg = e.toString()
      if (e instanceof ComAtprotoServerCreateAccount.InvalidInviteCodeError) {
        errMsg =
          'Invite code not accepted. Check that you input it correctly and try again.'
      }
      this.rootStore.log.error('Failed to create account', e)
      this.setIsProcessing(false)
      this.setError(cleanError(errMsg))
      throw e
    } finally {
      track('Create Account')
    }
  }

  // form state accessors
  // =

  get canBack() {
    return this.step > 1
  }

  get canNext() {
    if (this.step === 1) {
      return !!this.serviceDescription
    } else if (this.step === 2) {
      return (
        (!this.isInviteCodeRequired || this.inviteCode) &&
        !!this.email &&
        !!this.password
      )
    }
    return !!this.handle
  }

  get isServiceDescribed() {
    return !!this.serviceDescription
  }

  get isInviteCodeRequired() {
    return this.serviceDescription?.inviteCodeRequired
  }

  // setters
  // =

  setIsProcessing(v: boolean) {
    this.isProcessing = v
  }

  setIsFetchingServiceDescription(v: boolean) {
    this.isFetchingServiceDescription = v
  }

  setDidServiceDescriptionFetchFail(v: boolean) {
    this.didServiceDescriptionFetchFail = v
  }

  setError(v: string) {
    this.error = v
  }

  setServiceUrl(v: string) {
    this.serviceUrl = v
  }

  setServiceDescription(v: ServiceDescription | undefined) {
    this.serviceDescription = v
  }

  setUserDomain(v: string) {
    this.userDomain = v
  }

  setInviteCode(v: string) {
    this.inviteCode = v
  }

  setEmail(v: string) {
    this.email = v
  }

  setPassword(v: string) {
    this.password = v
  }

  setHandle(v: string) {
    this.handle = v
  }

  setBirthDate(v: Date) {
    this.birthDate = v
  }
}