node.jsexpressejsviewengine

how to give user specific file access in express js


I'm using express js and passport js as authentication system also using view engine. I'm looking for a solution that would give access to user and let users see their file, not the other one's file. for example, in the image folder, the user would access to their files and after that, I want to pass these files to view engine. If I use the public folder, anyone is able to see every file in there. what solution do you recommend?


Solution

  • You should create a directory for each users.

    then, for example, your URL is /show/files

    the inside your logic, filter the directory by user info.

    app.get('/show/files', (req,res)=>{
     // filter resources by user info
    })
    

    don't forget to create a secure URL for your resources.

    Bad Idea: /images/amin/profile.png

    Good Idea: create a route to serve your resources.

    app.get('/resources', (req,res)=>{
    // add query parameter for resource for example profile.png
    // then check user directory and send it
    })
    

    your url converts into

    /resousrce?file=profile.png