Merge pull request #799 from Hexastack/feat/support-redirection-with-query-params

feat: support redirection with query params
This commit is contained in:
Yassine 2025-04-11 07:41:36 +01:00 committed by GitHub
commit 0746b1c187
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 37 deletions

View File

@ -8,7 +8,7 @@
import getConfig from "next/config"; import getConfig from "next/config";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { createContext, ReactNode, useEffect, useState } from "react"; import { createContext, ReactNode } from "react";
import { import {
QueryObserverResult, QueryObserverResult,
RefetchOptions, RefetchOptions,
@ -25,7 +25,6 @@ import { useSubscribeBroadcastChannel } from "@/hooks/useSubscribeBroadcastChann
import { useTranslate } from "@/hooks/useTranslate"; import { useTranslate } from "@/hooks/useTranslate";
import { RouterType } from "@/services/types"; import { RouterType } from "@/services/types";
import { IUser } from "@/types/user.types"; import { IUser } from "@/types/user.types";
import { getFromQuery } from "@/utils/URL";
export interface AuthContextValue { export interface AuthContextValue {
user: IUser | undefined; user: IUser | undefined;
@ -51,10 +50,8 @@ const { publicRuntimeConfig } = getConfig();
export const AuthProvider = ({ children }: AuthProviderProps): JSX.Element => { export const AuthProvider = ({ children }: AuthProviderProps): JSX.Element => {
const router = useRouter(); const router = useRouter();
const [search, setSearch] = useState("");
const hasPublicPath = PUBLIC_PATHS.includes(router.pathname); const hasPublicPath = PUBLIC_PATHS.includes(router.pathname);
const { i18n } = useTranslate(); const { i18n } = useTranslate();
const [isReady, setIsReady] = useState(false);
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const updateLanguage = (lang: string) => { const updateLanguage = (lang: string) => {
i18n.changeLanguage(lang); i18n.changeLanguage(lang);
@ -66,11 +63,11 @@ export const AuthProvider = ({ children }: AuthProviderProps): JSX.Element => {
}; };
const authRedirection = async (isAuthenticated: boolean) => { const authRedirection = async (isAuthenticated: boolean) => {
if (isAuthenticated) { if (isAuthenticated) {
const redirect = getFromQuery({ search, key: "redirect" }); if (
const nextPage = redirect && decodeURIComponent(redirect); router.query.redirect &&
router.query.redirect.toString().startsWith("/")
if (nextPage?.startsWith("/")) { ) {
await router.push(nextPage); await router.push(router.query.redirect.toString());
} else if (hasPublicPath) { } else if (hasPublicPath) {
await router.push(RouterType.HOME); await router.push(RouterType.HOME);
} }
@ -109,14 +106,9 @@ export const AuthProvider = ({ children }: AuthProviderProps): JSX.Element => {
router.reload(); router.reload();
}); });
useEffect(() => { if (isLoading) {
const search = location.search; return <Progress />;
}
setSearch(search);
setIsReady(true);
}, []);
if (!isReady || isLoading) return <Progress />;
return ( return (
<AuthContext.Provider <AuthContext.Provider

View File

@ -6,26 +6,6 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
export const getFromQuery = ({
key,
search,
defaultValue = "",
}: {
key: string;
search?: string;
defaultValue?: string;
}) => {
try {
const paramsString = search || window.location.search;
const searchParams = new URLSearchParams(paramsString);
const loadCampaign = searchParams.get(key) || defaultValue;
return loadCampaign;
} catch (e) {
return defaultValue;
}
};
export const buildURL = (baseUrl: string, relativePath: string): string => { export const buildURL = (baseUrl: string, relativePath: string): string => {
try { try {
return new URL(relativePath).toString(); return new URL(relativePath).toString();