Skip to content

Check that a secret exists in the cache before allowing navigation to the details routes

Problem to solve

We've only implemented basic secrets routing in Add routing to secrets page (!134459 - merged) - any key is treated as a valid key when you navigate to it. We should check that a secret with that key exists before we allow navigation to the details/auditlog/edit routes for it. If no secret exists with that key, we should redirect to the index route.

Proposal

In ee/app/assets/javascripts/ci/secrets/router.js, add a function like this and use it as the beforeEnter for those routes:

+ const checkSecretExists = (to, _, next) => {
+   // TODO: redirect to index if secret does not exist in apollo cache
+   if (to.params.key) {
+     next();
+   } else {
+     next({ name: INDEX_ROUTE_NAME });
+   }
+ };

export default (base) => {
  return new VueRouter({
    mode: 'history',
    base,
    routes: [
      // ...
      {
        path: '/:key',
        component: SecretTabs,
+       beforeEnter: checkSecretExists,
        props: (route) => {
          return { secretKey: route.params.key, routeName: route.name };
        },
        // ...
      },
      {
        name: EDIT_ROUTE_NAME,
        path: '/:key/edit',
        component: SecretFormWrapper,
+       beforeEnter: checkSecretExists,
        props: {
          isEditing: true,
        },
        meta: {
          getBreadcrumbText: () => __('Edit'),
        },
      },
      // ...
    ],
  });
};