Guest User

Untitled

a guest
Dec 9th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. # Auth stream
  2.  
  3. A stream that authorizes your client before it allows data through to your secret stream.
  4.  
  5. On the client pipe your stream into the auth stream and out. Then login in to the auth stream.
  6.  
  7. On the server, handle your login logic in the auth stream and pass it a secret stream to give access to once authorized.
  8.  
  9. ``` js
  10. var Auth = require("auth")
  11. , net = require("net")
  12. , through = require("through")
  13.  
  14. net.createServer(function (stream) {
  15. var secret = through(function (data) {
  16. console.log(data === "anything")
  17. this.emit("data", "secret")
  18. })
  19. , auth = Auth(secret, function (user, pass) {
  20. if (user === "steve" && pass === "jones") {
  21. return true
  22. }
  23. })
  24.  
  25. stream.pipe(auth).pipe(stream)
  26. }).listen(8080, function () {
  27. var stream = net.connect(8080)
  28. , auth = Auth()
  29.  
  30. stream.pipe(auth).pipe(stream)
  31.  
  32. stream.on("data", function (secret) {
  33. console.log(secret === "secret")
  34. })
  35.  
  36. // login(user, pass)
  37. auth.login("steve", "jones")
  38.  
  39. stream.write("anything")
  40. })
  41. ```
Add Comment
Please, Sign In to add comment