@@ -14,7 +14,7 @@ import { None, Option, Some } from "@undb/domain"
1414import { env } from "@undb/env"
1515import { createLogger } from "@undb/logger"
1616import { 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"
1818import { type ISpaceService , injectSpaceService } from "@undb/space"
1919import { Context , Elysia , t } from "elysia"
2020import type { Session , User } from "lucia"
@@ -25,7 +25,6 @@ import { alphabet, generateRandomString, sha256 } from "oslo/crypto"
2525import { encodeHex } from "oslo/encoding"
2626import { omit } from "radash"
2727import { v7 } from "uuid"
28- import { withTransaction } from "../../db"
2928import { injectLucia } from "./auth.provider"
3029import { OAuth } from "./oauth/oauth"
3130
@@ -52,10 +51,12 @@ export class Auth {
5251 private readonly mailService : IMailService ,
5352 @injectLucia ( )
5453 private readonly lucia : Lucia ,
54+ @injectTxCTX ( )
55+ private readonly txContext : ITxContext ,
5556 ) { }
5657
5758 async #generateEmailVerificationCode( userId : string , email : string ) : Promise < string > {
58- const tx = getCurrentTransaction ( )
59+ const tx = this . txContext . getCurrentTransaction ( )
5960 await tx . deleteFrom ( "undb_email_verification_code" ) . where ( "user_id" , "=" , userId ) . execute ( )
6061 const code = env . UNDB_MOCK_MAIL_CODE || generateRandomString ( 6 , alphabet ( "0-9" ) )
6162 await tx
@@ -71,29 +72,32 @@ export class Auth {
7172 }
7273
7374 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 ( )
8488
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+ } )
9397 }
9498
9599 async #createPasswordResetToken( userId : string ) : Promise < string > {
96- const db = getCurrentTransaction ( ) ?? this . queryBuilder
100+ const db = this . txContext . getCurrentTransaction ( )
97101 await db . deleteFrom ( "undb_password_reset_token" ) . where ( "user_id" , "=" , userId ) . execute ( )
98102 const tokenId = generateIdFromEntropySize ( 25 ) // 40 character
99103 const tokenHash = encodeHex ( await sha256 ( new TextEncoder ( ) . encode ( tokenId ) ) )
@@ -206,8 +210,9 @@ export class Auth {
206210 } ,
207211 } )
208212
209- await withTransaction ( this . queryBuilder ) ( async ( ) => {
210- await getCurrentTransaction ( )
213+ await this . txContext . withTransaction ( async ( ) => {
214+ await this . txContext
215+ . getCurrentTransaction ( )
211216 . insertInto ( "undb_user" )
212217 . values ( {
213218 email : adminEmail ,
@@ -326,8 +331,9 @@ export class Auth {
326331 } ,
327332 } )
328333
329- await withTransaction ( this . queryBuilder ) ( async ( ) => {
330- await getCurrentTransaction ( )
334+ await this . txContext . withTransaction ( async ( ) => {
335+ await this . txContext
336+ . getCurrentTransaction ( )
331337 . insertInto ( "undb_user" )
332338 . values ( {
333339 email,
@@ -470,9 +476,9 @@ export class Auth {
470476 . post (
471477 "/api/reset-password" ,
472478 async ( ctx ) => {
473- return withTransaction ( this . queryBuilder ) ( async ( ) => {
479+ return this . txContext . withTransaction ( async ( ) => {
474480 const email = ctx . body . email
475- const tx = getCurrentTransaction ( ) ?? this . queryBuilder
481+ const tx = this . txContext . getCurrentTransaction ( )
476482 const user = await tx . selectFrom ( "undb_user" ) . selectAll ( ) . where ( "email" , "=" , email ) . executeTakeFirst ( )
477483 if ( ! user ) {
478484 return new Response ( null , {
@@ -505,8 +511,8 @@ export class Auth {
505511 . post (
506512 "/api/reset-password/:token" ,
507513 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 ( )
510516
511517 const password = ctx . body . password
512518 const verificationToken = ctx . params . token
@@ -589,7 +595,8 @@ export class Auth {
589595 }
590596
591597 await this . lucia . invalidateUserSessions ( user . id )
592- await ( getCurrentTransaction ( ) ?? this . queryBuilder )
598+ await this . txContext
599+ . getCurrentTransaction ( )
593600 . updateTable ( "undb_user" )
594601 . set ( "email_verified" , true )
595602 . where ( "id" , "=" , user . id )
@@ -615,7 +622,7 @@ export class Auth {
615622 . get (
616623 "/invitation/:invitationId/accept" ,
617624 async ( ctx ) => {
618- return withTransaction ( this . queryBuilder ) ( async ( ) => {
625+ return this . txContext . withTransaction ( async ( ) => {
619626 const { invitationId } = ctx . params
620627 await this . commandBus . execute ( new AcceptInvitationCommand ( { id : invitationId } ) )
621628
0 commit comments