feat: new route guards for the new setup flow
This commit is contained in:
@@ -244,7 +244,7 @@ export const auth = {
|
|||||||
getFirstTimeInfraSetupStatus: async (): Promise<boolean> => {
|
getFirstTimeInfraSetupStatus: async (): Promise<boolean> => {
|
||||||
try {
|
try {
|
||||||
const res = await authQuery.getFirstTimeInfraSetupStatus();
|
const res = await authQuery.getFirstTimeInfraSetupStatus();
|
||||||
return res.data?.value;
|
return res.data?.value === 'true' ? true : false;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Setup is not done
|
// Setup is not done
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -1,14 +1,19 @@
|
|||||||
import { auth } from '~/helpers/auth';
|
import { auth } from '~/helpers/auth';
|
||||||
import { HoppModule } from '.';
|
import { HoppModule } from '.';
|
||||||
|
|
||||||
const isAdmin = () => {
|
const isSetupRoute = (to: unknown) => to === 'setup';
|
||||||
const user = auth.getCurrentUser();
|
|
||||||
return user ? user.isAdmin : false;
|
const isGuestRoute = (to: unknown) => ['index', 'enter'].includes(to as string);
|
||||||
|
|
||||||
|
const getAdminStatus = async () => {
|
||||||
|
const user = await auth.checkCurrentUser();
|
||||||
|
return !!user?.isAdmin;
|
||||||
};
|
};
|
||||||
|
|
||||||
const GUEST_ROUTES = ['index', 'enter'];
|
const getFirstTimeInfraSetupStatus = async () => {
|
||||||
|
const isInfraNotSetup = await auth.getFirstTimeInfraSetupStatus();
|
||||||
const isGuestRoute = (to: unknown) => GUEST_ROUTES.includes(to as string);
|
return isInfraNotSetup;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @module routers
|
* @module routers
|
||||||
@@ -24,13 +29,55 @@ const isGuestRoute = (to: unknown) => GUEST_ROUTES.includes(to as string);
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export default <HoppModule>{
|
export default <HoppModule>{
|
||||||
onBeforeRouteChange(to, from, next) {
|
async onBeforeRouteChange(to, _from, next) {
|
||||||
if (!isGuestRoute(to.name) && !isAdmin()) {
|
const isAdmin = await getAdminStatus();
|
||||||
next({ name: 'index' });
|
|
||||||
} else if (isGuestRoute(to.name) && isAdmin()) {
|
if (!isAdmin) {
|
||||||
next({ name: 'dashboard' });
|
auth.signOutUser();
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Route Guards
|
||||||
|
if (!isGuestRoute(to.name) && !isAdmin) {
|
||||||
|
/**
|
||||||
|
* Reroutes the user to the login page if user is not logged in
|
||||||
|
* and is not an admin
|
||||||
|
*/
|
||||||
|
return next({ name: 'index' });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isAdmin) {
|
||||||
|
// This block applies to the case where the user is logged in successfully and validated as an admin
|
||||||
|
const isInfraNotSetup = await getFirstTimeInfraSetupStatus();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reroutes the user to the dashboard homepage if they have setup the infra already
|
||||||
|
* Else, the Setup page
|
||||||
|
*/
|
||||||
|
if (isGuestRoute(to.name)) {
|
||||||
|
console.log('hi');
|
||||||
|
|
||||||
|
const name = isInfraNotSetup ? 'setup' : 'dashboard';
|
||||||
|
return next({ name });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reroutes the user to the dashboard homepage if they have setup the infra already
|
||||||
|
* and are trying to access the setup page
|
||||||
|
*/
|
||||||
|
if (isSetupRoute(to.name) && !isInfraNotSetup) {
|
||||||
|
return next({ name: 'dashboard' });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reroutes the user to the setup page if they have not setup the infra yet
|
||||||
|
* and tries to access a valid route which is not a guest route
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (isInfraNotSetup && !isSetupRoute(to.name)) {
|
||||||
|
return next({ name: 'setup' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user