koajs / static

Static file server middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Protected path with auth 🚧 -- Question

macarthuror opened this issue Β· comments

Hello everyone πŸ––

Is it possible to protect a path in the public directory?

Right now I'm developing a project using sessions for the auth, but I want to protect the resources for the no authenticated people.

This is my code rigth now

app.use(mount('/assets', serve(__dirname + '/../public')))

I'm trying to implement a middleware before serve the files but all what I tried failed.

Does anyone have some idea to solve it ?

Thanks 😊

Allrady resolved πŸ‘Œ

To verify I used a global middleware who checks when someone want to get the files.

app.use((ctx, next) => {
  const { path } = ctx
  if (path.includes('/assets/private')) {
    if (!isAuth){
      ctx.throw(401)
      return
    }
    next()
  } else {
    next()
  }
})