-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1345 from flanksource/fix-clerk-kratos-switching-…
…not-working fix: fix switch between clerk and kratos not working
- Loading branch information
Showing
4 changed files
with
172 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/components/Authentication/Clerk/ClerkAuthContextProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useOrganization } from "@clerk/nextjs"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { AxiosError } from "axios"; | ||
import { User, whoami } from "../../../api/services/users"; | ||
import { AuthContext } from "../../../context"; | ||
import ErrorPage from "../../Errors/ErrorPage"; | ||
import FullPageSkeletonLoader from "../../SkeletonLoader/FullPageSkeletonLoader"; | ||
import InstanceCreationInProgress from "./InstanceCreationInProgress"; | ||
|
||
type AuthProviderWrapperProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export default function ClerkAuthContextProvider({ | ||
children | ||
}: AuthProviderWrapperProps) { | ||
// when organization is switched, we need to re-fetch the user and the UI | ||
const { organization } = useOrganization(); | ||
|
||
const { | ||
data: user, | ||
isLoading, | ||
error | ||
} = useQuery<User, AxiosError>( | ||
["user", "whoami", organization], | ||
() => whoami(), | ||
{ | ||
refetchOnWindowFocus: false, | ||
refetchInterval: 0, | ||
refetchOnReconnect: false | ||
} | ||
); | ||
|
||
if (isLoading && !user) { | ||
return <FullPageSkeletonLoader />; | ||
} | ||
|
||
// if the organization backend is not yet created, we need to wait for it to | ||
if ( | ||
error && | ||
(error.response?.status?.toString().startsWith("5") || | ||
error?.response?.status === 404) | ||
) { | ||
return <InstanceCreationInProgress />; | ||
} | ||
|
||
if (error && !user) { | ||
return <ErrorPage error={error} />; | ||
} | ||
|
||
return ( | ||
<AuthContext.Provider value={{ user, setUser: () => {} }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
} |
41 changes: 14 additions & 27 deletions
41
src/components/Authentication/Kratos/KratosAuthContextProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { useFormikContext } from "formik"; | ||
import { get } from "lodash"; | ||
import { useState, useEffect, useCallback } from "react"; | ||
import FormikCheckboxFieldsGroup from "./FormikCheckboxFieldsGroup"; | ||
import FormikEnvVarConfigsFields from "./FormikConfigEnvVarFields"; | ||
import { Switch } from "../../Switch"; | ||
|
||
type FormikAuthFieldsProps = { | ||
name: string; | ||
fields: { | ||
name: string; | ||
label: string; | ||
}[]; | ||
label?: string; | ||
}; | ||
|
||
export default function FormikAuthFields({ | ||
name, | ||
fields, | ||
label = "Authentication" | ||
}: FormikAuthFieldsProps) { | ||
const { setFieldValue, values } = useFormikContext<Record<string, any>>(); | ||
|
||
const [selectedMethod, setSelectedMethod] = useState<"None" | string>(() => { | ||
fields.forEach((field) => { | ||
if (get(values, `${name}.${field.name}`)) { | ||
return field.name; | ||
} | ||
}); | ||
return "None"; | ||
}); | ||
|
||
useEffect(() => { | ||
fields.forEach((field) => { | ||
if (get(values, `${name}.${field.name}`)) { | ||
setSelectedMethod(field.name); | ||
} | ||
}); | ||
}, [fields, name, values]); | ||
|
||
const setAuthenticationMethodFormValue = useCallback( | ||
(method: "None" | string) => { | ||
// reset all fields | ||
fields.forEach((field) => { | ||
setFieldValue(`${name}.${field.name}`, undefined); | ||
}); | ||
|
||
// set the correct method | ||
fields.forEach((field) => { | ||
if (field.name === method) { | ||
setFieldValue(`${name}.${field.name}`, true); | ||
} | ||
}); | ||
}, | ||
[fields, name, setFieldValue] | ||
); | ||
|
||
return ( | ||
<div className="flex flex-col space-y-2"> | ||
<label className="font-semibold text-sm">{label}</label> | ||
<div className="flex flex-row w-full"> | ||
<Switch | ||
options={fields.map((field) => field.label)} | ||
defaultValue="None" | ||
value={selectedMethod} | ||
onChange={(v) => { | ||
setSelectedMethod(v); | ||
setAuthenticationMethodFormValue(v); | ||
}} | ||
/> | ||
</div> | ||
{selectedMethod !== "None" && ( | ||
<div className="flex flex-col p-2"> | ||
<FormikCheckboxFieldsGroup | ||
name={`${name}.authentication.username`} | ||
label="Username" | ||
> | ||
<FormikEnvVarConfigsFields | ||
name={`${name}.authentication.username`} | ||
/> | ||
</FormikCheckboxFieldsGroup> | ||
<FormikCheckboxFieldsGroup | ||
name={`${name}.authentication.password`} | ||
label="Password" | ||
> | ||
<FormikEnvVarConfigsFields | ||
name={`${name}.authentication.password`} | ||
/> | ||
</FormikCheckboxFieldsGroup> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |