A social knowledge tool for researchers built on ATProto
at development 2.0 kB view raw
1import { Result, err, ok } from 'src/shared/core/Result'; 2import { IUserRepository } from '../../domain/repositories/IUserRepository'; 3import { 4 IUserAuthenticationService, 5 AuthenticationResult, 6} from '../../domain/services/IUserAuthenticationService'; 7import { DID } from '../../domain/value-objects/DID'; 8import { Handle } from '../../domain/value-objects/Handle'; 9import { User } from '../../domain/User'; 10 11export class FakeUserAuthenticationService 12 implements IUserAuthenticationService 13{ 14 constructor(private userRepository: IUserRepository) {} 15 16 async validateUserCredentials( 17 did: DID, 18 handle?: Handle, 19 ): Promise<Result<AuthenticationResult>> { 20 try { 21 // Use mock data from environment variables 22 const mockDid = process.env.BSKY_DID || 'did:plc:mock123'; 23 const mockHandle = process.env.BSKY_HANDLE || 'mock.bsky.social'; 24 25 // Create mock DID and Handle 26 const mockDIDResult = DID.create(mockDid); 27 if (mockDIDResult.isErr()) { 28 return err(mockDIDResult.error); 29 } 30 31 const mockHandleResult = Handle.create(mockHandle); 32 if (mockHandleResult.isErr()) { 33 return err(mockHandleResult.error); 34 } 35 36 // Try to find existing user 37 const userResult = await this.userRepository.findByDID( 38 mockDIDResult.value, 39 ); 40 41 if (userResult.isErr()) { 42 return err(userResult.error); 43 } 44 45 const existingUser = userResult.value; 46 47 // If user exists, return it 48 if (existingUser) { 49 return ok({ 50 user: existingUser, 51 isNewUser: false, 52 }); 53 } 54 55 // Create new user with mock data 56 const newUserResult = User.createNew( 57 mockDIDResult.value, 58 mockHandleResult.value, 59 ); 60 61 if (newUserResult.isErr()) { 62 return err(newUserResult.error); 63 } 64 65 return ok({ 66 user: newUserResult.value, 67 isNewUser: true, 68 }); 69 } catch (error: any) { 70 return err(error); 71 } 72 } 73}