|
1
|
import type { |
|
2
|
AuthMfaFactorResponse, |
|
3
|
AuthMfaListResponse, |
|
4
|
AuthMfaRecoveryCodesResponse, |
|
5
|
AuthMfaSetupResponse, |
|
6
|
FetchError, |
|
7
|
} from "@medusajs/js-sdk" |
|
8
|
import type { AuthTypes } from "@medusajs/types" |
|
9
|
import { |
|
10
|
QueryKey, |
|
11
|
UseMutationOptions, |
|
12
|
UseQueryOptions, |
|
13
|
useMutation, |
|
14
|
useQuery, |
|
15
|
} from "@tanstack/react-query" |
|
16
|
import { sdk } from "../../lib/client" |
|
17
|
import { queryClient } from "../../lib/query-client" |
|
18
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
19
|
|
|
20
|
const MFA_QUERY_KEY = "mfa" as const |
|
21
|
export const mfaQueryKeys = queryKeysFactory(MFA_QUERY_KEY) |
|
22
|
|
|
23
|
export const callbackWithCloudAuth = async ( |
|
24
|
query: Record<string, unknown> |
|
25
|
): ReturnType<typeof sdk.auth.callback> => |
|
26
|
sdk.auth.callback("user", "cloud", query) |
|
27
|
|
|
28
|
export const useAuthMfa = ( |
|
29
|
options?: Omit< |
|
30
|
UseQueryOptions< |
|
31
|
AuthMfaListResponse, |
|
32
|
FetchError, |
|
33
|
AuthMfaListResponse, |
|
34
|
QueryKey |
|
35
|
>, |
|
36
|
"queryFn" | "queryKey" |
|
37
|
> |
|
38
|
) => { |
|
39
|
const { data, ...rest } = useQuery({ |
|
40
|
queryFn: () => sdk.auth.mfa.list(), |
|
41
|
queryKey: mfaQueryKeys.lists(), |
|
42
|
...options, |
|
43
|
}) |
|
44
|
|
|
45
|
return { ...data, ...rest } |
|
46
|
} |
|
47
|
|
|
48
|
export const useStartAuthMfa = ( |
|
49
|
options?: UseMutationOptions< |
|
50
|
AuthMfaSetupResponse, |
|
51
|
FetchError, |
|
52
|
{ |
|
53
|
provider: AuthTypes.AuthMfaProviderMethod |
|
54
|
label?: string | null |
|
55
|
issuer?: string |
|
56
|
metadata?: Record<string, unknown> | null |
|
57
|
} |
|
58
|
> |
|
59
|
) => { |
|
60
|
return useMutation({ |
|
61
|
mutationFn: (payload) => sdk.auth.mfa.start(payload), |
|
62
|
...options, |
|
63
|
onSuccess: (data, variables, context) => { |
|
64
|
queryClient.invalidateQueries({ queryKey: mfaQueryKeys.lists() }) |
|
65
|
options?.onSuccess?.(data, variables, context) |
|
66
|
}, |
|
67
|
}) |
|
68
|
} |
|
69
|
|
|
70
|
export const useVerifyAuthMfa = ( |
|
71
|
id: string, |
|
72
|
options?: UseMutationOptions< |
|
73
|
AuthMfaFactorResponse, |
|
74
|
FetchError, |
|
75
|
{ code: string } |
|
76
|
> |
|
77
|
) => { |
|
78
|
return useMutation({ |
|
79
|
mutationFn: (payload) => sdk.auth.mfa.verify(id, payload), |
|
80
|
...options, |
|
81
|
onSuccess: (data, variables, context) => { |
|
82
|
queryClient.invalidateQueries({ queryKey: mfaQueryKeys.lists() }) |
|
83
|
options?.onSuccess?.(data, variables, context) |
|
84
|
}, |
|
85
|
}) |
|
86
|
} |
|
87
|
|
|
88
|
export const useDisableAuthMfa = ( |
|
89
|
id: string, |
|
90
|
options?: UseMutationOptions< |
|
91
|
AuthMfaFactorResponse, |
|
92
|
FetchError, |
|
93
|
{ method?: AuthTypes.AuthMfaChallengeMethod; code?: string } | void |
|
94
|
> |
|
95
|
) => { |
|
96
|
return useMutation({ |
|
97
|
mutationFn: (payload) => sdk.auth.mfa.disable(id, payload ?? {}), |
|
98
|
...options, |
|
99
|
onSuccess: (data, variables, context) => { |
|
100
|
queryClient.invalidateQueries({ queryKey: mfaQueryKeys.lists() }) |
|
101
|
options?.onSuccess?.(data, variables, context) |
|
102
|
}, |
|
103
|
}) |
|
104
|
} |
|
105
|
|
|
106
|
export const useGenerateAuthMfaRecoveryCodes = ( |
|
107
|
options?: UseMutationOptions< |
|
108
|
AuthMfaRecoveryCodesResponse, |
|
109
|
FetchError, |
|
110
|
{ count?: number } | void |
|
111
|
> |
|
112
|
) => { |
|
113
|
return useMutation({ |
|
114
|
mutationFn: (payload) => sdk.auth.mfa.generateRecoveryCodes(payload ?? {}), |
|
115
|
...options, |
|
116
|
}) |
|
117
|
} |
|
118
|
|
|
119
|
export const useVerifyAuthMfaChallenge = ( |
|
120
|
options?: UseMutationOptions< |
|
121
|
string, |
|
122
|
FetchError, |
|
123
|
{ id: string; method: AuthTypes.AuthMfaChallengeMethod; code: string } |
|
124
|
> |
|
125
|
) => { |
|
126
|
return useMutation({ |
|
127
|
mutationFn: ({ id, method, code }) => |
|
128
|
sdk.auth.mfa.verifyChallenge(id, { method, code }), |
|
129
|
...options, |
|
130
|
}) |
|
131
|
} |