|
1
|
import { |
|
2
|
AuthLoginResponse, |
|
3
|
AuthRegisterResponse, |
|
4
|
FetchError, |
|
5
|
} from "@medusajs/js-sdk" |
|
6
|
import { HttpTypes } from "@medusajs/types" |
|
7
|
import { |
|
8
|
UseMutationOptions, |
|
9
|
UseQueryOptions, |
|
10
|
useMutation, |
|
11
|
useQuery, |
|
12
|
} from "@tanstack/react-query" |
|
13
|
import { sdk } from "../../lib/client" |
|
14
|
|
|
15
|
/** |
|
16
|
* Public information about an auth provider registered for the `user` actor, |
|
17
|
* as returned by `GET /auth/user/providers`. |
|
18
|
*/ |
|
19
|
export type AuthProvider = HttpTypes.AuthProvider |
|
20
|
|
|
21
|
export const authProvidersQueryKey = ["auth", "providers"] as const |
|
22
|
|
|
23
|
/** |
|
24
|
* Fetches the auth providers available for the `user` actor. This is a public, |
|
25
|
* pre-auth endpoint, so no authenticated session is required. |
|
26
|
*/ |
|
27
|
export const useAuthProviders = ( |
|
28
|
options?: Omit< |
|
29
|
UseQueryOptions<HttpTypes.AuthProvidersListResponse, FetchError>, |
|
30
|
"queryKey" | "queryFn" |
|
31
|
> |
|
32
|
) => { |
|
33
|
const { data, ...rest } = useQuery({ |
|
34
|
queryKey: authProvidersQueryKey, |
|
35
|
queryFn: async () => sdk.auth.listProviders("user"), |
|
36
|
staleTime: 5 * 60 * 1000, |
|
37
|
...options, |
|
38
|
}) |
|
39
|
|
|
40
|
return { ...data, ...rest } |
|
41
|
} |
|
42
|
|
|
43
|
export const provisionAuthUser = async ( |
|
44
|
provider: string, |
|
45
|
token: string |
|
46
|
): Promise<void> => { |
|
47
|
await sdk.auth.createUser(provider, { |
|
48
|
Authorization: `Bearer ${token}`, |
|
49
|
}) |
|
50
|
} |
|
51
|
|
|
52
|
export const useSignInWithEmailPass = ( |
|
53
|
options?: UseMutationOptions< |
|
54
|
AuthLoginResponse, |
|
55
|
FetchError, |
|
56
|
HttpTypes.AdminSignUpWithEmailPassword |
|
57
|
> |
|
58
|
) => { |
|
59
|
return useMutation({ |
|
60
|
mutationFn: (payload) => |
|
61
|
sdk.auth.login( |
|
62
|
"user", |
|
63
|
"emailpass", |
|
64
|
payload |
|
65
|
) as Promise<AuthLoginResponse>, |
|
66
|
onSuccess: async (data, variables, context) => { |
|
67
|
options?.onSuccess?.(data, variables, context) |
|
68
|
}, |
|
69
|
...options, |
|
70
|
}) |
|
71
|
} |
|
72
|
|
|
73
|
export const useSignUpWithEmailPass = ( |
|
74
|
options?: UseMutationOptions< |
|
75
|
AuthRegisterResponse, |
|
76
|
FetchError, |
|
77
|
HttpTypes.AdminSignInWithEmailPassword |
|
78
|
> |
|
79
|
) => { |
|
80
|
return useMutation({ |
|
81
|
mutationFn: (payload) => sdk.auth.register("user", "emailpass", payload), |
|
82
|
onSuccess: async (data, variables, context) => { |
|
83
|
options?.onSuccess?.(data, variables, context) |
|
84
|
}, |
|
85
|
...options, |
|
86
|
}) |
|
87
|
} |
|
88
|
|
|
89
|
export const useResetPasswordForEmailPass = ( |
|
90
|
options?: UseMutationOptions<void, FetchError, { email: string }> |
|
91
|
) => { |
|
92
|
return useMutation({ |
|
93
|
mutationFn: (payload) => |
|
94
|
sdk.auth.resetPassword("user", "emailpass", { |
|
95
|
identifier: payload.email, |
|
96
|
}), |
|
97
|
onSuccess: async (data, variables, context) => { |
|
98
|
options?.onSuccess?.(data, variables, context) |
|
99
|
}, |
|
100
|
...options, |
|
101
|
}) |
|
102
|
} |
|
103
|
|
|
104
|
export const useLogout = (options?: UseMutationOptions<void, FetchError>) => { |
|
105
|
return useMutation({ |
|
106
|
mutationFn: () => sdk.auth.logout(), |
|
107
|
...options, |
|
108
|
}) |
|
109
|
} |
|
110
|
|
|
111
|
export const useUpdateProviderForEmailPass = ( |
|
112
|
token: string, |
|
113
|
options?: UseMutationOptions<void, FetchError, HttpTypes.AdminUpdateProvider> |
|
114
|
) => { |
|
115
|
return useMutation({ |
|
116
|
mutationFn: (payload) => |
|
117
|
sdk.auth.updateProvider("user", "emailpass", payload, token), |
|
118
|
onSuccess: async (data, variables, context) => { |
|
119
|
options?.onSuccess?.(data, variables, context) |
|
120
|
}, |
|
121
|
...options, |
|
122
|
}) |
|
123
|
} |