Skip to content

Commit

Permalink
optimize(projects): optimize getRouteQueryOfLoginRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jun 21, 2024
1 parent 49f60b2 commit 693f704
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/router/guard/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,7 @@ async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw
// initialize the auth route requires the user to be logged in, if not, redirect to the login page
if (!isLogin) {
const loginRoute: RouteKey = 'login';
const redirect = to.fullPath;
const [redirectPath, redirectQuery] = redirect.split('?');
const redirectName = getRouteName(redirectPath as RoutePath);

const isRedirectHome = routeStore.routeHome === redirectName || import.meta.env.VITE_ROUTE_HOME === redirectName;

const query: LocationQueryRaw = to.name !== loginRoute && !isRedirectHome ? { redirect } : {};

if (isRedirectHome && redirectQuery) {
query.redirect = `/?${redirectQuery}`;
}
const query = getRouteQueryOfLoginRoute(to, routeStore.routeHome);

const location: RouteLocationRaw = {
name: loginRoute,
Expand Down Expand Up @@ -206,3 +196,20 @@ function handleRouteSwitch(to: RouteLocationNormalized, from: RouteLocationNorma

next();
}

function getRouteQueryOfLoginRoute(to: RouteLocationNormalized, routeHome: RouteKey) {
const loginRoute: RouteKey = 'login';
const redirect = to.fullPath;
const [redirectPath, redirectQuery] = redirect.split('?');
const redirectName = getRouteName(redirectPath as RoutePath);

const isRedirectHome = routeHome === redirectName;

const query: LocationQueryRaw = to.name !== loginRoute && !isRedirectHome ? { redirect } : {};

if (isRedirectHome && redirectQuery) {
query.redirect = `/?${redirectQuery}`;
}

return query;
}

0 comments on commit 693f704

Please sign in to comment.