jeremyben/json-server-auth

Mouting json-server on non root path breaks DELETE

tmarkovic opened this issue · 2 comments

First off, thank you so much for this module. Really makes it fast to prototype JWT-auth.

It seems that if you mount your json-server-api on a non root path f.e "/api" like this

const rules = rewriter({
  "/api/users*": "/600/api/users$1",
  "/api/myresource*": "/640/api/myresource$1",
})
app.db = router.db
app.use(rules);

app.use(auth)
app.use("/api", router)

resourceId extraction breaks in guards.ts

      const [, mod, resource, id] = path.split('/');

which causes db.get to return undefined and entity.userId to throw an error

            if (id) {
                // prettier-ignore
                const entity = db.get(resource).getById(id).value();
                // get id if we are in the users collection
                const userId = resource === 'users' ? entity.id : entity.userId;
                hasRightUserId = userId == req.claims.sub;
            }

Might be me who missused the rewriter because configuring it like so
const rules = auth.rewriter({ users: 600, my-resource: 640, auth: 440 })

works like a charm

Yeah I didn't think of that use case, but glad if it still works the way you want 🙂
I'm still gonna look into it and keep the issue opened meanwhile.
Thanks !