Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/auth-providers/create-user-auth-provider-mutation.ts
2501 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { useMutation, useQueryClient } from "@tanstack/react-query";
8
import { CreateAuthProviderRequest } from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";
9
import { authProviderClient } from "../../service/public-api";
10
import { getUserAuthProvidersQueryKey } from "./user-auth-providers-query";
11
12
type CreateAuthProviderArgs = {
13
provider: Pick<CreateAuthProviderRequest, "host" | "type"> & {
14
clientId: string;
15
clientSecret: string;
16
userId: string;
17
authorizationUrl?: string;
18
tokenUrl?: string;
19
};
20
};
21
export const useCreateUserAuthProviderMutation = () => {
22
const queryClient = useQueryClient();
23
24
return useMutation({
25
mutationFn: async ({ provider }: CreateAuthProviderArgs) => {
26
const response = await authProviderClient.createAuthProvider(
27
new CreateAuthProviderRequest({
28
owner: { case: "ownerId", value: provider.userId },
29
host: provider.host,
30
oauth2Config: {
31
clientId: provider.clientId,
32
clientSecret: provider.clientSecret,
33
authorizationUrl: provider.authorizationUrl,
34
tokenUrl: provider.tokenUrl,
35
},
36
type: provider.type,
37
}),
38
);
39
return response.authProvider!;
40
},
41
onSuccess(provider) {
42
const userId = provider?.owner?.value;
43
if (!userId) {
44
return;
45
}
46
47
queryClient.invalidateQueries({ queryKey: getUserAuthProvidersQueryKey(userId) });
48
},
49
});
50
};
51
52