-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(routing): simplify and improve robustness (#2479)
Signed-off-by: Fernando Fernández <[email protected]>
- Loading branch information
Showing
14 changed files
with
113 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,75 @@ | ||
import type { | ||
RouteLocationNormalized, | ||
RouteLocationPathRaw, | ||
RouteLocationRaw | ||
NavigationGuardReturn, | ||
RouteLocationNormalized | ||
} from 'vue-router'; | ||
import type { RouteNamedMap } from 'vue-router/auto-routes'; | ||
import { until } from '@vueuse/core'; | ||
import { remote } from '@/plugins/remote'; | ||
import { isNil } from '@/utils/validation'; | ||
import { getJSONConfig } from '@/utils/external-config'; | ||
import { jsonConfig } from '@/utils/external-config'; | ||
import { useSnackbar } from '@/composables/use-snackbar'; | ||
import { i18n } from '@/plugins/i18n'; | ||
|
||
const serverAddUrl = '/server/add'; | ||
const serverSelectUrl = '/server/select'; | ||
const serverLoginUrl = '/server/login'; | ||
const serverRoutes = new Set([serverAddUrl, serverSelectUrl]); | ||
const routes = new Set([...serverRoutes, serverLoginUrl]); | ||
const serverWizard = '/wizard'; | ||
const serverPages = new Set<keyof RouteNamedMap>([serverAddUrl, serverSelectUrl, serverLoginUrl, serverWizard]); | ||
|
||
/** | ||
* Performs the login guard redirection ensuring no redirection loops happen | ||
* Gets the best server page based on the current state. | ||
* Note that the final page rendered might differ from the best one here | ||
* in the loginGuard | ||
*/ | ||
function doRedir(dest: RouteLocationPathRaw, to: RouteLocationNormalized) { | ||
return to.path === dest.path | ||
? true | ||
: dest; | ||
} | ||
|
||
/** | ||
* Redirects to login page if there's no user logged in. | ||
*/ | ||
export async function loginGuard( | ||
to: RouteLocationNormalized | ||
): Promise<boolean | RouteLocationRaw> { | ||
const jsonConfig = await getJSONConfig(); | ||
|
||
async function _getBestServerPage(): Promise<Nullish<keyof RouteNamedMap>> { | ||
if (jsonConfig.defaultServerURLs.length && isNil(remote.auth.currentServer)) { | ||
await until(() => remote.auth.currentServer).toBeTruthy({ flush: 'pre' }); | ||
} | ||
|
||
if ( | ||
!isNil(remote.auth.currentServer) | ||
&& !isNil(remote.auth.currentUser) | ||
&& !isNil(remote.auth.currentUserToken) | ||
&& routes.has(to.path) | ||
) { | ||
return doRedir({ path: '/', replace: true }, to); | ||
if (!remote.auth.servers.length) { | ||
return serverAddUrl; | ||
} else if (isNil(remote.auth.currentServer)) { | ||
return serverSelectUrl; | ||
} else if (!remote.auth.currentServer.StartupWizardCompleted) { | ||
return serverWizard; | ||
} | ||
} | ||
|
||
if (jsonConfig.allowServerSelection) { | ||
if (!remote.auth.servers.length) { | ||
return doRedir({ path: serverAddUrl, replace: true }, to); | ||
} else if (isNil(remote.auth.currentServer)) { | ||
return doRedir({ path: serverSelectUrl, replace: true }, to); | ||
} | ||
} else { | ||
return doRedir({ path: serverLoginUrl, replace: true }, to); | ||
export const loginGuard = async ( | ||
to: RouteLocationNormalized, | ||
from: RouteLocationNormalized | ||
): Promise<Exclude<NavigationGuardReturn, Error>> => { | ||
const toServerPages = serverPages.has(to.name); | ||
|
||
if (!jsonConfig.allowServerSelection && toServerPages) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
const fromServerPages = serverPages.has(from.name); | ||
const res = await _getBestServerPage(); | ||
|
||
const loggedIn = !isNil(remote.auth.currentUser); | ||
const shouldBlockToServer = loggedIn && toServerPages; | ||
const shouldBlockToApp = !loggedIn && !toServerPages; | ||
const shouldBlock = shouldBlockToServer || shouldBlockToApp; | ||
const shouldRedirectToHome = loggedIn && fromServerPages; | ||
/** | ||
* Redirections between server and app pages are freely allowed | ||
*/ | ||
const shouldRedirect = !isNil(res) || shouldBlockToApp || shouldRedirectToHome; | ||
|
||
if (shouldRedirect) { | ||
const name = loggedIn ? '/' : res ?? serverLoginUrl; | ||
|
||
if (to.name !== name) { | ||
return { | ||
name, | ||
replace: true | ||
}; | ||
} | ||
} else if (shouldBlock) { | ||
useSnackbar(i18n.t('unauthorized'), 'error'); | ||
|
||
return false; | ||
} | ||
}; |
Oops, something went wrong.