loginRedirect.ts
This commit is contained in:
30
src/utils/loginRedirect.ts
Normal file
30
src/utils/loginRedirect.ts
Normal file
@@ -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());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user