diff --git a/src/utils/loginRedirect.ts b/src/utils/loginRedirect.ts new file mode 100644 index 0000000..4c43ff1 --- /dev/null +++ b/src/utils/loginRedirect.ts @@ -0,0 +1,30 @@ +import { PageEnum } from '@/enums/pageEnum'; + +function getAppBaseUrl() { + const baseUrl = import.meta.env.BASE_URL || '/'; + return baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`; +} + +function getCurrentRouteFullPath() { + const hashPath = window.location.hash.replace(/^#/, ''); + if (hashPath) return hashPath; + + const { pathname, search } = window.location; + const appBase = getAppBaseUrl().replace(/\/$/, ''); + const routePath = appBase && pathname.startsWith(appBase) ? pathname.slice(appBase.length) || '/' : pathname; + return `${routePath}${search}`; +} + +export function getLoginHref(redirect = getCurrentRouteFullPath()) { + const appBase = getAppBaseUrl(); + const redirectQuery = + redirect && !redirect.startsWith(PageEnum.BASE_LOGIN) + ? `?redirect=${encodeURIComponent(redirect)}` + : ''; + + return `${appBase}#${PageEnum.BASE_LOGIN}${redirectQuery}`; +} + +export function replaceToLogin() { + window.location.replace(getLoginHref()); +}