Skip to content

How to implement user authorization? #112

Answered by hst-m
caracal7 asked this question in Q&A
Discussion options

You must be logged in to vote

@lagden the point of the upgrade handler is to deny the request before the socket opens so you would want to update that

Here is JsonWebToken example

app.ws('/', {
   upgrade:(res, req, context) => {
      try { res.user = decodeJwtCookie(res, req, 'cookieName'); }
      catch { return res.writeStatus('401').end(); }
      res.upgrade({ uid: res.user._id }, req.getHeader('sec-websocket-key'), req.getHeader('sec-websocket-protocol'), req.getHeader('sec-websocket-extensions'), context);
   },
   open: ws => console.log('open-ws', ws.uid)
});


const getCookie = (res, req, name) => { res.cookies ??= req.getHeader('cookie'); return res.cookies && res.cookies.match(getCookie[name] ??= new RegExp(

Replies: 8 comments 10 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
10 replies
@dragonlobster
Comment options

@e3dio
Comment options

@dragonlobster
Comment options

@Wyzix33
Comment options

@lagden
Comment options

Answer selected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
9 participants
Converted from issue

This discussion was converted from issue #112 on December 09, 2020 11:47.