-
Notifications
You must be signed in to change notification settings - Fork 4
Fix disconnect social auth bugs #1600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
787f3d9
096b8fd
0eec26d
15ddefd
4310ad1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,31 @@ | ||
| import { type ReactElement, useRef } from "react"; | ||
|
|
||
| import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
| import { faGithub, faDiscord } from "@fortawesome/free-brands-svg-icons"; | ||
|
|
||
| import { useOutletContext, useRevalidator } from "react-router"; | ||
|
|
||
| import { NewLink, OverwolfLogo, useToast } from "@thunderstore/cyberstorm"; | ||
| import { ApiAction } from "@thunderstore/ts-api-react-actions"; | ||
| import { ApiError } from "@thunderstore/thunderstore-api"; | ||
|
|
||
| import { useOutletContext } from "react-router"; | ||
| import { buildAuthLoginUrl } from "cyberstorm/utils/ThunderstoreAuth"; | ||
|
|
||
| import { userLinkedAccountDisconnect } from "../../../../../../packages/thunderstore-api/src"; | ||
| import { getPublicEnvVariables } from "cyberstorm/security/publicEnvVariables"; | ||
|
|
||
| import { Connection } from "~/commonComponents/Connection/Connection"; | ||
| import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
| import { faGithub, faDiscord } from "@fortawesome/free-brands-svg-icons"; | ||
| import { type ReactElement } from "react"; | ||
| import { type OutletContextShape } from "~/root"; | ||
| import { ApiAction } from "@thunderstore/ts-api-react-actions"; | ||
| import { NotLoggedIn } from "~/commonComponents/NotLoggedIn/NotLoggedIn"; | ||
| import { getPublicEnvVariables } from "cyberstorm/security/publicEnvVariables"; | ||
| import { type OutletContextShape } from "~/root"; | ||
|
|
||
| import { userLinkedAccountDisconnect } from "../../../../../../packages/thunderstore-api/src"; | ||
| import { useHydrated } from "remix-utils/use-hydrated"; | ||
|
|
||
| type ProvidersType = { | ||
| name: string; | ||
| identifier: "discord" | "github" | "overwolf"; | ||
| icon: ReactElement; | ||
| }[]; | ||
| }; | ||
|
|
||
| export const PROVIDERS: ProvidersType = [ | ||
| export const PROVIDERS: ProvidersType[] = [ | ||
| { | ||
| name: "Discord", | ||
| identifier: "discord", | ||
|
|
@@ -36,31 +41,59 @@ export const PROVIDERS: ProvidersType = [ | |
|
|
||
| export default function Connections() { | ||
| const outletContext = useOutletContext() as OutletContextShape; | ||
| const isHydrated = useHydrated(); | ||
| const revalidator = useRevalidator(); | ||
| const toast = useToast(); | ||
| const disconnectingProviderRef = useRef<string | null>(null); | ||
|
|
||
| if (!outletContext.currentUser || !outletContext.currentUser.username) | ||
| return <NotLoggedIn />; | ||
| const onlyOneConnected = () => { | ||
| const connectedProviders = | ||
| outletContext.currentUser?.connections?.length ?? 0; | ||
| return connectedProviders === 1; | ||
| }; | ||
|
|
||
| const toast = useToast(); | ||
| const getConnection = (provider: ProvidersType) => | ||
| outletContext.currentUser?.connections?.find( | ||
| (c) => c.provider?.toLowerCase() === provider.identifier | ||
| ); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const onSubmitSuccess = (_r: unknown) => { | ||
| if (!outletContext.currentUser || !outletContext.currentUser.username) | ||
| throw new Error("User not logged in"); | ||
| const username = outletContext.currentUser?.username; | ||
|
|
||
| revalidator.revalidate(); | ||
|
|
||
| toast.addToast({ | ||
| csVariant: "success", | ||
| children: ( | ||
| <> | ||
| User {outletContext.currentUser.username} was disconnected from TODO | ||
| User {username} was disconnected from{" "} | ||
| {disconnectingProviderRef.current} | ||
| </> | ||
| ), | ||
| duration: 30000, | ||
| }); | ||
|
|
||
| disconnectingProviderRef.current = null; | ||
| }; | ||
|
Comment on lines
61
to
78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Toast message shows provider identifier instead of display name. Line 71 displays Apply this diff to show the proper provider name: const onSubmitSuccess = (_r: unknown) => {
const username = outletContext.currentUser?.username;
+ const providerName = PROVIDERS.find(
+ p => p.identifier === disconnectingProviderRef.current
+ )?.name || disconnectingProviderRef.current;
revalidator.revalidate();
toast.addToast({
csVariant: "success",
children: (
<>
- User {username} was disconnected from{" "}
- {disconnectingProviderRef.current}
+ User {username} was disconnected from {providerName}
</>
),
duration: 30000,
});
disconnectingProviderRef.current = null;
};
🤖 Prompt for AI Agents |
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const onSubmitError = (_e: unknown) => { | ||
|
|
||
| const onSubmitError = (error: unknown) => { | ||
| let message = "Error when disconnecting account."; | ||
|
|
||
| if (error instanceof ApiError) { | ||
| const fieldErrors = error.getFieldErrors(); | ||
| message = | ||
| fieldErrors.non_field_errors?.[0] || | ||
| fieldErrors.detail?.[0] || | ||
| fieldErrors.root?.[0] || | ||
| error.message || | ||
| message; | ||
| } | ||
|
|
||
| toast.addToast({ | ||
| csVariant: "danger", | ||
| children: <>Unknown error occurred. The error has been logged</>, | ||
| children: <>{message}</>, | ||
| duration: 8000, | ||
| }); | ||
| }; | ||
|
|
||
|
|
@@ -73,8 +106,17 @@ export default function Connections() { | |
| const publicEnvVariables = getPublicEnvVariables([ | ||
| "VITE_AUTH_BASE_URL", | ||
| "VITE_AUTH_RETURN_URL", | ||
| "VITE_BETA_SITE_URL", | ||
| ]); | ||
|
|
||
| if (!isHydrated) { | ||
| return <div style={{ padding: "32px" }}>Loading...</div>; | ||
| } | ||
|
|
||
| if (!outletContext.currentUser) { | ||
| return <NotLoggedIn />; | ||
| } | ||
|
|
||
| return ( | ||
| <div className="settings-items"> | ||
| <div className="settings-items__item"> | ||
|
|
@@ -94,45 +136,31 @@ export default function Connections() { | |
| </p> | ||
| </div> | ||
| <div className="settings-items__content"> | ||
| {PROVIDERS.map((p) => { | ||
| if ( | ||
| !outletContext.currentUser || | ||
| !outletContext.currentUser.username | ||
| ) | ||
| throw new Error("User not logged in"); | ||
| return ( | ||
| <Connection | ||
| key={p.name} | ||
| icon={p.icon} | ||
| name={p.name} | ||
| identifier={p.identifier} | ||
| connection={outletContext.currentUser.connections?.find( | ||
| (c) => c.provider.toLowerCase() === p.identifier | ||
| )} | ||
| connectionLink={buildAuthLoginUrl({ | ||
| type: p.identifier, | ||
| authBaseDomain: publicEnvVariables.VITE_AUTH_BASE_URL || "", | ||
| authReturnDomain: | ||
| publicEnvVariables.VITE_AUTH_RETURN_URL || "", | ||
| })} | ||
| disconnectFunction={(p) => { | ||
| if ( | ||
| !outletContext.currentUser || | ||
| !outletContext.currentUser.username | ||
| ) | ||
| throw new Error("User not logged in"); | ||
| return onSubmit({ | ||
| params: { | ||
| provider: p, | ||
| }, | ||
| config: outletContext.requestConfig, | ||
| queryParams: {}, | ||
| data: { provider: p }, | ||
| }); | ||
| }} | ||
| /> | ||
| ); | ||
| })} | ||
| {PROVIDERS.map((provider) => ( | ||
| <Connection | ||
| key={provider.name} | ||
| disabled={onlyOneConnected() && !!getConnection(provider)} | ||
| icon={provider.icon} | ||
| name={provider.name} | ||
| identifier={provider.identifier} | ||
| connection={getConnection(provider)} | ||
| connectionLink={buildAuthLoginUrl({ | ||
| type: provider.identifier, | ||
| authBaseDomain: publicEnvVariables.VITE_AUTH_BASE_URL || "", | ||
| authReturnDomain: publicEnvVariables.VITE_AUTH_RETURN_URL || "", | ||
| nextUrl: `${publicEnvVariables.VITE_BETA_SITE_URL}/settings`, | ||
| })} | ||
| disconnectFunction={(provider) => { | ||
| disconnectingProviderRef.current = provider; | ||
| return onSubmit({ | ||
| params: { provider }, | ||
| config: outletContext.requestConfig, | ||
| queryParams: {}, | ||
| data: { provider }, | ||
| }); | ||
| }} | ||
| /> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix provider typing in
getConnection.Parameter is typed as
ProvidersType(the whole array), soidentifieris missing and the file won’t compile. Please type it as a single provider entry instead.🤖 Prompt for AI Agents