2023-11-23 03:22:47 -08:00
|
|
|
import type { RouterMiddleware } from '@/types/router';
|
|
|
|
import { VIEWS } from '@/constants';
|
|
|
|
import type { AuthenticatedPermissionOptions } from '@/types/rbac';
|
2024-06-06 06:30:17 -07:00
|
|
|
import { isAuthenticated } from '@/utils/rbac/checks';
|
2023-11-23 03:22:47 -08:00
|
|
|
|
|
|
|
export const authenticatedMiddleware: RouterMiddleware<AuthenticatedPermissionOptions> = async (
|
|
|
|
to,
|
2024-05-17 03:29:47 -07:00
|
|
|
_from,
|
2023-11-23 03:22:47 -08:00
|
|
|
next,
|
2024-02-23 08:20:12 -08:00
|
|
|
options,
|
2023-11-23 03:22:47 -08:00
|
|
|
) => {
|
2024-02-23 08:20:12 -08:00
|
|
|
const valid = isAuthenticated(options);
|
2023-11-23 03:22:47 -08:00
|
|
|
if (!valid) {
|
|
|
|
const redirect =
|
|
|
|
to.query.redirect ??
|
|
|
|
encodeURIComponent(`${window.location.pathname}${window.location.search}`);
|
|
|
|
return next({ name: VIEWS.SIGNIN, query: { redirect } });
|
|
|
|
}
|
|
|
|
};
|