1 import {
2 AuthenticationInput,
3 AuthenticationResponse,
4 AuthIdentityDTO,
5 AuthIdentityProviderService,
6 } from "@medusajs/framework/types"
7 import {
8 AbstractAuthModuleProvider,
9 MedusaError,
10 } from "@medusajs/framework/utils"
11
12 export class AuthServiceFixtures extends AbstractAuthModuleProvider {
13 static identifier = "plaintextpass"
14
15 constructor() {
16 super(
17 {},
18 { provider: "plaintextpass", displayName: "plaintextpass Fixture" }
19 )
20 }
21
22 async authenticate(
23 authenticationData: AuthenticationInput,
24 service: AuthIdentityProviderService
25 ): Promise<AuthenticationResponse> {
26 const { email, password } = authenticationData.body ?? {}
27 let authIdentity: AuthIdentityDTO | undefined
28 try {
29 authIdentity = await service.retrieve({
30 entity_id: email,
31 })
32
33 // The provider has to be present, guaranteed by the retrieve filter above.
34 const providerIdentity = authIdentity.provider_identities?.find(
35 (pi) => pi.provider === this.provider
36 )!
37
38 if (providerIdentity.provider_metadata?.password === password) {
39 return {
40 success: true,
41 authIdentity,
42 }
43 }
44 } catch (error) {
45 if (error.type === MedusaError.Types.NOT_FOUND) {
46 const createdAuthIdentity = await service.create({
47 entity_id: email,
48 provider_metadata: {
49 password,
50 },
51 })
52
53 return {
54 success: true,
55 authIdentity: createdAuthIdentity,
56 }
57 }
58
59 return { success: false, error: error.message }
60 }
61
62 return {
63 success: false,
64 error: "Invalid email or password",
65 }
66 }
67 }
68
69 export const services = [AuthServiceFixtures]