/examples/auth.js

http://github.com/OpenRTMFP/ArcusNode · JavaScript · 36 lines · 19 code · 7 blank · 10 comment · 6 complexity · c747f07c379001a64885e72e76234781 MD5 · raw file

  1. var ArcusNode = require('../lib/arcus_node.js');
  2. //Create a new ArcusNode instance
  3. var arc = new ArcusNode(null);
  4. arc.on('connect', function(nc, options, username, password){
  5. // Let the connection wait for auth,
  6. // if we need to authenticate the connection with data elsewhere
  7. nc.wait();
  8. var users = {
  9. foo: 'bar',
  10. name: 'password'
  11. }
  12. //Make sure the username and password are given
  13. if(typeof(username) == 'undefined' || typeof(password) == 'undefined'){
  14. //If not, let the connection fail -> NetConnection.Connect.Failed
  15. nc.fail();
  16. return;
  17. }
  18. //Check the credentials
  19. if(users[username] === password){
  20. //Accept the connection if credentials are correct -> NetConnection.Connect.Success
  21. //The specified argument is a description which can be accessed in the client at NetStatusEvent|info.description
  22. nc.accept('Authentication successfull.');
  23. } else {
  24. //Reject the connection with the given message -> NetConnection.Connect.Rejected
  25. nc.reject('Wrong credentials');
  26. }
  27. });
  28. //Start the server
  29. arc.run();