diff --git a/deepfence_frontend/apps/dashboard/src/features/auth/actions/loginAction.ts b/deepfence_frontend/apps/dashboard/src/features/auth/actions/loginAction.ts index e5b00a3f25..e8e964ee9e 100644 --- a/deepfence_frontend/apps/dashboard/src/features/auth/actions/loginAction.ts +++ b/deepfence_frontend/apps/dashboard/src/features/auth/actions/loginAction.ts @@ -1,9 +1,9 @@ -import { ActionFunction, redirect } from 'react-router-dom'; +import { ActionFunction } from 'react-router-dom'; import { getAuthenticationApiClient } from '@/api/api'; import { ApiDocsBadRequestResponse } from '@/api/generated'; -import { apiWrapper, validateRedirectToUrl } from '@/utils/api'; -import storage from '@/utils/storage'; +import { apiWrapper } from '@/utils/api'; +import { handleLoginAndRedirect } from '@/utils/auth'; export type LoginActionReturnType = { error?: string; @@ -53,19 +53,5 @@ export const loginAction: ActionFunction = async ({ throw loginResponse.error; } - storage.setAuth({ - accessToken: loginResponse.value.access_token, - refreshToken: loginResponse.value.refresh_token, - }); - - const redirectTo = url.searchParams.get('redirectTo'); - if (redirectTo && validateRedirectToUrl(redirectTo)) { - throw redirect(url.searchParams.get('redirectTo') as string, 302); - } - - if (!loginResponse.value.onboarding_required) { - throw redirect('/dashboard', 302); - } - - throw redirect('/onboard', 302); + handleLoginAndRedirect(loginResponse.value, url.searchParams); }; diff --git a/deepfence_frontend/apps/dashboard/src/features/auth/actions/registerAction.ts b/deepfence_frontend/apps/dashboard/src/features/auth/actions/registerAction.ts index 3e21e2dca0..89fa506aa3 100644 --- a/deepfence_frontend/apps/dashboard/src/features/auth/actions/registerAction.ts +++ b/deepfence_frontend/apps/dashboard/src/features/auth/actions/registerAction.ts @@ -1,10 +1,10 @@ -import { ActionFunction, redirect } from 'react-router-dom'; +import { ActionFunction } from 'react-router-dom'; import { getUserApiClient } from '@/api/api'; import { ApiDocsBadRequestResponse } from '@/api/generated'; import { get403Message } from '@/utils/403'; import { apiWrapper } from '@/utils/api'; -import storage from '@/utils/storage'; +import { handleLoginAndRedirect } from '@/utils/auth'; export type RegisterActionReturnType = { error?: string; @@ -72,9 +72,5 @@ export const registerAction: ActionFunction = async ({ throw registerUserResponse.error; } - storage.setAuth({ - accessToken: registerUserResponse.value.access_token, - refreshToken: registerUserResponse.value.refresh_token, - }); - throw redirect('/onboard', 302); + handleLoginAndRedirect(registerUserResponse.value); }; diff --git a/deepfence_frontend/apps/dashboard/src/features/auth/pages/RegisterWithInvite.tsx b/deepfence_frontend/apps/dashboard/src/features/auth/pages/RegisterWithInvite.tsx index 7f51eeacf1..87172f5bc6 100644 --- a/deepfence_frontend/apps/dashboard/src/features/auth/pages/RegisterWithInvite.tsx +++ b/deepfence_frontend/apps/dashboard/src/features/auth/pages/RegisterWithInvite.tsx @@ -1,11 +1,5 @@ import { useState } from 'react'; -import { - ActionFunctionArgs, - Link, - redirect, - useFetcher, - useSearchParams, -} from 'react-router-dom'; +import { ActionFunctionArgs, Link, useFetcher, useSearchParams } from 'react-router-dom'; import { cn } from 'tailwind-preset'; import { Button, Checkbox, TextInput } from 'ui-components'; @@ -14,7 +8,7 @@ import { ApiDocsBadRequestResponse } from '@/api/generated'; import { DFLink } from '@/components/DFLink'; import { get403Message } from '@/utils/403'; import { apiWrapper } from '@/utils/api'; -import storage from '@/utils/storage'; +import { handleLoginAndRedirect } from '@/utils/auth'; export type RegisterWithInviteActionReturnType = { error?: string; @@ -76,17 +70,7 @@ const action = async ({ } throw registerInvitedUserResponse.error; } - - storage.setAuth({ - accessToken: registerInvitedUserResponse.value.access_token, - refreshToken: registerInvitedUserResponse.value.refresh_token, - }); - - if (!registerInvitedUserResponse.value.onboarding_required) { - throw redirect('/dashboard', 302); - } - - throw redirect('/onboard', 302); + handleLoginAndRedirect(registerInvitedUserResponse.value); }; const RegisterWithInvite = () => { diff --git a/deepfence_frontend/apps/dashboard/src/utils/auth.ts b/deepfence_frontend/apps/dashboard/src/utils/auth.ts new file mode 100644 index 0000000000..f8c1621609 --- /dev/null +++ b/deepfence_frontend/apps/dashboard/src/utils/auth.ts @@ -0,0 +1,26 @@ +import { redirect } from 'react-router-dom'; + +import { ModelLoginResponse } from '@/api/generated'; +import { validateRedirectToUrl } from '@/utils/api'; +import storage from '@/utils/storage'; + +export function handleLoginAndRedirect( + loginResponse: ModelLoginResponse, + searchParams?: URLSearchParams, +): never { + storage.setAuth({ + accessToken: loginResponse.access_token, + refreshToken: loginResponse.refresh_token, + }); + + if (loginResponse.onboarding_required) { + throw redirect('/onboard', 302); + } + + const redirectTo = searchParams?.get('redirectTo'); + if (redirectTo && validateRedirectToUrl(redirectTo)) { + throw redirect(redirectTo, 302); + } + + throw redirect('/dashboard', 302); +}