@@ -14,7 +14,7 @@ import { None, Option, Some } from "@undb/domain"
14
14
import { env } from "@undb/env"
15
15
import { createLogger } from "@undb/logger"
16
16
import { type IMailService , injectMailService } from "@undb/mail"
17
- import { type IQueryBuilder , getCurrentTransaction , injectQueryBuilder } from "@undb/persistence/server"
17
+ import { type IQueryBuilder , type ITxContext , injectQueryBuilder , injectTxCTX } from "@undb/persistence/server"
18
18
import { type ISpaceService , injectSpaceService } from "@undb/space"
19
19
import { Context , Elysia , t } from "elysia"
20
20
import type { Session , User } from "lucia"
@@ -25,7 +25,6 @@ import { alphabet, generateRandomString, sha256 } from "oslo/crypto"
25
25
import { encodeHex } from "oslo/encoding"
26
26
import { omit } from "radash"
27
27
import { v7 } from "uuid"
28
- import { withTransaction } from "../../db"
29
28
import { injectLucia } from "./auth.provider"
30
29
import { OAuth } from "./oauth/oauth"
31
30
@@ -52,10 +51,12 @@ export class Auth {
52
51
private readonly mailService : IMailService ,
53
52
@injectLucia ( )
54
53
private readonly lucia : Lucia ,
54
+ @injectTxCTX ( )
55
+ private readonly txContext : ITxContext ,
55
56
) { }
56
57
57
58
async #generateEmailVerificationCode( userId : string , email : string ) : Promise < string > {
58
- const tx = getCurrentTransaction ( )
59
+ const tx = this . txContext . getCurrentTransaction ( )
59
60
await tx . deleteFrom ( "undb_email_verification_code" ) . where ( "user_id" , "=" , userId ) . execute ( )
60
61
const code = env . UNDB_MOCK_MAIL_CODE || generateRandomString ( 6 , alphabet ( "0-9" ) )
61
62
await tx
@@ -71,29 +72,32 @@ export class Auth {
71
72
}
72
73
73
74
async #verifyVerificationCode( user : User , code : string ) : Promise < boolean > {
74
- return ( getCurrentTransaction ( ) ?? this . queryBuilder ) . transaction ( ) . execute ( async ( tx ) => {
75
- const databaseCode = await tx
76
- . selectFrom ( "undb_email_verification_code" )
77
- . selectAll ( )
78
- . where ( "user_id" , "=" , user . id )
79
- . executeTakeFirst ( )
80
- if ( ! databaseCode || databaseCode . code !== code ) {
81
- return false
82
- }
83
- await tx . deleteFrom ( "undb_email_verification_code" ) . where ( "id" , "=" , databaseCode . id ) . execute ( )
75
+ return this . txContext
76
+ . getCurrentTransaction ( )
77
+ . transaction ( )
78
+ . execute ( async ( tx ) => {
79
+ const databaseCode = await tx
80
+ . selectFrom ( "undb_email_verification_code" )
81
+ . selectAll ( )
82
+ . where ( "user_id" , "=" , user . id )
83
+ . executeTakeFirst ( )
84
+ if ( ! databaseCode || databaseCode . code !== code ) {
85
+ return false
86
+ }
87
+ await tx . deleteFrom ( "undb_email_verification_code" ) . where ( "id" , "=" , databaseCode . id ) . execute ( )
84
88
85
- if ( ! isWithinExpirationDate ( new Date ( databaseCode . expires_at ) ) ) {
86
- return false
87
- }
88
- if ( databaseCode . email !== user . email ) {
89
- return false
90
- }
91
- return true
92
- } )
89
+ if ( ! isWithinExpirationDate ( new Date ( databaseCode . expires_at ) ) ) {
90
+ return false
91
+ }
92
+ if ( databaseCode . email !== user . email ) {
93
+ return false
94
+ }
95
+ return true
96
+ } )
93
97
}
94
98
95
99
async #createPasswordResetToken( userId : string ) : Promise < string > {
96
- const db = getCurrentTransaction ( ) ?? this . queryBuilder
100
+ const db = this . txContext . getCurrentTransaction ( )
97
101
await db . deleteFrom ( "undb_password_reset_token" ) . where ( "user_id" , "=" , userId ) . execute ( )
98
102
const tokenId = generateIdFromEntropySize ( 25 ) // 40 character
99
103
const tokenHash = encodeHex ( await sha256 ( new TextEncoder ( ) . encode ( tokenId ) ) )
@@ -206,8 +210,9 @@ export class Auth {
206
210
} ,
207
211
} )
208
212
209
- await withTransaction ( this . queryBuilder ) ( async ( ) => {
210
- await getCurrentTransaction ( )
213
+ await this . txContext . withTransaction ( async ( ) => {
214
+ await this . txContext
215
+ . getCurrentTransaction ( )
211
216
. insertInto ( "undb_user" )
212
217
. values ( {
213
218
email : adminEmail ,
@@ -326,8 +331,9 @@ export class Auth {
326
331
} ,
327
332
} )
328
333
329
- await withTransaction ( this . queryBuilder ) ( async ( ) => {
330
- await getCurrentTransaction ( )
334
+ await this . txContext . withTransaction ( async ( ) => {
335
+ await this . txContext
336
+ . getCurrentTransaction ( )
331
337
. insertInto ( "undb_user" )
332
338
. values ( {
333
339
email,
@@ -470,9 +476,9 @@ export class Auth {
470
476
. post (
471
477
"/api/reset-password" ,
472
478
async ( ctx ) => {
473
- return withTransaction ( this . queryBuilder ) ( async ( ) => {
479
+ return this . txContext . withTransaction ( async ( ) => {
474
480
const email = ctx . body . email
475
- const tx = getCurrentTransaction ( ) ?? this . queryBuilder
481
+ const tx = this . txContext . getCurrentTransaction ( )
476
482
const user = await tx . selectFrom ( "undb_user" ) . selectAll ( ) . where ( "email" , "=" , email ) . executeTakeFirst ( )
477
483
if ( ! user ) {
478
484
return new Response ( null , {
@@ -505,8 +511,8 @@ export class Auth {
505
511
. post (
506
512
"/api/reset-password/:token" ,
507
513
async ( ctx ) => {
508
- return withTransaction ( this . queryBuilder ) ( async ( ) => {
509
- const tx = getCurrentTransaction ( ) ?? this . queryBuilder
514
+ return this . txContext . withTransaction ( async ( ) => {
515
+ const tx = this . txContext . getCurrentTransaction ( )
510
516
511
517
const password = ctx . body . password
512
518
const verificationToken = ctx . params . token
@@ -589,7 +595,8 @@ export class Auth {
589
595
}
590
596
591
597
await this . lucia . invalidateUserSessions ( user . id )
592
- await ( getCurrentTransaction ( ) ?? this . queryBuilder )
598
+ await this . txContext
599
+ . getCurrentTransaction ( )
593
600
. updateTable ( "undb_user" )
594
601
. set ( "email_verified" , true )
595
602
. where ( "id" , "=" , user . id )
@@ -615,7 +622,7 @@ export class Auth {
615
622
. get (
616
623
"/invitation/:invitationId/accept" ,
617
624
async ( ctx ) => {
618
- return withTransaction ( this . queryBuilder ) ( async ( ) => {
625
+ return this . txContext . withTransaction ( async ( ) => {
619
626
const { invitationId } = ctx . params
620
627
await this . commandBus . execute ( new AcceptInvitationCommand ( { id : invitationId } ) )
621
628
0 commit comments