Browse Source

Add middlewares

pull/2207/head
Hironsan 1 year ago
parent
commit
a603c41276
2 changed files with 25 additions and 0 deletions
  1. 12
      frontend/middleware/isProjectAdmin.ts
  2. 13
      frontend/middleware/setCurrentProject.ts

12
frontend/middleware/isProjectAdmin.ts

@ -0,0 +1,12 @@
import { NuxtAppOptions } from '@nuxt/types'
import _ from 'lodash'
export default _.debounce(async ({ app, route, redirect }: NuxtAppOptions) => {
const member = await app.$repositories.member.fetchMyRole(route.params.id)
const projectRoot = app.localePath('/projects/' + route.params.id)
const path = route.fullPath.replace(/\/$/g, '')
if (!member.isProjectAdmin && path !== projectRoot) {
return redirect(app.localePath('/projects/' + route.params.id))
}
}, 1000)

13
frontend/middleware/setCurrentProject.ts

@ -0,0 +1,13 @@
import { NuxtAppOptions } from '@nuxt/types'
export default async ({ app, route, redirect }: NuxtAppOptions) => {
const project = app.store.getters['projects/currentProject']
const isNotSet = Object.keys(project).length === 0 && project.constructor === Object
if (isNotSet || project.id !== route.params.id) {
try {
await app.store.dispatch('projects/setCurrentProject', route.params.id)
} catch (e) {
redirect('/projects')
}
}
}
Loading…
Cancel
Save