@@ -20,6 +20,7 @@ import Database from '../../../../db'
20
20
import { didDocForSession } from './util'
21
21
import { getPdsEndpoint } from '../../../../pds-agents'
22
22
import { isThisPds } from '../../../proxy'
23
+ import { dbLogger as log } from '../../../../logger'
23
24
24
25
export default function ( server : Server , ctx : AppContext ) {
25
26
server . com . atproto . server . createAccount ( {
@@ -96,19 +97,6 @@ export default function (server: Server, ctx: AppContext) {
96
97
throw err
97
98
}
98
99
99
- // Generate a real did with PLC
100
- if ( plcOp && ! entrywayAssignedPds ) {
101
- try {
102
- await ctx . plcClient . sendOperation ( did , plcOp )
103
- } catch ( err ) {
104
- req . log . error (
105
- { didKey : ctx . plcRotationKey . did ( ) , handle } ,
106
- 'failed to create did:plc' ,
107
- )
108
- throw err
109
- }
110
- }
111
-
112
100
// insert invite code use
113
101
if ( ctx . cfg . invites . required && inviteCode ) {
114
102
await ensureCodeIsAvailable ( dbTxn , inviteCode , true )
@@ -132,6 +120,10 @@ export default function (server: Server, ctx: AppContext) {
132
120
. execute ( )
133
121
}
134
122
123
+ if ( ! entrywayAssignedPds ) {
124
+ await repoTxn . createRepo ( did , [ ] , now )
125
+ }
126
+
135
127
const { access, refresh } = await ctx . services
136
128
. auth ( dbTxn )
137
129
. createSession ( {
@@ -140,6 +132,15 @@ export default function (server: Server, ctx: AppContext) {
140
132
appPasswordName : null ,
141
133
} )
142
134
135
+ return {
136
+ did,
137
+ pdsDid : entrywayAssignedPds ?. did ?? null ,
138
+ accessJwt : access ,
139
+ refreshJwt : refresh ,
140
+ }
141
+ } )
142
+
143
+ try {
143
144
if ( entrywayAssignedPds ) {
144
145
const agent = ctx . pdsAgents . get ( entrywayAssignedPds . host )
145
146
await agent . com . atproto . server . createAccount ( {
@@ -149,17 +150,21 @@ export default function (server: Server, ctx: AppContext) {
149
150
recoveryKey : input . body . recoveryKey ,
150
151
} )
151
152
} else {
152
- // Setup repo root
153
- await repoTxn . createRepo ( did , [ ] , now )
154
- }
155
-
156
- return {
157
- did,
158
- pdsDid : entrywayAssignedPds ?. did ?? null ,
159
- accessJwt : access ,
160
- refreshJwt : refresh ,
153
+ assert ( plcOp )
154
+ try {
155
+ await ctx . plcClient . sendOperation ( did , plcOp )
156
+ } catch ( err ) {
157
+ req . log . error (
158
+ { didKey : ctx . plcRotationKey . did ( ) , handle } ,
159
+ 'failed to create did:plc' ,
160
+ )
161
+ throw err
162
+ }
161
163
}
162
- } )
164
+ } catch ( err ) {
165
+ await cleanupUncreatedAccount ( ctx , did )
166
+ throw err
167
+ }
163
168
164
169
const didDoc = await didDocForSession ( ctx , result )
165
170
@@ -496,3 +501,21 @@ const randomIndexByWeight = (weights) => {
496
501
const rand = Math . random ( ) * sum
497
502
return cumulative . findIndex ( ( item ) => item >= rand )
498
503
}
504
+
505
+ const cleanupUncreatedAccount = async (
506
+ ctx : AppContext ,
507
+ did : string ,
508
+ tries = 0 ,
509
+ ) => {
510
+ if ( tries > 3 ) return
511
+ try {
512
+ await Promise . all ( [
513
+ ctx . services . account ( ctx . db ) . deleteAccount ( did ) ,
514
+ ctx . services . record ( ctx . db ) . deleteForActor ( did ) ,
515
+ ctx . services . repo ( ctx . db ) . deleteRepo ( did ) ,
516
+ ] )
517
+ } catch ( err ) {
518
+ log . error ( { err, did, tries } , 'failed to clean up partially created user' )
519
+ return cleanupUncreatedAccount ( ctx , did , tries + 1 )
520
+ }
521
+ }
0 commit comments