refactor(oidc): inline OIDC utility function
Moves the processCallback function from src/utils/oidc.ts directly into the OidcLoginButton.tsx component where it is used. The now-empty oidc.ts utility file has been removed. This improves code co-location as the function is only used in a single place. Addresses https://github.com/fallenbagel/jellyseerr/pull/1505#discussion_r2194982515
This commit is contained in:
committed by
Michael Thomas
parent
4a3f38fa10
commit
f4988aba15
@@ -1,7 +1,6 @@
|
||||
import defineMessages from '@app/utils/defineMessages';
|
||||
import { processCallback } from '@app/utils/oidc';
|
||||
import type { PublicOidcProvider } from '@server/lib/settings';
|
||||
import axios from 'axios';
|
||||
import axios, { isAxiosError } from 'axios'; // <-- isAxiosError is now needed here
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useIntl } from 'react-intl';
|
||||
@@ -11,6 +10,33 @@ const messages = defineMessages('components.Login', {
|
||||
oidcLoginError: 'An error occurred while logging in with {provider}.',
|
||||
});
|
||||
|
||||
// The function from src/utils/oidc.ts is now here as a local helper
|
||||
async function processCallback(params: URLSearchParams, provider: string) {
|
||||
const url = new URL(
|
||||
`/api/v1/auth/oidc/callback/${encodeURIComponent(provider)}`,
|
||||
window.location.origin
|
||||
);
|
||||
url.search = params.toString();
|
||||
|
||||
try {
|
||||
const res = await axios.get(url.toString());
|
||||
|
||||
return {
|
||||
type: 'success',
|
||||
message: res.data,
|
||||
};
|
||||
} catch (e) {
|
||||
if (isAxiosError(e) && e.response?.data?.message) {
|
||||
return { type: 'error', message: e.response.data.message };
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'error',
|
||||
message: e.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
type OidcLoginButtonProps = {
|
||||
provider: PublicOidcProvider;
|
||||
onError?: (message: string) => void;
|
||||
@@ -43,6 +69,7 @@ export default function OidcLoginButton({
|
||||
}, [provider, intl, onError]);
|
||||
|
||||
const handleCallback = useCallback(async () => {
|
||||
// This now calls the local helper function
|
||||
const result = await processCallback(searchParams, provider.slug);
|
||||
if (result.type === 'success') {
|
||||
// redirect to homepage
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import axios, { isAxiosError } from 'axios'; // <-- Import axios and isAxiosError
|
||||
|
||||
export async function processCallback(
|
||||
params: URLSearchParams,
|
||||
provider: string
|
||||
) {
|
||||
const url = new URL(
|
||||
`/api/v1/auth/oidc/callback/${encodeURIComponent(provider)}`,
|
||||
window.location.origin
|
||||
);
|
||||
url.search = params.toString();
|
||||
|
||||
try {
|
||||
// The fetch call is replaced with axios.get.
|
||||
// On success, the JSON response is automatically parsed and available in res.data.
|
||||
const res = await axios.get(url.toString());
|
||||
|
||||
return {
|
||||
type: 'success',
|
||||
message: res.data,
|
||||
};
|
||||
} catch (e) {
|
||||
// Axios throws an error for non-2xx responses. We can check if it's an axios error
|
||||
// to safely access the error message from the server's response payload.
|
||||
if (isAxiosError(e) && e.response?.data?.message) {
|
||||
return { type: 'error', message: e.response.data.message };
|
||||
}
|
||||
|
||||
// Fallback for generic errors
|
||||
return {
|
||||
type: 'error',
|
||||
message: e.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user