Check each node.js request for authentication credentials

前端 未结 3 1445
北海茫月
北海茫月 2021-01-30 23:08

I\'m using node.js with Express and connect-auth to authenticate users.

This is the verification when requesting /index:

if(req.isAuthenticated()) {
  re         


        
3条回答
  •  心在旅途
    2021-01-30 23:38

    Another way is to app.use a middleware function. (Example in CoffeeScript.)

    # middleware
    authKick = (req, res, next) ->
      if not do req.isAuthenticated then return res.redirect '/login'
      return do next
    
    # apply
    app.use authKick
    

    This will work on each request without having to touch the routes.

提交回复
热议问题