@atproto/oauth-provider-ui 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/.linguirc +57 -0
  2. package/CHANGELOG.md +7 -0
  3. package/CONTRIBUTING.md +6 -0
  4. package/LICENSE.txt +7 -0
  5. package/dist/assets/COdVzed-.css +3 -0
  6. package/dist/assets/COdVzed-.js +100 -0
  7. package/dist/assets/COdVzed-.js.map +1 -0
  8. package/dist/assets/Cqnfnbvc.js +6 -0
  9. package/dist/assets/Cqnfnbvc.js.map +1 -0
  10. package/dist/assets/bundle-manifest.json +630 -0
  11. package/dist/assets/error-view-Bu4y7Nd8.js +208 -0
  12. package/dist/assets/error-view-Bu4y7Nd8.js.map +1 -0
  13. package/dist/assets/index-DXlCRM6V.js +36 -0
  14. package/dist/assets/index-DXlCRM6V.js.map +1 -0
  15. package/dist/assets/messages-2GoTm2qL.js +4 -0
  16. package/dist/assets/messages-2GoTm2qL.js.map +1 -0
  17. package/dist/assets/messages-6Cn2Jbhw.js +4 -0
  18. package/dist/assets/messages-6Cn2Jbhw.js.map +1 -0
  19. package/dist/assets/messages-75hFgOK2.js +4 -0
  20. package/dist/assets/messages-75hFgOK2.js.map +1 -0
  21. package/dist/assets/messages-B3OK4k0O.js +4 -0
  22. package/dist/assets/messages-B3OK4k0O.js.map +1 -0
  23. package/dist/assets/messages-BNXlPzKV.js +4 -0
  24. package/dist/assets/messages-BNXlPzKV.js.map +1 -0
  25. package/dist/assets/messages-BUygB8mD.js +4 -0
  26. package/dist/assets/messages-BUygB8mD.js.map +1 -0
  27. package/dist/assets/messages-BVPPcwNr.js +4 -0
  28. package/dist/assets/messages-BVPPcwNr.js.map +1 -0
  29. package/dist/assets/messages-BbbWUQS8.js +4 -0
  30. package/dist/assets/messages-BbbWUQS8.js.map +1 -0
  31. package/dist/assets/messages-BibKCYyW.js +4 -0
  32. package/dist/assets/messages-BibKCYyW.js.map +1 -0
  33. package/dist/assets/messages-BlPrr9_7.js +4 -0
  34. package/dist/assets/messages-BlPrr9_7.js.map +1 -0
  35. package/dist/assets/messages-ByVCw40U.js +4 -0
  36. package/dist/assets/messages-ByVCw40U.js.map +1 -0
  37. package/dist/assets/messages-C5DU1neP.js +4 -0
  38. package/dist/assets/messages-C5DU1neP.js.map +1 -0
  39. package/dist/assets/messages-C6IgUtbX.js +4 -0
  40. package/dist/assets/messages-C6IgUtbX.js.map +1 -0
  41. package/dist/assets/messages-C92Zzt2o.js +4 -0
  42. package/dist/assets/messages-C92Zzt2o.js.map +1 -0
  43. package/dist/assets/messages-CGZqYT14.js +4 -0
  44. package/dist/assets/messages-CGZqYT14.js.map +1 -0
  45. package/dist/assets/messages-CGlsy4wt.js +4 -0
  46. package/dist/assets/messages-CGlsy4wt.js.map +1 -0
  47. package/dist/assets/messages-CPT1nd0u.js +4 -0
  48. package/dist/assets/messages-CPT1nd0u.js.map +1 -0
  49. package/dist/assets/messages-CTTdXyw_.js +4 -0
  50. package/dist/assets/messages-CTTdXyw_.js.map +1 -0
  51. package/dist/assets/messages-ChK_C_Pj.js +4 -0
  52. package/dist/assets/messages-ChK_C_Pj.js.map +1 -0
  53. package/dist/assets/messages-CjJbk7Uf.js +4 -0
  54. package/dist/assets/messages-CjJbk7Uf.js.map +1 -0
  55. package/dist/assets/messages-CoiLjLYO.js +4 -0
  56. package/dist/assets/messages-CoiLjLYO.js.map +1 -0
  57. package/dist/assets/messages-Cwx6B4Ti.js +4 -0
  58. package/dist/assets/messages-Cwx6B4Ti.js.map +1 -0
  59. package/dist/assets/messages-D0uXAp_H.js +4 -0
  60. package/dist/assets/messages-D0uXAp_H.js.map +1 -0
  61. package/dist/assets/messages-DG0_arU0.js +4 -0
  62. package/dist/assets/messages-DG0_arU0.js.map +1 -0
  63. package/dist/assets/messages-DOXFJh9K.js +4 -0
  64. package/dist/assets/messages-DOXFJh9K.js.map +1 -0
  65. package/dist/assets/messages-DPK7nOoC.js +4 -0
  66. package/dist/assets/messages-DPK7nOoC.js.map +1 -0
  67. package/dist/assets/messages-Duccgtu0.js +4 -0
  68. package/dist/assets/messages-Duccgtu0.js.map +1 -0
  69. package/dist/assets/messages-DxTqgsHq.js +4 -0
  70. package/dist/assets/messages-DxTqgsHq.js.map +1 -0
  71. package/dist/assets/messages-E5_lTg7A.js +4 -0
  72. package/dist/assets/messages-E5_lTg7A.js.map +1 -0
  73. package/dist/assets/messages-UhunAjh1.js +4 -0
  74. package/dist/assets/messages-UhunAjh1.js.map +1 -0
  75. package/dist/assets/messages-Xg_3YLGw.js +4 -0
  76. package/dist/assets/messages-Xg_3YLGw.js.map +1 -0
  77. package/dist/assets/messages-iliBQHY2.js +4 -0
  78. package/dist/assets/messages-iliBQHY2.js.map +1 -0
  79. package/dist/assets/messages-lRprpIl-.js +4 -0
  80. package/dist/assets/messages-lRprpIl-.js.map +1 -0
  81. package/dist/assets/messages-pbPHQbz1.js +4 -0
  82. package/dist/assets/messages-pbPHQbz1.js.map +1 -0
  83. package/dist/assets/messages-q-O7ZQGs.js +4 -0
  84. package/dist/assets/messages-q-O7ZQGs.js.map +1 -0
  85. package/dist/lib/index.d.ts +19 -0
  86. package/dist/lib/index.d.ts.map +1 -0
  87. package/dist/lib/index.js +47 -0
  88. package/dist/lib/index.js.map +1 -0
  89. package/dist/tsconfig.backend.tsbuildinfo +1 -0
  90. package/lib/index.ts +72 -0
  91. package/package.json +73 -0
  92. package/rollup.config.js +102 -0
  93. package/src/authorization-page.html +183 -0
  94. package/src/authorization-page.tsx +55 -0
  95. package/src/backend-data.ts +35 -0
  96. package/src/components/forms/button-toggle-visibility.tsx +43 -0
  97. package/src/components/forms/button.tsx +60 -0
  98. package/src/components/forms/fieldset.tsx +55 -0
  99. package/src/components/forms/form-card-async.tsx +103 -0
  100. package/src/components/forms/form-card.tsx +49 -0
  101. package/src/components/forms/input-checkbox.tsx +78 -0
  102. package/src/components/forms/input-container.tsx +107 -0
  103. package/src/components/forms/input-email-address.tsx +65 -0
  104. package/src/components/forms/input-new-password.tsx +62 -0
  105. package/src/components/forms/input-password.tsx +87 -0
  106. package/src/components/forms/input-text.tsx +82 -0
  107. package/src/components/forms/input-token.tsx +94 -0
  108. package/src/components/forms/wizard-card.tsx +116 -0
  109. package/src/components/layouts/layout-title-page.tsx +77 -0
  110. package/src/components/layouts/layout-welcome.tsx +73 -0
  111. package/src/components/utils/account-identifier.tsx +23 -0
  112. package/src/components/utils/account-image.tsx +33 -0
  113. package/src/components/utils/admonition.tsx +52 -0
  114. package/src/components/utils/client-name.tsx +45 -0
  115. package/src/components/utils/error-card.tsx +93 -0
  116. package/src/components/utils/error-message.tsx +88 -0
  117. package/src/components/utils/help-card.tsx +46 -0
  118. package/src/components/utils/icons.tsx +88 -0
  119. package/src/components/utils/link-anchor.tsx +28 -0
  120. package/src/components/utils/link-title.tsx +26 -0
  121. package/src/components/utils/multi-lang-string.tsx +56 -0
  122. package/src/components/utils/password-strength-label.tsx +37 -0
  123. package/src/components/utils/password-strength-meter.tsx +58 -0
  124. package/src/components/utils/url-viewer.tsx +73 -0
  125. package/src/cookies.ts +11 -0
  126. package/src/error-page.html +125 -0
  127. package/src/error-page.tsx +29 -0
  128. package/src/hooks/use-api.ts +182 -0
  129. package/src/hooks/use-async-action.ts +120 -0
  130. package/src/hooks/use-bound-dispatch.ts +5 -0
  131. package/src/hooks/use-browser-color-scheme.ts +31 -0
  132. package/src/hooks/use-csrf-token.ts +5 -0
  133. package/src/hooks/use-random-string.ts +37 -0
  134. package/src/hooks/use-stepper.ts +87 -0
  135. package/src/index.html +13 -0
  136. package/src/lib/api.ts +234 -0
  137. package/src/lib/backend-data.ts +6 -0
  138. package/src/lib/clsx.ts +6 -0
  139. package/src/lib/json-client.ts +97 -0
  140. package/src/lib/password.ts +98 -0
  141. package/src/lib/ref.ts +17 -0
  142. package/src/lib/util.ts +13 -0
  143. package/src/locales/an/messages.po +487 -0
  144. package/src/locales/ast/messages.po +487 -0
  145. package/src/locales/ca/messages.po +487 -0
  146. package/src/locales/da/messages.po +487 -0
  147. package/src/locales/de/messages.po +487 -0
  148. package/src/locales/el/messages.po +487 -0
  149. package/src/locales/en/messages.po +487 -0
  150. package/src/locales/en-GB/messages.po +487 -0
  151. package/src/locales/es/messages.po +487 -0
  152. package/src/locales/eu/messages.po +487 -0
  153. package/src/locales/fi/messages.po +487 -0
  154. package/src/locales/fr/messages.po +487 -0
  155. package/src/locales/ga/messages.po +487 -0
  156. package/src/locales/gl/messages.po +487 -0
  157. package/src/locales/hi/messages.po +487 -0
  158. package/src/locales/hu/messages.po +487 -0
  159. package/src/locales/ia/messages.po +487 -0
  160. package/src/locales/id/messages.po +487 -0
  161. package/src/locales/it/messages.po +487 -0
  162. package/src/locales/ja/messages.po +487 -0
  163. package/src/locales/km/messages.po +487 -0
  164. package/src/locales/ko/messages.po +487 -0
  165. package/src/locales/load.ts +8 -0
  166. package/src/locales/locale-context.ts +19 -0
  167. package/src/locales/locale-provider.tsx +112 -0
  168. package/src/locales/locale-selector.tsx +58 -0
  169. package/src/locales/locales.ts +168 -0
  170. package/src/locales/ne/messages.po +487 -0
  171. package/src/locales/nl/messages.po +487 -0
  172. package/src/locales/pl/messages.po +487 -0
  173. package/src/locales/pt-BR/messages.po +487 -0
  174. package/src/locales/ro/messages.po +487 -0
  175. package/src/locales/ru/messages.po +487 -0
  176. package/src/locales/sv/messages.po +487 -0
  177. package/src/locales/th/messages.po +487 -0
  178. package/src/locales/tr/messages.po +487 -0
  179. package/src/locales/uk/messages.po +487 -0
  180. package/src/locales/vi/messages.po +487 -0
  181. package/src/locales/zh-CN/messages.po +487 -0
  182. package/src/locales/zh-HK/messages.po +487 -0
  183. package/src/locales/zh-TW/messages.po +487 -0
  184. package/src/styles.css +33 -0
  185. package/src/views/authorize/accept/accept-form.tsx +150 -0
  186. package/src/views/authorize/accept/accept-view.tsx +70 -0
  187. package/src/views/authorize/authorize-view.tsx +183 -0
  188. package/src/views/authorize/reset-password/reset-password-confirm-form.tsx +88 -0
  189. package/src/views/authorize/reset-password/reset-password-request-form.tsx +80 -0
  190. package/src/views/authorize/reset-password/reset-password-view.tsx +127 -0
  191. package/src/views/authorize/sign-in/sign-in-form.tsx +242 -0
  192. package/src/views/authorize/sign-in/sign-in-picker.tsx +116 -0
  193. package/src/views/authorize/sign-in/sign-in-view.tsx +145 -0
  194. package/src/views/authorize/sign-up/sign-up-account-form.tsx +142 -0
  195. package/src/views/authorize/sign-up/sign-up-disclaimer.tsx +51 -0
  196. package/src/views/authorize/sign-up/sign-up-handle-form.tsx +287 -0
  197. package/src/views/authorize/sign-up/sign-up-hcaptcha-form.tsx +108 -0
  198. package/src/views/authorize/sign-up/sign-up-view.tsx +158 -0
  199. package/src/views/authorize/welcome/welcome-view.tsx +56 -0
  200. package/src/views/error/error-view.tsx +31 -0
  201. package/tailwind.config.js +31 -0
  202. package/tsconfig.backend.json +8 -0
  203. package/tsconfig.frontend.json +10 -0
  204. package/tsconfig.frontend.tsbuildinfo +1 -0
  205. package/tsconfig.json +8 -0
  206. package/tsconfig.tools.json +8 -0
  207. package/tsconfig.tools.tsbuildinfo +1 -0
  208. package/vite.config.mjs +16 -0
package/src/lib/api.ts ADDED
@@ -0,0 +1,234 @@
1
+ import type { ApiEndpoints } from '@atproto/oauth-provider-api'
2
+ import {
3
+ JsonClient,
4
+ JsonErrorPayload,
5
+ JsonErrorResponse,
6
+ } from './json-client.ts'
7
+
8
+ export type { Options } from './json-client.ts'
9
+
10
+ export type AcceptData = {
11
+ sub: string
12
+ }
13
+
14
+ export class Api extends JsonClient<ApiEndpoints> {
15
+ constructor(csrfToken: string) {
16
+ const baseUrl = new URL('/oauth/authorize', window.origin).toString()
17
+ super(baseUrl, csrfToken)
18
+ }
19
+
20
+ public buildAcceptUrl({ sub }: AcceptData): URL {
21
+ const url = new URL(`${this.baseUrl}/accept`)
22
+ url.searchParams.set('account_sub', sub)
23
+ url.searchParams.set('csrf_token', this.csrfToken)
24
+ return url
25
+ }
26
+
27
+ public buildRejectUrl(): URL {
28
+ const url = new URL(`${this.baseUrl}/reject`)
29
+ url.searchParams.set('csrf_token', this.csrfToken)
30
+ return url
31
+ }
32
+
33
+ // Override the parent's parseError method to handle expected error responses
34
+ // and transform them into instances of the corresponding error classes.
35
+ public static override parseError(
36
+ json: unknown,
37
+ ): undefined | JsonErrorResponse {
38
+ // @NOTE Most specific errors first !
39
+ if (SecondAuthenticationFactorRequiredError.is(json)) {
40
+ return new SecondAuthenticationFactorRequiredError(json)
41
+ }
42
+ if (InvalidCredentialsError.is(json)) {
43
+ return new InvalidCredentialsError(json)
44
+ }
45
+ if (InvalidInviteCodeError.is(json)) {
46
+ return new InvalidInviteCodeError(json)
47
+ }
48
+ if (HandleUnavailableError.is(json)) {
49
+ return new HandleUnavailableError(json)
50
+ }
51
+ if (EmailTakenError.is(json)) {
52
+ return new EmailTakenError(json)
53
+ }
54
+ if (RequestExpiredError.is(json)) {
55
+ return new RequestExpiredError(json)
56
+ }
57
+ if (UnknownRequestUriError.is(json)) {
58
+ return new UnknownRequestUriError(json)
59
+ }
60
+ if (InvalidRequestError.is(json)) {
61
+ return new InvalidRequestError(json)
62
+ }
63
+ if (AccessDeniedError.is(json)) {
64
+ return new AccessDeniedError(json)
65
+ }
66
+ return super.parseError(json)
67
+ }
68
+ }
69
+
70
+ export type AccessDeniedPayload = JsonErrorPayload<'access_denied'>
71
+ export class AccessDeniedError<
72
+ P extends AccessDeniedPayload = AccessDeniedPayload,
73
+ > extends JsonErrorResponse<P> {
74
+ constructor(
75
+ payload: P,
76
+ message = payload.error_description || 'Access denied',
77
+ ) {
78
+ super(payload, message)
79
+ }
80
+
81
+ static is(json: unknown): json is AccessDeniedPayload {
82
+ return super.is(json) && json.error === 'access_denied'
83
+ }
84
+ }
85
+
86
+ export type InvalidRequestPayload = JsonErrorPayload<'invalid_request'>
87
+ export class InvalidRequestError<
88
+ P extends InvalidRequestPayload = InvalidRequestPayload,
89
+ > extends JsonErrorResponse<P> {
90
+ constructor(
91
+ payload: P,
92
+ message = payload.error_description || 'Invalid request',
93
+ ) {
94
+ super(payload, message)
95
+ }
96
+
97
+ static is(json: unknown): json is InvalidRequestPayload {
98
+ return super.is(json) && json.error === 'invalid_request'
99
+ }
100
+ }
101
+
102
+ export type InvalidInviteCodePayload = InvalidRequestPayload & {
103
+ error_description: `This invite code is invalid.${string}`
104
+ }
105
+ export class InvalidInviteCodeError<
106
+ P extends InvalidInviteCodePayload = InvalidInviteCodePayload,
107
+ > extends InvalidRequestError<P> {
108
+ constructor(payload: P) {
109
+ super(payload)
110
+ }
111
+
112
+ static is(json: unknown): json is InvalidInviteCodePayload {
113
+ return (
114
+ super.is(json) &&
115
+ json.error_description != null &&
116
+ json.error_description.startsWith('This invite code is invalid.')
117
+ )
118
+ }
119
+ }
120
+
121
+ export type RequestExpiredPayload = AccessDeniedPayload & {
122
+ error_description: 'This request has expired'
123
+ }
124
+ export class RequestExpiredError<
125
+ P extends RequestExpiredPayload = RequestExpiredPayload,
126
+ > extends AccessDeniedError<P> {
127
+ static is(json: unknown): json is RequestExpiredPayload {
128
+ return (
129
+ super.is(json) && json.error_description === 'This request has expired'
130
+ )
131
+ }
132
+ }
133
+
134
+ export type InvalidCredentialsPayload = InvalidRequestPayload & {
135
+ error_description: 'Invalid identifier or password'
136
+ }
137
+ export class InvalidCredentialsError<
138
+ P extends InvalidCredentialsPayload = InvalidCredentialsPayload,
139
+ > extends InvalidRequestError<P> {
140
+ static is(json: unknown): json is InvalidCredentialsPayload {
141
+ return (
142
+ super.is(json) &&
143
+ json.error_description === 'Invalid identifier or password'
144
+ )
145
+ }
146
+ }
147
+
148
+ export type UnknownRequestPayload = InvalidRequestPayload & {
149
+ error_description: 'Unknown request_uri'
150
+ }
151
+ export class UnknownRequestUriError<
152
+ P extends UnknownRequestPayload = UnknownRequestPayload,
153
+ > extends InvalidRequestError<P> {
154
+ static is(json: unknown): json is UnknownRequestPayload {
155
+ return super.is(json) && json.error_description === 'Unknown request_uri'
156
+ }
157
+ }
158
+ export type EmailTakenPayload = InvalidRequestPayload & {
159
+ error_description: 'Email already taken'
160
+ }
161
+ export class EmailTakenError<
162
+ P extends EmailTakenPayload = EmailTakenPayload,
163
+ > extends InvalidRequestError<P> {
164
+ static is(json: unknown): json is EmailTakenPayload {
165
+ return super.is(json) && json.error_description === 'Email already taken'
166
+ }
167
+ }
168
+
169
+ export type HandleUnavailablePayload =
170
+ JsonErrorPayload<'handle_unavailable'> & {
171
+ reason: 'syntax' | 'domain' | 'slur' | 'taken'
172
+ }
173
+ export class HandleUnavailableError<
174
+ P extends HandleUnavailablePayload = HandleUnavailablePayload,
175
+ > extends JsonErrorResponse<P> {
176
+ constructor(
177
+ payload: P,
178
+ message = payload.error_description || 'That handle cannot be used',
179
+ ) {
180
+ super(payload, message)
181
+ }
182
+
183
+ get reason() {
184
+ return this.payload.reason
185
+ }
186
+
187
+ static is(json: unknown): json is HandleUnavailablePayload {
188
+ return (
189
+ super.is(json) &&
190
+ json.error === 'handle_unavailable' &&
191
+ 'reason' in json &&
192
+ (json.reason === 'syntax' ||
193
+ json.reason === 'domain' ||
194
+ json.reason === 'slur' ||
195
+ json.reason === 'taken')
196
+ )
197
+ }
198
+ }
199
+
200
+ export type SecondAuthenticationFactorRequiredPayload =
201
+ JsonErrorPayload<'second_authentication_factor_required'> & {
202
+ type: 'emailOtp'
203
+ hint: string
204
+ }
205
+ export class SecondAuthenticationFactorRequiredError<
206
+ P extends
207
+ SecondAuthenticationFactorRequiredPayload = SecondAuthenticationFactorRequiredPayload,
208
+ > extends JsonErrorResponse<P> {
209
+ constructor(
210
+ payload: P,
211
+ message = payload.error_description ||
212
+ `${payload.type} authentication factor required (hint: ${payload.hint})`,
213
+ ) {
214
+ super(payload, message)
215
+ }
216
+
217
+ get type() {
218
+ return this.payload.type
219
+ }
220
+ get hint() {
221
+ return this.payload.hint
222
+ }
223
+
224
+ static is(json: unknown): json is SecondAuthenticationFactorRequiredPayload {
225
+ return (
226
+ super.is(json) &&
227
+ json.error === 'second_authentication_factor_required' &&
228
+ 'type' in json &&
229
+ json.type === 'emailOtp' &&
230
+ 'hint' in json &&
231
+ typeof json.hint === 'string'
232
+ )
233
+ }
234
+ }
@@ -0,0 +1,6 @@
1
+ export function readBackendData<T>(key: string): T {
2
+ const value = window[key] as T | undefined
3
+ delete window[key] // Prevent accidental usage / potential leaks to dependencies
4
+ if (value !== undefined) return value
5
+ throw new TypeError(`Backend data "${key}" is missing`)
6
+ }
@@ -0,0 +1,6 @@
1
+ type ClsxArg = string | false | undefined
2
+
3
+ export function clsx(...args: [ClsxArg, ...ClsxArg[]]): string | undefined {
4
+ const filtered = args.filter(Boolean) as string[]
5
+ return filtered.length > 0 ? filtered.join(' ') : undefined
6
+ }
@@ -0,0 +1,97 @@
1
+ // Using a type import to avoid bundling this lib
2
+ import type { Json } from '@atproto-labs/fetch'
3
+
4
+ export { type Json }
5
+
6
+ export type Options = {
7
+ signal?: AbortSignal
8
+ }
9
+
10
+ export type EndpointPath = `/${string}`
11
+ export type EndpointDefinition = {
12
+ input: Json
13
+ output: Json | void
14
+ }
15
+
16
+ export class JsonClient<
17
+ Endpoints extends { [Path: EndpointPath]: EndpointDefinition },
18
+ > {
19
+ constructor(
20
+ protected readonly baseUrl: string,
21
+ protected readonly csrfToken: string,
22
+ ) {}
23
+
24
+ public async fetch<Path extends EndpointPath & keyof Endpoints>(
25
+ path: Path,
26
+ payload: Endpoints[Path]['input'],
27
+ options?: Options,
28
+ ): Promise<Endpoints[Path]['output']> {
29
+ const response = await fetch(`${this.baseUrl}${path}`, {
30
+ method: 'POST',
31
+ headers: {
32
+ 'Content-Type': 'application/json',
33
+ 'X-CSRF-Token': this.csrfToken,
34
+ },
35
+ mode: 'same-origin',
36
+ body: JSON.stringify(payload),
37
+ signal: options?.signal,
38
+ })
39
+
40
+ if (response.status === 204) {
41
+ return undefined
42
+ }
43
+
44
+ return response.json().then((json: Json) => {
45
+ if (response.ok) return json as Endpoints[Path]['output']
46
+ else throw this.parseError(response, json)
47
+ })
48
+ }
49
+
50
+ protected parseError(response: Response, json: Json): Error {
51
+ const Class = this.constructor as typeof JsonClient
52
+ const error = Class.parseError(json)
53
+ if (error) return error
54
+
55
+ return new Error('Invalid JSON response', { cause: response })
56
+ }
57
+
58
+ public static parseError(json: unknown): undefined | JsonErrorResponse {
59
+ if (JsonErrorResponse.is(json)) {
60
+ return new JsonErrorResponse(json)
61
+ }
62
+ }
63
+ }
64
+
65
+ export type JsonErrorPayload<E extends string = string> = {
66
+ error: E
67
+ error_description?: string
68
+ }
69
+
70
+ export class JsonErrorResponse<
71
+ P extends JsonErrorPayload = JsonErrorPayload,
72
+ > extends Error {
73
+ constructor(
74
+ public readonly payload: P,
75
+ message = payload.error_description,
76
+ ) {
77
+ super(message || `Error "${payload.error}"`)
78
+ }
79
+
80
+ get error(): string {
81
+ return this.payload.error
82
+ }
83
+
84
+ get description(): string | undefined {
85
+ return this.payload.error_description
86
+ }
87
+
88
+ static is(json: unknown): json is JsonErrorPayload {
89
+ return (
90
+ json != null &&
91
+ typeof json === 'object' &&
92
+ typeof json['error'] === 'string' &&
93
+ (json['error_description'] === undefined ||
94
+ typeof json['error_description'] === 'string')
95
+ )
96
+ }
97
+ }
@@ -0,0 +1,98 @@
1
+ export const MIN_PASSWORD_LENGTH = 8
2
+
3
+ const EMOJI =
4
+ /(\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/
5
+ const UPPER = /[A-Z]/
6
+ const LOWER = /[a-z]/
7
+ const DEC = /[0-9]/
8
+ const SPECIAL = /[^a-zA-Z0-9]/
9
+
10
+ export enum PasswordStrength {
11
+ weak = 1,
12
+ moderate = 2,
13
+ strong = 3,
14
+ extra = 4,
15
+ }
16
+
17
+ export function getPasswordStrength(pwd: string): PasswordStrength {
18
+ if (pwd.length < MIN_PASSWORD_LENGTH) {
19
+ return PasswordStrength.weak
20
+ }
21
+
22
+ // Very long passwords
23
+ if (pwd.length >= MIN_PASSWORD_LENGTH + 12) {
24
+ return PasswordStrength.extra
25
+ }
26
+
27
+ // Long passwords
28
+ if (pwd.length >= MIN_PASSWORD_LENGTH + 8) {
29
+ if (matches(pwd, [SPECIAL])) {
30
+ return PasswordStrength.extra
31
+ }
32
+ if (matches(pwd, [UPPER, LOWER, DEC], 2)) {
33
+ return PasswordStrength.extra
34
+ }
35
+ return PasswordStrength.strong
36
+ }
37
+
38
+ // Emojis make passwords strong
39
+ if (pwd.length >= MIN_PASSWORD_LENGTH) {
40
+ if (matches(pwd, [EMOJI])) {
41
+ return PasswordStrength.strong
42
+ }
43
+ }
44
+
45
+ // Pretty long passwords
46
+ if (pwd.length >= MIN_PASSWORD_LENGTH + 6) {
47
+ if (matches(pwd, [SPECIAL])) {
48
+ return PasswordStrength.strong
49
+ }
50
+ if (matches(pwd, [UPPER, LOWER, DEC], 2)) {
51
+ return PasswordStrength.strong
52
+ }
53
+ // Only 1 type of alpha-num characters
54
+ return PasswordStrength.moderate
55
+ }
56
+
57
+ // Longish password
58
+ if (pwd.length >= MIN_PASSWORD_LENGTH + 4) {
59
+ if (matches(pwd, [SPECIAL])) {
60
+ return PasswordStrength.moderate
61
+ }
62
+ if (matches(pwd, [UPPER, LOWER, DEC], 2)) {
63
+ return PasswordStrength.moderate
64
+ }
65
+
66
+ // Only 1 type of alpha-num characters
67
+ return PasswordStrength.weak
68
+ }
69
+
70
+ // Short password (8-11 characters)
71
+ if (pwd.length >= MIN_PASSWORD_LENGTH) {
72
+ if (matches(pwd, [SPECIAL])) {
73
+ return PasswordStrength.moderate
74
+ }
75
+ if (matches(pwd, [UPPER, LOWER, DEC])) {
76
+ return PasswordStrength.moderate
77
+ }
78
+ }
79
+
80
+ return PasswordStrength.weak
81
+ }
82
+
83
+ function matches(
84
+ pwd: string,
85
+ regexps: RegExp[],
86
+ regexpsCountToMatch: number = regexps.length,
87
+ ): boolean {
88
+ if (regexpsCountToMatch < 1 || regexpsCountToMatch > regexps.length) {
89
+ throw new TypeError('Invalid regexpsCountToMatch')
90
+ }
91
+ for (const regexp of regexps) {
92
+ if (regexp.test(pwd)) {
93
+ regexpsCountToMatch--
94
+ if (regexpsCountToMatch === 0) return true
95
+ }
96
+ }
97
+ return false
98
+ }
package/src/lib/ref.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { ForwardedRef } from 'react'
2
+
3
+ export function updateRef<T>(ref: ForwardedRef<T>, value: T | null) {
4
+ if (typeof ref === 'function') {
5
+ ref(value)
6
+ } else if (ref) {
7
+ ref.current = value
8
+ }
9
+ }
10
+
11
+ export function mergeRefs<T>(refs: readonly (ForwardedRef<T> | undefined)[]) {
12
+ return (value: T | null) => {
13
+ for (const ref of refs) {
14
+ if (ref) updateRef(ref, value)
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,13 @@
1
+ export function upsert<T>(
2
+ arr: readonly T[],
3
+ item: T,
4
+ predicate: (value: T, index: number, obj: readonly T[]) => boolean,
5
+ ): T[] {
6
+ const idx = arr.findIndex(predicate)
7
+ return idx === -1
8
+ ? [...arr, item]
9
+ : [...arr.slice(0, idx), item, ...arr.slice(idx + 1)]
10
+ }
11
+
12
+ export type Simplify<T> = { [K in keyof T]: T[K] } & NonNullable<unknown>
13
+ export type Override<T, U> = Simplify<Omit<T, keyof U> & U>