M
M
Michael2018-04-15 23:30:25
Node.js
Michael, 2018-04-15 23:30:25

How to give html page only to authorized users in express?

Hello. I am writing a chat on express and passport. Understood with authorization. Works like this:

function authUser() {
    return function(request, response, next) {
        request.isAuthenticated() ? next() : response.redirect('/')
    }
}

app.get('/chat', authUser(), (req, res) => {
    res.send('Это видят только авторизованные пользователи')
})

I have a chat.html page. I want only authorized users to see it. The content there is updated dynamically using websockets. So far I've done this:
app.get('/chat', authUser(), (req, res) => {
     response.sendFile(path.join(`${__dirname}/static/html/chat.html`))
})

But I feel that this approach is wrong. Maybe something better?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-04-16
@mak_ufo

Yes, everything is fine, you can use a template engine if you need replacements later, or even just cache it to a file so that you don’t read it from the hard disk every time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question